Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions adv.rb
Original file line number Diff line number Diff line change
@@ -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.'
35 changes: 0 additions & 35 deletions adventure.rb

This file was deleted.

6 changes: 0 additions & 6 deletions config/database.yml.sample

This file was deleted.

20 changes: 12 additions & 8 deletions db/migrate/001_creates_page.rb
Original file line number Diff line number Diff line change
@@ -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

12 changes: 11 additions & 1 deletion db/seed.rb
Original file line number Diff line number Diff line change
@@ -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?")


2 changes: 1 addition & 1 deletion db/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
27 changes: 13 additions & 14 deletions models/book.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 6 additions & 7 deletions models/page.rb
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

end
end
52 changes: 26 additions & 26 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 25 additions & 21 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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




2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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"