From eac28ff73ae2ef2738a058d60a2e4d35a610db60 Mon Sep 17 00:00:00 2001 From: mo khan Date: Sat, 9 Nov 2013 08:28:02 -0700 Subject: extract method to search using specification. --- src/movie.coffee | 11 +++++++++++ src/movie_library.coffee | 8 ++++---- test/movie_library_spec.coffee | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/movie.coffee b/src/movie.coffee index bf2e77d..9d22e85 100644 --- a/src/movie.coffee +++ b/src/movie.coffee @@ -6,3 +6,14 @@ module.exports = class Movie equals: (other) -> @title == other["title"] + + @by: (studio) -> + console.log(studio) + new StudioSpecification(studio) + +class StudioSpecification + constructor: (studio) -> + @studio = studio + + matches: (movie) -> + @studio == movie.studio diff --git a/src/movie_library.coffee b/src/movie_library.coffee index 69af878..5b77972 100644 --- a/src/movie_library.coffee +++ b/src/movie_library.coffee @@ -20,6 +20,10 @@ Enumerable = results.push(item) if predicate(item) results + all: (specification) -> + @find_all (movie) -> + specification.matches(movie) + moduleKeywords = ['extended', 'included'] class Module @@ -46,10 +50,6 @@ module.exports = class MovieLibrary extends Module for movie in @movies visitor(movie) - find_all_movies_by_pixar: -> - @find_all (movie) => - movie.studio == Studio.Pixar - find_movies_by_pixar_or_disney: -> @find_all (movie) => movie.studio == Studio.Pixar || movie.studio == Studio.Disney diff --git a/test/movie_library_spec.coffee b/test/movie_library_spec.coffee index 9f6ddcc..8e9b0f6 100644 --- a/test/movie_library_spec.coffee +++ b/test/movie_library_spec.coffee @@ -49,7 +49,7 @@ describe "MovieLibrary", -> @sut.add(movie) it "can find all pixar movies", -> - results = @sut.find_all_movies_by_pixar() + results = @sut.all(Movie.by(Studio.Pixar)) results.should.include(@toy_story) results.should.include(@up) results.should.include(@cars) -- cgit v1.2.3