main
1require 'spec_helper'
2
3
4class Blah
5end
6
7module LifeBoat
8 class Boat
9 end
10end
11
12module Lifeboat
13 class Ship
14 end
15end
16
17module ExposeBinding
18 def get_binding
19 binding
20 end
21end
22
23module LifeBoat
24 class FloatationDevice
25 end
26end
27
28module Queryable
29 def matches?
30 each do |item|
31 result = yield(item) if block_given?
32 return item if result
33 end
34 nil
35 end
36end
37
38
39class Book
40 attr_reader :title
41
42 def initialize(title)
43 @title = title
44 end
45end
46
47class Library
48 include Queryable
49
50 def initialize(books = [])
51 @books = books
52 end
53
54 def add(book)
55 @books.push(book)
56 end
57
58 def has_book?(title)
59 matches? do |book|
60 book.title == title
61 end
62 end
63
64 private
65
66 def each
67 @books.each do |book|
68 yield book
69 end
70 end
71end
72
73describe "modules" do
74 it "can float" do
75 device = LifeBoat::FloatationDevice.new
76 device.should_not be_nil
77 end
78
79 context "Library" do
80 let(:library) { Library.new }
81
82 before :each do
83 library.add(Book.new("roots"))
84 library.add(Book.new("the life of pie"))
85 library.add(Book.new("the curious incident of the dog in the night time"))
86 end
87
88 it "can find all items that match" do
89 library.has_book?("the curious incident of the dog in the night time").should be_true
90 end
91
92 it "should return false if the book is not in the library" do
93 library.has_book?("george of the jungle").should be_false
94 end
95
96 it "should tell how many books are in the library" do
97 library.extend(Enumerable)
98 library.count.should == 3
99 end
100 end
101end