Commit 046bf60

Stephen Peasley <stephenpeasley@hotmail.com>
2013-09-28 14:14:27
working towards extracting to a module.
1 parent ef17da8
Changed files (3)
spec/lesson_four/lesson_spec.rb
@@ -0,0 +1,75 @@
+require 'spec_helper'
+
+class Blah
+end
+
+module LifeBoat
+  class FloatationDevice
+  end
+end
+
+
+class Book
+  attr_reader :title
+  
+  def initialize(title)
+    @title = title
+  end
+end
+
+class Library
+  def initialize(books = [])
+    @books = books
+  end
+  
+  def add(book)
+    @books.push(book)
+  end
+  
+  def has_book?(title)
+    matches? do |book|
+      book.title == title
+    end
+  end
+  
+  private
+  
+  def all
+    @books.each do |book|
+      yield book
+    end
+  end
+  
+  def matches?
+    all do |item|
+      result = yield(item) if block_given?
+      return item if result
+    end
+    nil
+  end  
+end
+
+describe "modules" do
+  it "can float" do
+    device = LifeBoat::FloatationDevice.new
+    device.should_not be_nil
+  end
+  
+  context "Library" do
+    let(:library) { Library.new }
+    
+    before :each do
+      library.add(Book.new("roots"))
+      library.add(Book.new("the life of pie"))
+      library.add(Book.new("the curious incident of the dog in the night time"))
+    end
+    
+    it "can find all items that match" do
+      library.has_book?("the curious incident of the dog in the night time").should be_true
+    end
+    
+    it "should return false if the book is not in the library" do
+      library.has_book?("george of the jungle").should be_false
+    end
+  end
+end
\ No newline at end of file
spec/lesson_four/review_spec.rb
@@ -0,0 +1,45 @@
+require "spec_helper"
+require 'ostruct'
+
+class Book
+  attr_accessor :title
+  def to_s
+    title
+  end
+end
+
+class Library
+  def initialize(books = [])
+    @books = books
+  end
+  
+  def add(book)
+    @books.push(book)
+  end
+  
+  def print_each_title
+    puts "PRINTING EACH TITLE #{@books.size}"
+    @books.each do |book|
+      puts book
+    end
+  end
+end
+
+describe Library do
+  xit "can add a book to the library" do
+    library = Library.new
+    book = Book.new
+    library.add(book)
+    book.title = "blah"
+    library.print_each_title
+  end
+  
+  it "can pass by value" do
+    library = Library.new
+    book = 1
+    library.add(book)
+    book = "not blah"
+    library.print_each_title    
+  end
+  
+end
\ No newline at end of file
readme.md
@@ -18,14 +18,19 @@
 * review
 * object references
 * arrays
-* iterators
-* enumerable
 * acceptance testing (watir, selenium, capybara)
 
+# lesson four
+
+* review
+* modules
+* equality (value vs instance)
+
 # todo
 
+* iterators
+* enumerable
 * encapsulation
-* equality (value vs instance)
 * public, private, protected
 * begin,rescue,end
 * hashes
@@ -35,4 +40,4 @@
 * tools (ri, irb)
 * solid
 * tdd
-
+* range