From 844823d2c2f792285779f975e44c0bae529b3421 Mon Sep 17 00:00:00 2001 From: Gus Hovland Date: Fri, 7 Dec 2012 08:15:41 +0100 Subject: [PATCH] added each function to collection --- README.md | 10 ++++++---- dist/lilmodel.js | 30 ++++++++++++++++++++---------- dist/lilmodel.min.js | 4 ++-- lib/lilmodel/collection.js | 10 ++++++++-- lib/lilmodel/model.js | 14 +++++++------- package.json | 2 +- test/lilmodel-test.js | 5 ++++- 7 files changed, 48 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2614c37..55cc6b2 100644 --- a/README.md +++ b/README.md @@ -72,11 +72,11 @@ dunkel.save(function (err) { ### _model_.validate() -### _model_.save(callback) +### _model_.save(callback, context) -### _model_.fetch(callback) +### _model_.fetch(callback, context) -### _model_.destroy(callback) +### _model_.destroy(callback, context) ### collection.extend(config) @@ -90,7 +90,9 @@ dunkel.save(function (err) { ### _collection_.get(query) -### _collection_.find(query, callback) +### _collection_.each(callback, context) + +### _collection_.find(callback, context) ## License Copyright (c) 2012 August Hovland diff --git a/dist/lilmodel.js b/dist/lilmodel.js index 7d7badf..3c81a37 100644 --- a/dist/lilmodel.js +++ b/dist/lilmodel.js @@ -1,4 +1,4 @@ -/*! lilmodel - v0.0.2 - 2012-12-06 +/*! lilmodel - v0.0.3 - 2012-12-07 * Copyright (c) 2012 August Hovland ; Licensed MIT */ (function (ctx) { @@ -305,6 +305,10 @@ var validator = { return typeof value === 'string'; }, + boolean: function (value) { + return typeof value === 'boolean'; + }, + length: function (value, min, max) { var isBigEnough = !min || value.length >= min; @@ -427,9 +431,15 @@ module.exports = LilObj.extend({ }, - find: function (query, next) { + each: function (next, ctx) { + _.each(this.$, next, ctx); + }, + + find: function (next, ctx) { + var sync = syncr(); - sync('find', this, next); + sync('find', this, next.bind(ctx)); + } }); @@ -518,28 +528,28 @@ module.exports = LilObj.extend({ }, - save: function (next) { + save: function (next, ctx) { var sync = syncr(); var method = this.$._id ? 'update' : 'create'; var validation = this.validate(); if (validation.isValid) { - sync(method, this, next); + sync(method, this, next.bind(ctx)); } else { - next(validation.error, this); + next.call(ctx, validation.error, this); } }, - fetch: function (next) { + fetch: function (next, ctx) { var sync = syncr(); - sync('fetch', this, next); + sync('fetch', this, next.bind(ctx)); }, - destroy: function (next) { + destroy: function (next, ctx) { var sync = syncr(); - sync('delete', this, next); + sync('delete', this, next.bind(ctx)); } }); diff --git a/dist/lilmodel.min.js b/dist/lilmodel.min.js index 1fa720f..6b3437a 100644 --- a/dist/lilmodel.min.js +++ b/dist/lilmodel.min.js @@ -1,3 +1,3 @@ -/*! lilmodel - v0.0.2 - 2012-12-06 +/*! lilmodel - v0.0.3 - 2012-12-07 * Copyright (c) 2012 August Hovland ; Licensed MIT */ -(function(e){"use strict";function r(e,t){if(t.indexOf(".")===-1)return t;t=t.split("/"),e=e?e.split("/"):[],e.pop(),t[0]==="."&&t.shift();while(t[0]==="..")t.shift(),e.pop();return e.concat(t).join("/")}var t={},n={};e.provide=function(e,r,i){return i?t[e]=r:n[e]=r},e.require=function(i,s){var o,u,a=s||i;return n[a]?n[a]:(o=n[a]={},u={exports:o},t[a](function(t){return e.require(t,r(a,t))},u,o),n[a]=u.exports)}})(this),provide("lil_",function(e,t,n){t.exports={typeOf:function(e){var t=typeof e;return t==="object"&&(t=Array.isArray(e)?"array":t,t=e===null?"null":t),t},each:function(e,t,n){e&&e.length&&e.forEach(t,n)},every:function(e,t,n){return e&&e.length?e.every(t,n):!1},map:function(e,t,n){return e&&e.length?e.map(t,n):[]},eachIn:function(e,t,n){var r=e?Object.keys(e):[];r.forEach(function(r,i){t.call(n,r,e[r],i)})},mapIn:function(e,t,n){var r={};return this.eachIn(e,function(e,n,i){r[e]=t.call(this,e,n,i)},n),r},walk:function(e,t,n,r){var i=this,s=function(e,t){i.eachIn(t,function(t,n){o(e[t],n,t,e)})},o=function(e,t,o,u){var a=i.typeOf(t);a==="object"?(!e&&u&&r&&(e=u[o]={}),s(e,t)):n.call(u,e,t,o)};o(e,t)},extend:function(e,t){return this.walk(e,t,function(e,t,n){this[n]=t},!0),e},defaults:function(e,t){return this.walk(e,t,function(e,t,n){e||(this[n]=t)},!0),e},match:function(e,t){var n=!0;return this.walk(e,t,function(e,t){n=e===t}),n},pick:function(e,t){var n={};return t=this.typeOf(t)==="array"?t:Object.keys(t),this.each(t,function(t){n[t]=e&&e[t]}),n},pushOn:function(e,t,n){e[t]&&typeof e[t].push=="function"?e[t].push(n):typeof e[t]=="undefined"&&(e[t]=[n])}}},!0),provide("lilobj",function(e,t,n){var r=e("lil_");t.exports={isA:function(e){function t(){}return t.prototype=e,this instanceof t},extend:function(e){var t=Object.create(this);return r.eachIn(e,function(e,n){t[e]=n}),t},create:function(){var e=Object.create(this);return e.construct!==undefined&&e.construct.apply(e,arguments),e}}},!0),provide("vladiator",function(e,t,n){var r=e("lil_"),i={required:function(e){return typeof e!="undefined"&&e!==null},array:function(e){return Array.isArray(e)},number:function(e){return typeof e=="number"},string:function(e){return typeof e=="string"},length:function(e,t,n){var r=!t||e.length>=t,i=!n||e.length<=n;return r&&i},gte:function(e,t){return e>=t}},s=function(e,t){var n={isValid:!0};return(e[0]==="required"||i.required(t))&&r.every(e,function(e){var r,s=[];return typeof e!="string"?(r=e[0],s=e.slice(1)):r=e,s.unshift(t),i[r].apply(null,s)?!0:(n.isValid=!1,n.error=r,!1)}),n.$=t,n};t.exports=s},!0),provide("lilmodel/collection",function(e,t,n){var r=e("lilobj"),i=e("lil_"),s=e("./syncr");t.exports=r.extend({construct:function(e){this.$=[],i.each(e,function(e){this.$.push(this.model.create(e))},this),this.validate()},validate:function(){var e={isValid:!0,error:[]};return i.each(this.$,function(t){var n=t.validate();e.error.push(n.error),e.isValid=e.isValid&&n.isValid}),e},add:function(e){var t;e.isA&&e.isA(this.model)?t=e:t=this.model.create(e),this.$.push(t)},remove:function(e){this.$=this.$.filter(function(t){return!i.match(t,e)})},get:function(e){return this.$.filter(function(t){return i.match(t,e)})},find:function(e,t){var n=s();n("find",this,t)}})},!0),provide("lilmodel/model",function(e,t,n){var r=e("lilobj"),i=e("lil_"),s=e("vladiator"),o=e("./syncr");t.exports=r.extend({construct:function(e){this.$={};var t=i.mapIn(this.rules,function(e,t){return{enumerable:!0,get:function(){return this.$[e]},set:function(t){var n=this.children&&this.children[e];n&&n.create&&typeof t=="object"?this.$[e]=n.create(t):n&&typeof t=="object"?this.$[e]=this.create(t):n||(this.$[e]=t)}}},this);Object.defineProperties(this,t),e=i.pick(e,this.rules),i.defaults(e,this.defaults),i.eachIn(e,function(e,t){this[e]=t},this),this.validate()},validate:function(){var e={isValid:!0,error:{}};return i.eachIn(this.rules,function(t,n){var r=this[t],i;r&&r.validate?i=r.validate():(i=s(n,r),r=i.$),e.error[t]=i.error,e.isValid=e.isValid&&i.isValid},this),e},save:function(e){var t=o(),n=this.$._id?"update":"create",r=this.validate();r.isValid?t(n,this,e):e(r.error,this)},fetch:function(e){var t=o();t("fetch",this,e)},destroy:function(e){var t=o();t("delete",this,e)}})},!0),provide("lilmodel/syncr",function(e,t,n){var r=function(e,t,n){n(null,t)};t.exports=function(e){return e&&(r=e),r}},!0),provide("lilmodel",function(e,t,n){var r=e("./lilmodel/syncr"),i=e("./lilmodel/model"),s=e("./lilmodel/collection");t.exports={syncr:r,model:i,collection:s}},!0); \ No newline at end of file +(function(e){"use strict";function r(e,t){if(t.indexOf(".")===-1)return t;t=t.split("/"),e=e?e.split("/"):[],e.pop(),t[0]==="."&&t.shift();while(t[0]==="..")t.shift(),e.pop();return e.concat(t).join("/")}var t={},n={};e.provide=function(e,r,i){return i?t[e]=r:n[e]=r},e.require=function(i,s){var o,u,a=s||i;return n[a]?n[a]:(o=n[a]={},u={exports:o},t[a](function(t){return e.require(t,r(a,t))},u,o),n[a]=u.exports)}})(this),provide("lil_",function(e,t,n){t.exports={typeOf:function(e){var t=typeof e;return t==="object"&&(t=Array.isArray(e)?"array":t,t=e===null?"null":t),t},each:function(e,t,n){e&&e.length&&e.forEach(t,n)},every:function(e,t,n){return e&&e.length?e.every(t,n):!1},map:function(e,t,n){return e&&e.length?e.map(t,n):[]},eachIn:function(e,t,n){var r=e?Object.keys(e):[];r.forEach(function(r,i){t.call(n,r,e[r],i)})},mapIn:function(e,t,n){var r={};return this.eachIn(e,function(e,n,i){r[e]=t.call(this,e,n,i)},n),r},walk:function(e,t,n,r){var i=this,s=function(e,t){i.eachIn(t,function(t,n){o(e[t],n,t,e)})},o=function(e,t,o,u){var a=i.typeOf(t);a==="object"?(!e&&u&&r&&(e=u[o]={}),s(e,t)):n.call(u,e,t,o)};o(e,t)},extend:function(e,t){return this.walk(e,t,function(e,t,n){this[n]=t},!0),e},defaults:function(e,t){return this.walk(e,t,function(e,t,n){e||(this[n]=t)},!0),e},match:function(e,t){var n=!0;return this.walk(e,t,function(e,t){n=e===t}),n},pick:function(e,t){var n={};return t=this.typeOf(t)==="array"?t:Object.keys(t),this.each(t,function(t){n[t]=e&&e[t]}),n},pushOn:function(e,t,n){e[t]&&typeof e[t].push=="function"?e[t].push(n):typeof e[t]=="undefined"&&(e[t]=[n])}}},!0),provide("lilobj",function(e,t,n){var r=e("lil_");t.exports={isA:function(e){function t(){}return t.prototype=e,this instanceof t},extend:function(e){var t=Object.create(this);return r.eachIn(e,function(e,n){t[e]=n}),t},create:function(){var e=Object.create(this);return e.construct!==undefined&&e.construct.apply(e,arguments),e}}},!0),provide("vladiator",function(e,t,n){var r=e("lil_"),i={required:function(e){return typeof e!="undefined"&&e!==null},array:function(e){return Array.isArray(e)},number:function(e){return typeof e=="number"},string:function(e){return typeof e=="string"},"boolean":function(e){return typeof e=="boolean"},length:function(e,t,n){var r=!t||e.length>=t,i=!n||e.length<=n;return r&&i},gte:function(e,t){return e>=t}},s=function(e,t){var n={isValid:!0};return(e[0]==="required"||i.required(t))&&r.every(e,function(e){var r,s=[];return typeof e!="string"?(r=e[0],s=e.slice(1)):r=e,s.unshift(t),i[r].apply(null,s)?!0:(n.isValid=!1,n.error=r,!1)}),n.$=t,n};t.exports=s},!0),provide("lilmodel/collection",function(e,t,n){var r=e("lilobj"),i=e("lil_"),s=e("./syncr");t.exports=r.extend({construct:function(e){this.$=[],i.each(e,function(e){this.$.push(this.model.create(e))},this),this.validate()},validate:function(){var e={isValid:!0,error:[]};return i.each(this.$,function(t){var n=t.validate();e.error.push(n.error),e.isValid=e.isValid&&n.isValid}),e},add:function(e){var t;e.isA&&e.isA(this.model)?t=e:t=this.model.create(e),this.$.push(t)},remove:function(e){this.$=this.$.filter(function(t){return!i.match(t,e)})},get:function(e){return this.$.filter(function(t){return i.match(t,e)})},each:function(e,t){i.each(this.$,e,t)},find:function(e,t){var n=s();n("find",this,e.bind(t))}})},!0),provide("lilmodel/model",function(e,t,n){var r=e("lilobj"),i=e("lil_"),s=e("vladiator"),o=e("./syncr");t.exports=r.extend({construct:function(e){this.$={};var t=i.mapIn(this.rules,function(e,t){return{enumerable:!0,get:function(){return this.$[e]},set:function(t){var n=this.children&&this.children[e];n&&n.create&&typeof t=="object"?this.$[e]=n.create(t):n&&typeof t=="object"?this.$[e]=this.create(t):n||(this.$[e]=t)}}},this);Object.defineProperties(this,t),e=i.pick(e,this.rules),i.defaults(e,this.defaults),i.eachIn(e,function(e,t){this[e]=t},this),this.validate()},validate:function(){var e={isValid:!0,error:{}};return i.eachIn(this.rules,function(t,n){var r=this[t],i;r&&r.validate?i=r.validate():(i=s(n,r),r=i.$),e.error[t]=i.error,e.isValid=e.isValid&&i.isValid},this),e},save:function(e,t){var n=o(),r=this.$._id?"update":"create",i=this.validate();i.isValid?n(r,this,e.bind(t)):e.call(t,i.error,this)},fetch:function(e,t){var n=o();n("fetch",this,e.bind(t))},destroy:function(e,t){var n=o();n("delete",this,e.bind(t))}})},!0),provide("lilmodel/syncr",function(e,t,n){var r=function(e,t,n){n(null,t)};t.exports=function(e){return e&&(r=e),r}},!0),provide("lilmodel",function(e,t,n){var r=e("./lilmodel/syncr"),i=e("./lilmodel/model"),s=e("./lilmodel/collection");t.exports={syncr:r,model:i,collection:s}},!0); \ No newline at end of file diff --git a/lib/lilmodel/collection.js b/lib/lilmodel/collection.js index 8b0de7f..2b3af80 100644 --- a/lib/lilmodel/collection.js +++ b/lib/lilmodel/collection.js @@ -64,9 +64,15 @@ module.exports = LilObj.extend({ }, - find: function (query, next) { + each: function (next, ctx) { + _.each(this.$, next, ctx); + }, + + find: function (next, ctx) { + var sync = syncr(); - sync('find', this, next); + sync('find', this, next.bind(ctx)); + } }); diff --git a/lib/lilmodel/model.js b/lib/lilmodel/model.js index d439a3c..448dae8 100644 --- a/lib/lilmodel/model.js +++ b/lib/lilmodel/model.js @@ -78,28 +78,28 @@ module.exports = LilObj.extend({ }, - save: function (next) { + save: function (next, ctx) { var sync = syncr(); var method = this.$._id ? 'update' : 'create'; var validation = this.validate(); if (validation.isValid) { - sync(method, this, next); + sync(method, this, next.bind(ctx)); } else { - next(validation.error, this); + next.call(ctx, validation.error, this); } }, - fetch: function (next) { + fetch: function (next, ctx) { var sync = syncr(); - sync('fetch', this, next); + sync('fetch', this, next.bind(ctx)); }, - destroy: function (next) { + destroy: function (next, ctx) { var sync = syncr(); - sync('delete', this, next); + sync('delete', this, next.bind(ctx)); } }); diff --git a/package.json b/package.json index 3303405..9d4ef9b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "August Hovland ", "name": "lilmodel", "description": "A li'l model wrapper", - "version": "0.0.3", + "version": "0.0.4", "repository": { "type": "git", "url": "git://github.com/gushov/lilmodel.git" diff --git a/test/lilmodel-test.js b/test/lilmodel-test.js index f7c66d0..58b2239 100644 --- a/test/lilmodel-test.js +++ b/test/lilmodel-test.js @@ -69,9 +69,12 @@ buster.testCase("lilmodel", { var results = chef.recipes.get({ name: 'tacos' }); chef.recipes.remove({ name: 'meatball sauce' }); - chef.save(nextSpy); + + var context = { me: 'gus' }; + chef.save(nextSpy, context); assert.calledOnce(this.syncStub); assert.calledOnce(nextSpy); + assert(nextSpy.calledOn(context)); assert.equals(chef.name, 'gus'); assert.equals(chef.sousChef.name, 'zoe'); assert.equals(chef.recipes.$[0].name, 'tacos');