Commit 3d1a9f2

mo khan <mo@mokhan.ca>
2013-11-07 03:59:08
move includes into Enumerable module.
1 parent 7ed1cc2
lib/movie.js
@@ -6,6 +6,10 @@ module.exports = Movie = (function() {
     this.title = attributes['title'];
   }
 
+  Movie.prototype.equals = function(other) {
+    return this.title === other["title"];
+  };
+
   return Movie;
 
 })();
lib/movie_library.js
@@ -16,6 +16,15 @@ Enumerable = {
       }
     });
     return result;
+  },
+  includes: function(other_item) {
+    var result,
+      _this = this;
+    result = false;
+    this.each(function(item) {
+      return result || (result = item === other_item || item.equals(other_item));
+    });
+    return result;
   }
 };
 
@@ -24,20 +33,6 @@ moduleKeywords = ['extended', 'included'];
 Module = (function() {
   function Module() {}
 
-  Module.extend = function(object) {
-    var key, value, _ref;
-    for (key in object) {
-      value = object[key];
-      if (__indexOf.call(moduleKeywords, key) < 0) {
-        this[key] = value;
-      }
-    }
-    if ((_ref = object.extended) != null) {
-      _ref.apply(this);
-    }
-    return this;
-  };
-
   Module.include = function(object) {
     var key, value, _ref;
     for (key in object) {
@@ -66,25 +61,15 @@ module.exports = MovieLibrary = (function(_super) {
   }
 
   MovieLibrary.prototype.add = function(movie) {
-    if (!this.contains(movie)) {
+    if (!this.includes(movie)) {
       return this.movies.push(movie);
     }
   };
 
-  MovieLibrary.prototype.total_count = function() {
+  MovieLibrary.prototype.count = function() {
     return this.movies.length;
   };
 
-  MovieLibrary.prototype.contains = function(other_movie) {
-    var result,
-      _this = this;
-    result = false;
-    this.each(function(movie) {
-      return result || (result = movie.title === other_movie.title);
-    });
-    return result;
-  };
-
   MovieLibrary.prototype.each = function(visitor) {
     var movie, _i, _len, _ref, _results;
     _ref = this.movies;
src/movie.coffee
@@ -1,3 +1,6 @@
 module.exports = class Movie
   constructor: (attributes) ->
     @title = attributes['title']
+
+  equals: (other) ->
+    @title == other["title"]
src/movie_library.coffee
@@ -1,4 +1,4 @@
-Enumerable = 
+Enumerable =
   any: (predicate) ->
     result = false
     @each (item) =>
@@ -6,16 +6,15 @@ Enumerable =
       return if result
     result
 
+  includes: (other_item) ->
+    result = false
+    @each (item) =>
+      result ||= (item == other_item || item.equals(other_item))
+    result
+
 moduleKeywords = ['extended', 'included']
 
 class Module
-  @extend: (object) ->
-    for key, value of object when key not in moduleKeywords
-      @[key] = value
-
-    object.extended?.apply(@)
-    this
-
   @include: (object) ->
     for key, value of object when key not in moduleKeywords
       @::[key] = value
@@ -25,21 +24,16 @@ class Module
 
 module.exports = class MovieLibrary extends Module
   @include Enumerable
+
   constructor: ->
     @movies = []
 
   add: (movie) ->
-    @movies.push(movie) unless @contains(movie)
+    @movies.push(movie) unless @includes(movie)
 
-  total_count: ->
+  count: ->
     @movies.length
 
-  contains: (other_movie) ->
-    result = false
-    @each (movie) =>
-      result ||= (movie.title == other_movie.title)
-    result
-
   each: (visitor) ->
     for movie in @movies
       visitor(movie)
test/movie_library_spec.coffee
@@ -22,17 +22,17 @@ describe "MovieLibrary", ->
     it "increases the total number of movies in the library", ->
       @sut.add(@shawshank_redemption)
       @sut.add(@chasing_amy)
-      @sut.total_count().should.equal(2)
+      @sut.count().should.equal(2)
 
     it "does not allow duplicate movies into the library", ->
       @sut.add(@man_on_fire)
       @sut.add(@man_on_fire)
-      @sut.total_count().should.equal(1)
+      @sut.count().should.equal(1)
 
     it "does not add two movies that have the same title (logically the same)", ->
       @sut.add(new Movie(title: 'old school'))
       @sut.add(new Movie(title: 'old school'))
-      @sut.total_count().should.equal(1)
+      @sut.count().should.equal(1)
 
   context "any?", ->
     it "returns true when something matches", ->