diff --git a/adv.rb b/adv.rb new file mode 100644 index 0000000..1813803 --- /dev/null +++ b/adv.rb @@ -0,0 +1,21 @@ +require 'rubygems' +require 'bundler/setup' + +require_relative 'db/setup' +Dir.glob('./models/*').each { |r| require r} +require "./db/seed" + +book = Book.new(Page.starting_point) + +until book.complete_game? do + puts book.current_page.content + puts "your options: " + puts " - #{book.current_page.options.first.preview}" + puts " - #{book.current_page.options.last.preview}" + puts "What do you want to do? Enter A or B" + + book.input( gets ) +end +puts book.current_page.content +puts '' +puts 'Maybe you can come back again if you\'re not too rich or too dead. Hope you had fun.' \ No newline at end of file diff --git a/adventure.rb b/adventure.rb deleted file mode 100644 index d65f616..0000000 --- a/adventure.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'rubygems' -require 'bundler/setup' - -require_relative 'db/setup' -require_relative 'models/page' -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: "Walk down the road") - -book = Book.new(page) - -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 "What do you want to do? Enter A or B" - - book.input( gets ) - -end -puts "------------------------------------------" -puts "| |" -puts "| |" -puts "| ADVENTURE COMPLETE |" -puts "| |" -puts "| |" -puts "------------------------------------------" - - -puts book.current_page.content - -puts "hope you won!" diff --git a/config/database.yml.sample b/config/database.yml.sample deleted file mode 100644 index 2e5c2a9..0000000 --- a/config/database.yml.sample +++ /dev/null @@ -1,6 +0,0 @@ -host: 'localhost' -adapter: 'postgresql' -database: 'episode5' -username: XXXXXXX -encoding: 'utf8' -pool: 5 diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb index 8a293c0..abcd9d0 100644 --- a/db/migrate/001_creates_page.rb +++ b/db/migrate/001_creates_page.rb @@ -1,10 +1,14 @@ class CreatesPage < ActiveRecord::Migration - def change - create_table :pages do |t| - t.text :content - t.integer :parent_id - t.boolean :starting_point, default: false - t.boolean :conclusion, default: false - end - end + def change + create_table :pages do |t| + t.text :preview + t.text :content + t.integer :parent_id + t.integer :option_a_id + t.integer :option_b_id + t.boolean :starting_point, default: false + t.boolean :conclusion, default: false + end + end end + diff --git a/db/seed.rb b/db/seed.rb index 1abe902..a0f9bd3 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -1 +1,11 @@ -# Cleaning Out + +Page.delete_all +steady = Page.create(conclusion: true, preview: 'You move forward', + content: 'You crash a secret conference of top teer venture capitalist. They like you so much they give you some pocket change. 30 bags of gold: WINNER') +sleep = Page.create(conclusion: true, preview: 'You lay down for a power nap', + content: 'A clan of dwarfs adopt you and name you sleepy. Unfortunately that very day grumpy\'s new pick-ax arrives from Amazon and you loose your head: LOSER') +forest = Page.create(option_a_id: steady.id, option_b_id: sleep.id, preview: "Go into the Forest", content: "You see smoke in the distance.") +lake = Page.create(option_a_id: steady.id, option_b_id: sleep.id, preview: "Sail across the Lake", content: "You begin to hear music in the distance.") +page = Page.create(starting_point: true, option_a_id: forest.id, option_b_id: lake.id, preview: "Welcome subjects", 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?") + + diff --git a/db/setup.rb b/db/setup.rb index 0e80690..1cbf7ce 100644 --- a/db/setup.rb +++ b/db/setup.rb @@ -12,4 +12,4 @@ # connect to it ActiveRecord::Base.establish_connection(connection_details) # Migrate all the things -ActiveRecord::Migrator.migrate("db/migrate/") +ActiveRecord::Migrator.migrate("db/migrate/") \ No newline at end of file diff --git a/models/book.rb b/models/book.rb index 5eb6f53..1b04653 100644 --- a/models/book.rb +++ b/models/book.rb @@ -1,21 +1,20 @@ class Book - attr_reader :current_page + attr_reader :current_page def initialize(starting_page) @current_page = starting_page - end + end - def input(input_string) - if input_string.chomp == "A" - @current_page = current_page.options.first - elsif input_string.chomp == "B" - @current_page = current_page.options.last - end - end + def input(input_string) + if input_string.chomp == "A" + @current_page = current_page.options.first + elsif input_string.chomp == "B" + @current_page = current_page.options.last + end + end - def complete_game? - current_page.conclusion? - end - -end + def complete_game? + current_page.conclusion? + end +end \ No newline at end of file diff --git a/models/page.rb b/models/page.rb index 2b88343..f440d9b 100644 --- a/models/page.rb +++ b/models/page.rb @@ -1,11 +1,10 @@ class Page < ActiveRecord::Base - def self.starting_point - Page.where(starting_point: true).first - end - - def options - Page.where(:parent_id => id).limit(2) - end + def self.starting_point + Page.where(starting_point: true).first + end + def options + Page.find(option_a_id, option_b_id) + end end diff --git a/spec/book_spec.rb b/spec/book_spec.rb index b429112..bcb3f53 100644 --- a/spec/book_spec.rb +++ b/spec/book_spec.rb @@ -1,33 +1,33 @@ require_relative "spec_helper" describe Book do - let!(:page) {Page.create(starting_point: true)} - subject { Book.new(page) } + let!(:option_a) {Page.create(preview: 'view option_a')} + let!(:option_b) {Page.create(preview: 'view option_b')} + let!(:page) {Page.create(starting_point: true, option_a_id: option_a.id, option_b_id: option_b.id)} + subject { Book.new(page) } - it "should have a page" do - subject.current_page.should eq(page) - end + it "should have a page" do + subject.current_page.should eq(page) + end - describe "#input" do - let!(:option_a) { Page.create(parent_id: page.id)} - let!(:option_b) { Page.create(parent_id: page.id)} + describe "#input" do - it "should receive input and opens page" do - subject.input("A") - subject.current_page.should eq(option_a) - end - it "should receive input and opens page" do - subject.input("B") - subject.current_page.should eq(option_b) - end - - end - - describe "#complete_game?" do - - it "should know when it's done" do - subject.stub(:current_page) { stub(:conclusion? => true)} - subject.complete_game?.should be_true - end - end + it "should receive input and opens page" do + subject.input("A") + subject.current_page.should eq(option_a) + end + it "should receive input and opens page" do + subject.input("B") + subject.current_page.should eq(option_b) + end end + +describe "#complete_game?" do + let!(:page) {Page.create(starting_point: true)} + subject { Book.new(page) } + it "should know when it's done" do + subject.stub(:current_page) { stub(:conclusion? => true)} + subject.complete_game?.should be_true + end + end +end \ No newline at end of file diff --git a/spec/page_spec.rb b/spec/page_spec.rb index 5951cdd..172a03f 100644 --- a/spec/page_spec.rb +++ b/spec/page_spec.rb @@ -6,38 +6,42 @@ Page.delete_all end - it "should know if it's at the end of the road" do + it "should know it's at the end of the road" do page = Page.create(conclusion: true) page.conclusion?.should be_true + end it "should have awesome content" do page = Page.create(content: "The fox and hound get along") Page.find(page.id).content.should eq("The fox and hound get along") - end + 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) } + context "#options" do + let(:option_a) {Page.create(preview: "Option_a view") } + let(:option_b) {Page.create(preview: "Option_b view") } + subject {Page.create(option_a_id: option_a.id, option_b_id: option_b.id)} - it "should have options for the next pages" do - subject.options.should eq([option_a, option_b]) - end - end + it "should have options for the next pages" do + subject.options.should eq([option_a, option_b]) + end + 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 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 + it "should have a starting point" do + the_page = Page.create(starting_point: true) + Page.starting_point.should eq(the_page) + end end + + + + \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bdb95b8..0cf24dc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,5 @@ require "rspec" require 'bundler/setup' -require_relative '../db/setup' +require_relative '../db/setup' require_relative "../models/page" require_relative "../models/book"