From a5eeb2371692fe0986ea0d24588cb2c67ceb34b1 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 21:12:52 +0800 Subject: [PATCH 1/3] Panda & Tiger level complete: can specify shows for a specific day --- db/seed.rb | 2 ++ models/show.rb | 2 +- watchman.rb | 12 ++++++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/db/seed.rb b/db/seed.rb index 3c028ff..888e13e 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: "How I met your mother", day_of_week: "Wednesday", hour_of_day: 20, network: nbc) +Show.create(name: "Modern Family", day_of_week: "Monday", hour_of_day: 19, network: nbc) diff --git a/models/show.rb b/models/show.rb index 6c82f65..57ebaba 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 #{hour_of_day}:00, #{day_of_week} on #{network} " end end diff --git a/watchman.rb b/watchman.rb index ebe9be4..3f3bef7 100644 --- a/watchman.rb +++ b/watchman.rb @@ -6,10 +6,14 @@ require "./db/seed" puts "There are #{Show.count} in the database" +puts +puts "What day would you like to watch shows?" +puts +print '~>' +day = gets.chomp.capitalize Network.all.each do |network| - puts "Shows airing on #{network}" - network.shows.each do |show| - puts show - end + network.shows.where(day_of_week: day).each do |show| + puts show + end end From 438d38e9206d21de3471666175074d94e1ed10e1 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 21:39:50 +0800 Subject: [PATCH 2/3] Eagle level: created a new sport class which you can interact with via the console --- db/migrate/201212102100_create_sports.rb | 9 ++++++++ db/seed.rb | 10 ++++++++ models/sport.rb | 6 +++++ watchman.rb | 29 +++++++++++++++++++++++- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 db/migrate/201212102100_create_sports.rb create mode 100644 models/sport.rb diff --git a/db/migrate/201212102100_create_sports.rb b/db/migrate/201212102100_create_sports.rb new file mode 100644 index 0000000..9d99bb6 --- /dev/null +++ b/db/migrate/201212102100_create_sports.rb @@ -0,0 +1,9 @@ +class CreateSports < ActiveRecord::Migration + def change + create_table :sports do |t| + t.string :name + t.string :description + t.timestamps + end + end +end diff --git a/db/seed.rb b/db/seed.rb index 888e13e..d92382d 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -1,9 +1,19 @@ # Cleaning Out Network.delete_all Show.delete_all +Sport.delete_all +# Create Networks amc = Network.create(name: "AMC") nbc = Network.create(name: "NBC") +# Create Shows 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: "How I met your mother", day_of_week: "Wednesday", hour_of_day: 20, network: nbc) Show.create(name: "Modern Family", day_of_week: "Monday", hour_of_day: 19, network: nbc) +# Create Sports +Sport.create(name: "Rugby", description: "Rugby is a style of football named after Rugby School in the United Kingdom. It is seen most prominently in two current sports, rugby league and rugby union.") +Sport.create(name: "Tennis", description: "Tennis is a sport usually played between two players using a racquet and ball. The object of the game is to play the ball in such a way that the opponent is not able to hit a good return.") +Sport.create(name: "Golf", description: "Golf is a precision sport, in which competing players use many types of clubs to hit balls into a series of holes using the fewest number of strokes.") +Sport.create(name: "Boxing", description: "Boxing is a combat sport in which two people engage in a contest of strength, reflexes and endurance by throwing punches at an opponent with gloved hands.") +Sport.create(name: "Soccer", description: "Soccer is a sport played between two teams of eleven players with a spherical ball.") + diff --git a/models/sport.rb b/models/sport.rb new file mode 100644 index 0000000..b4d6abc --- /dev/null +++ b/models/sport.rb @@ -0,0 +1,6 @@ +class Sport < ActiveRecord::Base + + def to_s + "#{name}" + end +end diff --git a/watchman.rb b/watchman.rb index 3f3bef7..f954aad 100644 --- a/watchman.rb +++ b/watchman.rb @@ -5,7 +5,7 @@ Dir.glob('./models/*').each { |r| require r} require "./db/seed" -puts "There are #{Show.count} in the database" +puts "There are #{Show.count} shows in the database" puts puts "What day would you like to watch shows?" puts @@ -17,3 +17,30 @@ puts show end end + +puts +puts "Great, now I also have some sports I can tell you about." +puts "Check out all the sports I know:" +Sport.all.each do |sport| + puts sport +end +puts "Now choose one and enter it below to find out more about it." +print "~>" + +input = gets.chomp.capitalize +sport = Sport.where(name: input) + +while sport.empty? + puts "Sorry, I can't do that right now." + puts "~>" + input = gets.chomp.capitalize + sport = Sport.where(name: input) +end + +puts +puts "A not so fun fact about #{sport.first.name}:" +puts + +Sport.where(name: input).each do |sport| + puts sport.description +end From e89d4ae0cd862133bfb7cd349a5b1ba55f280c62 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Thu, 13 Dec 2012 14:21:55 +0800 Subject: [PATCH 3/3] Fixed code so it fits with singular description --- watchman.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/watchman.rb b/watchman.rb index f954aad..0b66008 100644 --- a/watchman.rb +++ b/watchman.rb @@ -28,17 +28,18 @@ print "~>" input = gets.chomp.capitalize -sport = Sport.where(name: input) +sport = Sport.where(name: input).first -while sport.empty? +while sport.nil? puts "Sorry, I can't do that right now." - puts "~>" + puts "Try another sport..." + print "~>" input = gets.chomp.capitalize - sport = Sport.where(name: input) + sport = Sport.where(name: input).first end puts -puts "A not so fun fact about #{sport.first.name}:" +puts "A not so fun fact about #{sport.name}:" puts Sport.where(name: input).each do |sport|