Skip to content

Commit

Permalink
created after and before for accessing places, with specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrasaurus committed Aug 30, 2010
1 parent 9d56bb8 commit 2a6a20f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 22 deletions.
2 changes: 1 addition & 1 deletion book.rb
Expand Up @@ -8,7 +8,7 @@ def make_pie(&block)

make_pie do
create_places do
ship description:"ookina funa"
ship description:"ookina fune"
building description:"ookina biru"
tower description:"ookina towa"
end
Expand Down
29 changes: 25 additions & 4 deletions pie.rb
Expand Up @@ -20,10 +20,31 @@ class WebApp < Sinatra::Base
attr_accessor :places
attr_accessor :images

class Places < Array
def method_missing name, *args
puts "making a #{name} with #{args[0].inspect}"
self << {name.to_sym => args[0]}
class Places < Hash
def method_missing name, options = {}
unless options.is_a? Hash
puts "options for creating a place must have key and value"
return
end
puts "making a #{name} with #{options.inspect}"
self[name.to_sym] = options
end

def after(place_name)
index = keys.index(place_name.to_sym)
next_place_name = keys[index + 1]
self[next_place_name]
end

def before(place_name)
index = keys.index(place_name.to_sym)
index = index - 1
if index < 0
nil
else
prev_place_name = keys[index]
self[prev_place_name]
end
end
end

Expand Down
99 changes: 82 additions & 17 deletions pie_spec.rb
@@ -1,30 +1,95 @@
require File.expand_path(File.dirname(__FILE__) + '/pie')

describe "making pie" do
describe "image declaration" do
it "can declare an image" do
make_pie do
image ship:"http://foo.com/ship.png"
end
$pie.images[:ship].should == "http://foo.com/ship.png"
end

it "can declare an image" do
make_pie do
image ship:"http://foo.com/ship.png"
it "can declare multiple image statements" do
make_pie do
image ship:"http://foo.com/ship.png"
image basket:"http://foo.com/basket.png"
end
$pie.images[:ship].should == "http://foo.com/ship.png"
$pie.images[:basket].should == "http://foo.com/basket.png"
end
$pie.images[:ship].should == "http://foo.com/ship.png"
end

it "can declare multiple image statements" do
make_pie do
image ship:"http://foo.com/ship.png"
image basket:"http://foo.com/basket.png"
it "can declare multiple images with one statement" do
make_pie do
image ship:"http://foo.com/ship.png",
basket:"http://foo.com/basket.png"
end
$pie.images[:ship].should == "http://foo.com/ship.png"
$pie.images[:basket].should == "http://foo.com/basket.png"
end
$pie.images[:ship].should == "http://foo.com/ship.png"
$pie.images[:basket].should == "http://foo.com/basket.png"
end

it "can declare multiple images with one statement" do
make_pie do
image ship:"http://foo.com/ship.png",
basket:"http://foo.com/basket.png"
describe "creates places" do
it "can create a place without a description" do
make_pie do
create_places do
ship
end
end
$pie.places[:ship].should == {}
end

it "cannot create a place if options are invalid (given as a symbol not a Hash)" do
make_pie do
create_places do
result = ship :invalid
result.should == nil
end
end
$pie.places[:ship].should == nil
end
$pie.images[:ship].should == "http://foo.com/ship.png"
$pie.images[:basket].should == "http://foo.com/basket.png"

end
describe "can access places" do
before do
make_pie do
create_places do
ship description:"ookina fune"
building description:"ookina biru"
tower description:"ookina towa"
end
end
end

it "which are accessible by named key" do
ship = $pie.places[:ship]
ship.should_not be_nil
ship[:description].should == "ookina fune"
end

it "resulting in 2 places" do
$pie.places.length.should == 3
end

it "and can find place after named place" do
building = $pie.places.after(:ship)
building.should_not be_nil
building[:description].should == "ookina biru"
end

it "and finds nil after last place" do
nothing = $pie.places.after(:tower)
nothing.should be_nil
end

it "and can find place before named place" do
building = $pie.places.before(:tower)
building.should_not be_nil
building[:description].should == "ookina biru"
end

it "and finds nil before first place" do
nothing = $pie.places.before(:ship)
nothing.should be_nil
end
end
end

0 comments on commit 2a6a20f

Please sign in to comment.