From 7ef503b327a983d2fa76299c63893fe652d11d84 Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Mon, 21 Jan 2013 22:43:17 -0800 Subject: [PATCH] pages now have preview method; clearer outcome --- .gitignore | 2 ++ adventure.rb | 27 +++++++++++------------- db/migrate/001_creates_page.rb | 1 + models/page.rb | 6 +++++- spec/page_spec.rb | 38 +++++++++++++++++++++------------- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 61a293a..8836ce0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ doc/ config/database.yml TODO + +.idea diff --git a/adventure.rb b/adventure.rb index d65f616..31f3f3b 100644 --- a/adventure.rb +++ b/adventure.rb @@ -6,7 +6,7 @@ require_relative 'models/book' page = Page.create(starting_point: true, content: "You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose?") -Page.create(conclusion: true, parent_id: page.id, content: "Go into the forest") +Page.create(conclusion: true, parent_id: page.id, content: "Go into the forest", win: true) Page.create(conclusion: true, parent_id: page.id, content: "Walk down the road") book = Book.new(page) @@ -14,22 +14,19 @@ until book.complete_game? do puts book.current_page.content puts "your options: " - puts " - [#{book.current_page.options.first.content}]" - puts " - [#{book.current_page.options.last.content}]" + puts " Option A: [#{book.current_page.options.first.content}]" + puts " Option B: [#{book.current_page.options.last.content}]" puts "What do you want to do? Enter A or B" book.input( gets ) - + puts + puts book.current_page.preview + if book.current_page.win + puts 'You Win! You find the Fountain of Youth!' + else + puts 'You Lose! You are robbed and killed by evil ninjas..' + end end -puts "------------------------------------------" -puts "| |" -puts "| |" -puts "| ADVENTURE COMPLETE |" -puts "| |" -puts "| |" -puts "------------------------------------------" - - -puts book.current_page.content -puts "hope you won!" +puts +puts " The End! " diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb index 8a293c0..e8f025c 100644 --- a/db/migrate/001_creates_page.rb +++ b/db/migrate/001_creates_page.rb @@ -5,6 +5,7 @@ def change t.integer :parent_id t.boolean :starting_point, default: false t.boolean :conclusion, default: false + t.boolean :win, default: false end end end diff --git a/models/page.rb b/models/page.rb index 2b88343..0319693 100644 --- a/models/page.rb +++ b/models/page.rb @@ -6,6 +6,10 @@ def self.starting_point def options Page.where(:parent_id => id).limit(2) - end + end + + def preview + 'You ' + content + '...' + end end diff --git a/spec/page_spec.rb b/spec/page_spec.rb index 5951cdd..c8f930c 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -16,28 +16,38 @@ Page.find(page.id).content.should eq("The fox and hound get along") end - context "#options" do - subject {Page.create} - let(:option_a) {Page.create(parent_id: subject.id) } - let(:option_b) {Page.create(parent_id: subject.id) } - let(:option_c) {Page.create(parent_id: subject.id) } - - it "should have options for the next pages" do - subject.options.should eq([option_a, option_b]) - end - end - + describe "instance methods" do + subject {Page.create} + let(:option_a) {Page.create(parent_id: subject.id, content: 'Go to the store', win: true) } + let(:option_b) {Page.create(parent_id: subject.id, content: 'Take a nap') } + let(:option_c) {Page.create(parent_id: subject.id) } + + context "#options" do + it "should have options for the next pages" do + subject.options.should eq([option_a, option_b]) + end + end + + context "#preview" do + it "should show preview" do + option_a.preview.should eq('You Go to the store...') + end + end + end + + it "should not be a winning page by default" do + a_page = Page.create + a_page.win?.should_not be_true + end it "should not be a starting point by default" do Page.create.starting_point.should eq(false) end it "should not be a conclusion by default" do Page.create.conclusion.should eq(false) end - - it "should have a starting point" do the_page = Page.create(starting_point: true) Page.starting_point.should eq(the_page) - end + end end