Commit 7ed1cc2

mo khan <mo@mokhan.ca>
2013-11-07 03:53:22
implement any using a module.
1 parent 240b84b
lib/movie_library.js
@@ -1,7 +1,66 @@
 // Generated by CoffeeScript 1.6.3
-var MovieLibrary;
+var Enumerable, Module, MovieLibrary, 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; };
+
+Enumerable = {
+  any: function(predicate) {
+    var result,
+      _this = this;
+    result = false;
+    this.each(function(item) {
+      result = predicate(item);
+      if (result) {
+
+      }
+    });
+    return result;
+  }
+};
+
+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) {
+      value = object[key];
+      if (__indexOf.call(moduleKeywords, key) < 0) {
+        this.prototype[key] = value;
+      }
+    }
+    if ((_ref = object.included) != null) {
+      _ref.apply(this);
+    }
+    return this;
+  };
+
+  return Module;
+
+})();
+
+module.exports = MovieLibrary = (function(_super) {
+  __extends(MovieLibrary, _super);
+
+  MovieLibrary.include(Enumerable);
 
-module.exports = MovieLibrary = (function() {
   function MovieLibrary() {
     this.movies = [];
   }
@@ -39,4 +98,4 @@ module.exports = MovieLibrary = (function() {
 
   return MovieLibrary;
 
-})();
+})(Module);
src/movie_library.coffee
@@ -1,4 +1,30 @@
-module.exports = class MovieLibrary
+Enumerable = 
+  any: (predicate) ->
+    result = false
+    @each (item) =>
+      result = predicate(item)
+      return if result
+    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
+
+    object.included?.apply(@)
+    this
+
+module.exports = class MovieLibrary extends Module
+  @include Enumerable
   constructor: ->
     @movies = []
 
test/movie_library_spec.coffee
@@ -34,3 +34,13 @@ describe "MovieLibrary", ->
       @sut.add(new Movie(title: 'old school'))
       @sut.total_count().should.equal(1)
 
+  context "any?", ->
+    it "returns true when something matches", ->
+      @sut.add(new Movie(title: 'old school'))
+      @sut.add(new Movie(title: 'old school'))
+      result = @sut.any (movie) ->
+        movie.title == 'old school'
+
+      result.should.equal(true)
+
+