Skip to content
Browse files

updated collection find api

  • Loading branch information...
1 parent 3f700fc commit 6d049252067e705868fb28241bf8c0282c6d85f6 @gushov committed Dec 10, 2012
Showing with 72 additions and 22 deletions.
  1. +2 −4 README.md
  2. +23 −8 dist/lilmodel.js
  3. +2 −2 dist/lilmodel.min.js
  4. +6 −6 lib/lilmodel/collection.js
  5. +8 −1 lib/lilmodel/model.js
  6. +1 −1 package.json
  7. +30 −0 test/lilmodel-test.js
View
6 README.md
@@ -84,15 +84,13 @@ dunkel.save(function (err) {
### _collection_.validate()
-### _collection_.add(model)
+### _collection_.add(properties)
### _collection_.remove(query)
### _collection_.get(query)
-### _collection_.each(callback, context)
-
-### _collection_.find(callback, context)
+### _collection_.find(query, callback, context)
## License
Copyright (c) 2012 August Hovland
View
31 dist/lilmodel.js
@@ -1,4 +1,4 @@
-/*! lilmodel - v0.0.5 - 2012-12-09
+/*! lilmodel - v0.0.6 - 2012-12-10
* Copyright (c) 2012 August Hovland <gushov@gmail.com>; Licensed MIT */
(function (ctx) {
@@ -407,6 +407,17 @@ var arr = require('lilobj').arr;
var _ = require('lil_');
var syncr = require('./syncr');
+function parser(ctx, model, next) {
+
+ return function (err, values) {
+
+ var instance = !err && model.create(values);
+ next.call(ctx, err, instance);
+
+ };
+
+}
+
module.exports = arr.extend({
construct: function (values) {
@@ -473,14 +484,11 @@ module.exports = arr.extend({
},
- each: function (next, ctx) {
- _.each(this, next, ctx);
- },
-
- find: function (next, ctx) {
+ find: function (query, next, ctx) {
var sync = syncr();
- sync('find', this, next.bind(ctx));
+ this.query = query;
+ sync('find', this, parser(ctx, this, next));
}
@@ -520,7 +528,10 @@ function setter(name, value) {
function parser(ctx, model, next) {
return function (err, values) {
- next.call(ctx, err, model.create(values || {}));
+
+ var instance = !err ? model.create(values) : null;
+ next.call(ctx, err, instance);
+
};
}
@@ -593,13 +604,17 @@ module.exports = obj.extend({
},
fetch: function (next, ctx) {
+
var sync = syncr();
sync('fetch', this, parser(ctx, this, next));
+
},
destroy: function (next, ctx) {
+
var sync = syncr();
sync('destroy', this, parser(ctx, this, next));
+
}
});
View
4 dist/lilmodel.min.js
@@ -1,3 +1,3 @@
-/*! lilmodel - v0.0.5 - 2012-12-09
+/*! lilmodel - v0.0.6 - 2012-12-10
* Copyright (c) 2012 August Hovland <gushov@gmail.com>; 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/arr",function(e,t,n){var r=e("./obj"),i=e("lil_"),s=Object.create(Array.prototype);i.eachIn(r,function(e,t){s[e]=t}),t.exports=s},!0),provide("lilobj/obj",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("lilobj",function(e,t,n){var r=e("./lilobj/obj"),i=e("./lilobj/arr");t.exports={obj:r,arr:i}},!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").arr,i=e("lil_"),s=e("./syncr");t.exports=r.extend({construct:function(e){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){var t;i.each(this,function(n,r){i.match(n,e)&&(t=r)}),typeof t=="number"&&this.splice(t,1)},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){function u(e){return this.$[e]}function a(e,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)}function f(e,t,n){return function(r,i){n.call(e,r,t.create(i||{}))}}var r=e("lilobj").obj,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:u.bind(this,e),set:a.bind(this,e)}},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,f(t,this,e)):e.call(t,i.error,this)},fetch:function(e,t){var n=o();n("fetch",this,f(t,this,e))},destroy:function(e,t){var n=o();n("destroy",this,f(t,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);
+(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/arr",function(e,t,n){var r=e("./obj"),i=e("lil_"),s=Object.create(Array.prototype);i.eachIn(r,function(e,t){s[e]=t}),t.exports=s},!0),provide("lilobj/obj",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("lilobj",function(e,t,n){var r=e("./lilobj/obj"),i=e("./lilobj/arr");t.exports={obj:r,arr:i}},!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){function o(e,t,n){return function(r,i){var s=!r&&t.create(i);n.call(e,r,s)}}var r=e("lilobj").arr,i=e("lil_"),s=e("./syncr");t.exports=r.extend({construct:function(e){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){var t;i.each(this,function(n,r){i.match(n,e)&&(t=r)}),typeof t=="number"&&this.splice(t,1)},get:function(e){return this.filter(function(t){return i.match(t,e)})},find:function(e,t,n){var r=s();this.query=e,r("find",this,o(n,this,t))}})},!0),provide("lilmodel/model",function(e,t,n){function u(e){return this.$[e]}function a(e,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)}function f(e,t,n){return function(r,i){var s=r?null:t.create(i);n.call(e,r,s)}}var r=e("lilobj").obj,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:u.bind(this,e),set:a.bind(this,e)}},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,f(t,this,e)):e.call(t,i.error,this)},fetch:function(e,t){var n=o();n("fetch",this,f(t,this,e))},destroy:function(e,t){var n=o();n("destroy",this,f(t,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);
View
12 lib/lilmodel/collection.js
@@ -9,7 +9,10 @@ var syncr = require('./syncr');
function parser(ctx, model, next) {
return function (err, values) {
- next.call(ctx, err, model.create(values || []));
+
+ var instance = !err && model.create(values);
+ next.call(ctx, err, instance);
+
};
}
@@ -80,13 +83,10 @@ module.exports = arr.extend({
},
- each: function (next, ctx) {
- _.each(this, next, ctx);
- },
-
- find: function (next, ctx) {
+ find: function (query, next, ctx) {
var sync = syncr();
+ this.query = query;
sync('find', this, parser(ctx, this, next));
}
View
9 lib/lilmodel/model.js
@@ -28,7 +28,10 @@ function setter(name, value) {
function parser(ctx, model, next) {
return function (err, values) {
- next.call(ctx, err, model.create(values));
+
+ var instance = !err ? model.create(values) : null;
+ next.call(ctx, err, instance);
+
};
}
@@ -101,13 +104,17 @@ module.exports = obj.extend({
},
fetch: function (next, ctx) {
+
var sync = syncr();
sync('fetch', this, parser(ctx, this, next));
+
},
destroy: function (next, ctx) {
+
var sync = syncr();
sync('destroy', this, parser(ctx, this, next));
+
}
});
View
2 package.json
@@ -2,7 +2,7 @@
"author": "August Hovland <gushov@gmail.com>",
"name": "lilmodel",
"description": "A li'l model wrapper",
- "version": "0.0.6",
+ "version": "0.0.7",
"repository": {
"type": "git",
"url": "git://github.com/gushov/lilmodel.git"
View
30 test/lilmodel-test.js
@@ -83,6 +83,36 @@ buster.testCase("lilmodel", {
assert.equals(results.length, 1);
assert.equals(results[0], chef.recipes[0]);
+ },
+
+ "should call sync on collection find": function () {
+
+ var syncStub = this.stub().callsArg(2);
+ var nextSpy = this.spy();
+
+ lilModel.syncr(syncStub);
+
+ var Recipe = lilModel.model.extend({
+ rules: {
+ name: ['required', 'string']
+ }
+ });
+
+ var Recipes = lilModel.collection.extend({
+ model: Recipe
+ });
+
+ var recipes = Recipes.create();
+ var context = { me: 'gus' };
+ var query = { name: 'gus' };
+ recipes.find(query, nextSpy, context);
+
+ assert(syncStub.calledOnce);
+ assert(syncStub.calledWith('find', recipes));
+ assert.equals(syncStub.getCall(0).args[1].query, query);
+ assert(nextSpy.calledOnce);
+ assert(nextSpy.calledOn(context));
+
}
});

0 comments on commit 6d04925

Please sign in to comment.
Something went wrong with that request. Please try again.