Commit acce6e0

mo khan <mo@mokhan.ca>
2013-11-07 04:10:39
find all movies by pixar.
1 parent 3d1a9f2
lib/movie.js
@@ -4,6 +4,8 @@ var Movie;
 module.exports = Movie = (function() {
   function Movie(attributes) {
     this.title = attributes['title'];
+    this.studio = attributes['studio'];
+    this.year_published = attributes['year_published'];
   }
 
   Movie.prototype.equals = function(other) {
lib/movie_library.js
@@ -1,9 +1,11 @@
 // Generated by CoffeeScript 1.6.3
-var Enumerable, Module, MovieLibrary, moduleKeywords,
+var Enumerable, Module, MovieLibrary, Studio, moduleKeywords,
   __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
   __hasProp = {}.hasOwnProperty,
   __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
 
+Studio = require('./studio');
+
 Enumerable = {
   any: function(predicate) {
     var result,
@@ -25,6 +27,17 @@ Enumerable = {
       return result || (result = item === other_item || item.equals(other_item));
     });
     return result;
+  },
+  find_all: function(predicate) {
+    var results,
+      _this = this;
+    results = [];
+    this.each(function(item) {
+      if (predicate(item)) {
+        return results.push(item);
+      }
+    });
+    return results;
   }
 };
 
@@ -81,6 +94,13 @@ module.exports = MovieLibrary = (function(_super) {
     return _results;
   };
 
+  MovieLibrary.prototype.find_all_movies_by_pixar = function() {
+    var _this = this;
+    return this.find_all(function(movie) {
+      return movie.studio === Studio.Pixar;
+    });
+  };
+
   return MovieLibrary;
 
 })(Module);
src/movie.coffee
@@ -1,6 +1,8 @@
 module.exports = class Movie
   constructor: (attributes) ->
     @title = attributes['title']
+    @studio = attributes['studio']
+    @year_published = attributes['year_published']
 
   equals: (other) ->
     @title == other["title"]
src/movie_library.coffee
@@ -1,3 +1,5 @@
+Studio = require('./studio')
+
 Enumerable =
   any: (predicate) ->
     result = false
@@ -12,6 +14,12 @@ Enumerable =
       result ||= (item == other_item || item.equals(other_item))
     result
 
+  find_all: (predicate) ->
+    results = []
+    @each (item) =>
+      results.push(item) if predicate(item)
+    results
+
 moduleKeywords = ['extended', 'included']
 
 class Module
@@ -38,3 +46,7 @@ module.exports = class MovieLibrary extends Module
     for movie in @movies
       visitor(movie)
 
+  find_all_movies_by_pixar: ->
+    @find_all (movie) =>
+      movie.studio == Studio.Pixar
+
test/movie_library_spec.coffee
@@ -43,4 +43,15 @@ describe "MovieLibrary", ->
 
       result.should.equal(true)
 
+  context "searching for movies", ->
+    beforeEach ->
+      for movie in @all_movies
+        @sut.add(movie)
+
+    it "can find all pixar movies", ->
+      results = @sut.find_all_movies_by_pixar()
+      results.should.include(@toy_story)
+      results.should.include(@up)
+      results.should.include(@cars)
+      results.should.include(@monsters_inc)