From a0975fd2647ac717134e37fcb89c25bc80998102 Mon Sep 17 00:00:00 2001 From: ardder Date: Sat, 13 Jul 2013 13:37:01 +0800 Subject: [PATCH 1/5] panda level finished --- db/seed.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/db/seed.rb b/db/seed.rb index 3c028ff..b3d494b 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -5,3 +5,5 @@ nbc = Network.create(name: "NBC") Show.create(name: "Mad Men", day_of_week: "Sunday", hour_of_day: 22, network: amc) Show.create(name: "Community", day_of_week: "Thursday", hour_of_day: 20, network: nbc) +Show.create(name: "Monday Show", day_of_week: "Monday", hour_of_day: 20, network: amc) +Show.create(name: "Sunday Show", day_of_week: "Sunday", hour_of_day: 20, network: nbc) From 916da2c4a455de2f86069e768aebe0a449e030e1 Mon Sep 17 00:00:00 2001 From: ardder Date: Mon, 15 Jul 2013 13:16:30 +0800 Subject: [PATCH 2/5] tiger level completed --- models/show.rb | 2 +- watchman.rb | 48 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/models/show.rb b/models/show.rb index 6c82f65..d1d7cb6 100644 --- a/models/show.rb +++ b/models/show.rb @@ -4,6 +4,6 @@ class Show < ActiveRecord::Base validates_presence_of :name def to_s - "#{name} airs at #{hour_of_day}:#{day_of_week}:00 on #{network} " + "#{name} airs at #{day_of_week} #{hour_of_day}:00 on #{network} " end end diff --git a/watchman.rb b/watchman.rb index ebe9be4..6c577bf 100644 --- a/watchman.rb +++ b/watchman.rb @@ -2,14 +2,48 @@ require 'bundler/setup' require "./db/setup" -Dir.glob('./models/*').each { |r| require r} +Dir.glob('./models/*').each { |r| require r } require "./db/seed" -puts "There are #{Show.count} in the database" +WEEK_DAY = { + '1' => "Monday", + '2' => "Tuesday", + '3' => "Wednesday", + '4' => "Thursday", + '5' => "Friday", + '6' => "Saturday", + '7' => "Sunday" +} + +def select_day + puts "-" * 80 + puts "Which week day do you want watch TV shows?" + puts "1: Monday, 2: Tuesday, 3: Wednesday, 4: Thursday, 5: Friday, 6: Saturday, 7: Sunday or Q: Quit" + STDOUT.write "Select a day [1~7 or Q]:" + gets.chomp +end + +while true + day = select_day + + if day.upcase == 'Q' + puts "Goodbye!" + break + end + + unless day.match(/^[1-7]$/) + puts "No such day: '#{day}', please select again!" + next + end + + sday = WEEK_DAY[day] + shows = Show.where(day_of_week: sday) + + if shows.empty? + puts "No shows on #{sday}" + else + puts "Shows on #{sday}:" + shows.each {|show| puts show} + end -Network.all.each do |network| - puts "Shows airing on #{network}" - network.shows.each do |show| - puts show - end end From 5c695159766d52a72bfbb31b0b77560c54a967b1 Mon Sep 17 00:00:00 2001 From: ardder Date: Tue, 16 Jul 2013 21:57:51 +0800 Subject: [PATCH 3/5] refactoring tiger level --- watchman.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/watchman.rb b/watchman.rb index 6c577bf..9b2e89c 100644 --- a/watchman.rb +++ b/watchman.rb @@ -18,31 +18,31 @@ def select_day puts "-" * 80 puts "Which week day do you want watch TV shows?" - puts "1: Monday, 2: Tuesday, 3: Wednesday, 4: Thursday, 5: Friday, 6: Saturday, 7: Sunday or Q: Quit" - STDOUT.write "Select a day [1~7 or Q]:" + puts WEEK_DAY.map {|k, v| "#{k}: #{v}"}.join(', ') + " or Q: Quit" + print "Select a day [1~7 or Q]:" gets.chomp end while true - day = select_day + answer = select_day - if day.upcase == 'Q' + if answer.upcase == 'Q' puts "Goodbye!" break end - unless day.match(/^[1-7]$/) - puts "No such day: '#{day}', please select again!" + unless WEEK_DAY.keys.include?(answer) + puts "No such day: '#{answer}', please select again!" next end - sday = WEEK_DAY[day] - shows = Show.where(day_of_week: sday) + selected_day = WEEK_DAY[answer] + shows = Show.where(day_of_week: selected_day) if shows.empty? - puts "No shows on #{sday}" + puts "No shows on #{selected_day}" else - puts "Shows on #{sday}:" + puts "Shows on #{selected_day}:" shows.each {|show| puts show} end From 6253fb3bfe60914efc87dcf0070c5121b5569c1a Mon Sep 17 00:00:00 2001 From: ardder Date: Tue, 16 Jul 2013 22:21:54 +0800 Subject: [PATCH 4/5] more refactoring --- watchman.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/watchman.rb b/watchman.rb index 9b2e89c..88c33fd 100644 --- a/watchman.rb +++ b/watchman.rb @@ -31,12 +31,14 @@ def select_day break end - unless WEEK_DAY.keys.include?(answer) - puts "No such day: '#{answer}', please select again!" + + selected_day = WEEK_DAY[answer] + + unless selected_day + puts "Invalid selection: '#{answer}', please select again!" next end - selected_day = WEEK_DAY[answer] shows = Show.where(day_of_week: selected_day) if shows.empty? From 70d74f3e329838a09c114ca15d275e0284c0f80d Mon Sep 17 00:00:00 2001 From: ardder Date: Thu, 18 Jul 2013 16:40:05 +0800 Subject: [PATCH 5/5] eagle level completed --- .../201307152240_create_authors_and_books.rb | 14 +++++ db/seed.rb | 19 +++++++ models/author.rb | 7 +++ models/book.rb | 7 +++ watchman.rb | 54 ++++++------------- 5 files changed, 62 insertions(+), 39 deletions(-) create mode 100644 db/migrate/201307152240_create_authors_and_books.rb create mode 100644 models/author.rb create mode 100644 models/book.rb diff --git a/db/migrate/201307152240_create_authors_and_books.rb b/db/migrate/201307152240_create_authors_and_books.rb new file mode 100644 index 0000000..9c88796 --- /dev/null +++ b/db/migrate/201307152240_create_authors_and_books.rb @@ -0,0 +1,14 @@ +class CreateAuthorsAndBooks < ActiveRecord::Migration + def change + create_table :authors do |t| + t.string :name + t.timestamps + end + + create_table :books do |t| + t.string :title + t.references :author + t.timestamps + end + end +end \ No newline at end of file diff --git a/db/seed.rb b/db/seed.rb index b3d494b..1ddacd1 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -7,3 +7,22 @@ Show.create(name: "Community", day_of_week: "Thursday", hour_of_day: 20, network: nbc) Show.create(name: "Monday Show", day_of_week: "Monday", hour_of_day: 20, network: amc) Show.create(name: "Sunday Show", day_of_week: "Sunday", hour_of_day: 20, network: nbc) + + +Book.delete_all +Author.delete_all + +michael = Author.create(name: 'Michael Crichton') +dan = Author.create(name: 'Dan Brown') +daniel = Author.create(name: 'Daniel Suarez') +stuart = Author.create(name: 'Stuart Kauffman') +douglas = Author.create(name: 'Douglas R. Hofstadter') + +Book.create(title: 'Sphere', author: michael) +Book.create(title: 'Jurassic Park', author: michael) +Book.create(title: 'The Da Vinci Code', author: dan) +Book.create(title: 'Inferno', author: dan) +Book.create(title: 'Freedom', author: daniel) +Book.create(title: 'Daemon', author: daniel) +Book.create(title: 'At Home In The Universe', author: stuart) +Book.create(title: 'Godel, Escher, Bach: An Eternal Golden Braid', author: douglas) diff --git a/models/author.rb b/models/author.rb new file mode 100644 index 0000000..77ff271 --- /dev/null +++ b/models/author.rb @@ -0,0 +1,7 @@ +class Author < ActiveRecord::Base + has_many :books + + def to_s + "#{name}, with #{books.count} book(s)" + end +end \ No newline at end of file diff --git a/models/book.rb b/models/book.rb new file mode 100644 index 0000000..cd2024c --- /dev/null +++ b/models/book.rb @@ -0,0 +1,7 @@ +class Book < ActiveRecord::Base + belongs_to :author + + def to_s + title + end +end \ No newline at end of file diff --git a/watchman.rb b/watchman.rb index 88c33fd..6ee86f6 100644 --- a/watchman.rb +++ b/watchman.rb @@ -5,47 +5,23 @@ Dir.glob('./models/*').each { |r| require r } require "./db/seed" -WEEK_DAY = { - '1' => "Monday", - '2' => "Tuesday", - '3' => "Wednesday", - '4' => "Thursday", - '5' => "Friday", - '6' => "Saturday", - '7' => "Sunday" -} - -def select_day - puts "-" * 80 - puts "Which week day do you want watch TV shows?" - puts WEEK_DAY.map {|k, v| "#{k}: #{v}"}.join(', ') + " or Q: Quit" - print "Select a day [1~7 or Q]:" - gets.chomp +def horizontal_line + '-' * 80 end +puts "All authors in our database:" -while true - answer = select_day - - if answer.upcase == 'Q' - puts "Goodbye!" - break - end - +puts horizontal_line +Author.all.each {|author| puts author} +puts horizontal_line - selected_day = WEEK_DAY[answer] +puts "Which author are you interested in?" +name = gets.chomp - unless selected_day - puts "Invalid selection: '#{answer}', please select again!" - next - end +author = Author.where(name: name).first - shows = Show.where(day_of_week: selected_day) - - if shows.empty? - puts "No shows on #{selected_day}" - else - puts "Shows on #{selected_day}:" - shows.each {|show| puts show} - end - -end +if author + puts "Books written by #{name}:" + author.books.each_with_index {|book, i| puts "#{i+1}. #{book.title}"} +else + puts "Sorry, we don't have any book written by #{name}." +end \ No newline at end of file