From 9f1264b8a9e9c70cab43aed5c06e8e5e3e272fda Mon Sep 17 00:00:00 2001 From: mo k Date: Sat, 12 Nov 2011 10:05:39 -0700 Subject: add find_all_matching to library. --- lib/library.rb | 3 +++ test/library_specs.rb | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) 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 -- cgit v1.2.3