Commit 240b84b

mo khan <mo@mokhan.ca>
2013-11-07 03:35:10
do not allow movies with the same name to be added to the library.
1 parent bd7341a
lib/movie_library.js
@@ -1,6 +1,5 @@
 // Generated by CoffeeScript 1.6.3
-var MovieLibrary,
-  __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; };
+var MovieLibrary;
 
 module.exports = MovieLibrary = (function() {
   function MovieLibrary() {
@@ -8,7 +7,7 @@ module.exports = MovieLibrary = (function() {
   }
 
   MovieLibrary.prototype.add = function(movie) {
-    if (__indexOf.call(this.movies, movie) < 0) {
+    if (!this.contains(movie)) {
       return this.movies.push(movie);
     }
   };
@@ -17,6 +16,27 @@ module.exports = MovieLibrary = (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;
+    _results = [];
+    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+      movie = _ref[_i];
+      _results.push(visitor(movie));
+    }
+    return _results;
+  };
+
   return MovieLibrary;
 
 })();
src/movie_library.coffee
@@ -3,7 +3,18 @@ module.exports = class MovieLibrary
     @movies = []
 
   add: (movie) ->
-    @movies.push(movie) unless movie in @movies
+    @movies.push(movie) unless @contains(movie)
 
   total_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
@@ -29,7 +29,7 @@ describe "MovieLibrary", ->
       @sut.add(@man_on_fire)
       @sut.total_count().should.equal(1)
 
-    xit "does not add two movies that have the same title (logically the same)", ->
+    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)