diff options
| author | mo khan <mo@mokhan.ca> | 2013-11-09 09:05:20 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-11-09 09:05:20 -0700 |
| commit | f0a21e176dcdd90c33ebf18aeb6b8c2f07e4bcba (patch) | |
| tree | 5d4741c4d462160bc3c533152ce605b38571ff8e | |
| parent | 86ad7b779e64880bde62bf51ea72c6afa528ab33 (diff) | |
create or specification to find movies by multiple studio.
| -rw-r--r-- | src/movie.coffee | 13 | ||||
| -rw-r--r-- | src/movie_library.coffee | 4 | ||||
| -rw-r--r-- | test/movie_library_spec.coffee | 2 |
3 files changed, 13 insertions, 6 deletions
diff --git a/src/movie.coffee b/src/movie.coffee index 9fe2348..e00a4f9 100644 --- a/src/movie.coffee +++ b/src/movie.coffee @@ -10,12 +10,23 @@ module.exports = class Movie @where: (condition) -> new WhereSpecification(condition) +class OrSpecification + constructor: (left, right) -> + @left = left + @right = right + + matches: (item) -> + @left.matches(item) || @right.matches(item) + class WhereSpecification constructor: (condition) -> @condition = condition matches: (item) -> for key in Object.keys(@condition) - item[key] == @condition[key] + return false if item[key] != @condition[key] + return true + or: (other_specification) -> + new OrSpecification(this, other_specification) diff --git a/src/movie_library.coffee b/src/movie_library.coffee index 5b77972..ed50aec 100644 --- a/src/movie_library.coffee +++ b/src/movie_library.coffee @@ -50,10 +50,6 @@ module.exports = class MovieLibrary extends Module for movie in @movies visitor(movie) - find_movies_by_pixar_or_disney: -> - @find_all (movie) => - movie.studio == Studio.Pixar || movie.studio == Studio.Disney - find_movies_not_published_by_pixar: -> @find_all (movie) => movie.studio != Studio.Pixar diff --git a/test/movie_library_spec.coffee b/test/movie_library_spec.coffee index 284f129..0f97edf 100644 --- a/test/movie_library_spec.coffee +++ b/test/movie_library_spec.coffee @@ -57,7 +57,7 @@ describe "MovieLibrary", -> it 'finds all movies published by pixar or disney', -> - results = @sut.find_movies_by_pixar_or_disney() + results = @sut.all(Movie.where(studio: Studio.Pixar).or(Movie.where(studio: Studio.Disney))) results.length.should.equal(7) results.should.include(@toy_story) results.should.include(@up) |
