diff options
| author | mo k <m@mokhan.ca> | 2011-11-12 10:05:39 -0700 |
|---|---|---|
| committer | mo k <m@mokhan.ca> | 2011-11-12 10:05:39 -0700 |
| commit | 9f1264b8a9e9c70cab43aed5c06e8e5e3e272fda (patch) | |
| tree | 7f20a21dbf98fca4f8533c82f9a6e4f45d99fd6b | |
| parent | 053247ee4bf2131be768ac87a01644435b2a263f (diff) | |
add find_all_matching to library.
| -rw-r--r-- | lib/library.rb | 3 | ||||
| -rw-r--r-- | test/library_specs.rb | 12 |
2 files changed, 14 insertions, 1 deletions
diff --git a/lib/library.rb b/lib/library.rb index 4f1321a..f3765b5 100644 --- a/lib/library.rb +++ b/lib/library.rb @@ -8,4 +8,7 @@ class Library def contains(title) @books.find { |book| book.is_titled?(title) } != nil end + def find_all_matching(criteria) + @books.find_all {|book| criteria.is_satisfied_by book } + end end diff --git a/test/library_specs.rb b/test/library_specs.rb index f2972c1..ef68e4c 100644 --- a/test/library_specs.rb +++ b/test/library_specs.rb @@ -20,6 +20,16 @@ class TestLibrary < Test::Unit::TestCase assert_equal(false, @library.contains("little red riding hood")) end def test_should_be_able_to_find_all_books_by_an_author - + author = "dave thomas" + matches = @library.find_all_matching FindAllBooksByAuthor.new(author) + assert_includes(matches, @pickaxe) + end +end +class FindAllBooksByAuthor + def initialize(author) + @author = author + end + def is_satisfied_by(book) + book.author == @author end end |
