Permalink
Browse files

added add get and remove to collection

  • Loading branch information...
1 parent 49e6abd commit 0a6fe5605535c7a93d37fea70d3f5380454dfada @gushov committed Dec 6, 2012
Showing with 144 additions and 71 deletions.
  1. +7 −1 README.md
  2. +96 −25 dist/lilmodel.js
  3. +2 −2 dist/lilmodel.min.js
  4. +29 −1 lib/lilmodel/collection.js
  5. +2 −2 package.json
  6. +8 −40 test/lilmodel-test.js
View
@@ -84,7 +84,13 @@ dunkel.save(function (err) {
### _collection_.validate()
-### _collection_.find(callback)
+### _collection_.add(model)
+
+### _collection_.remove(query)
+
+### _collection_.get(query)
+
+### _collection_.find(query, callback)
## License
Copyright (c) 2012 August Hovland
View
@@ -1,4 +1,4 @@
-/*! lilmodel - v0.0.0 - 2012-11-15
+/*! lilmodel - v0.0.2 - 2012-12-06
* Copyright (c) 2012 August Hovland <gushov@gmail.com>; Licensed MIT */
(function (ctx) {
@@ -135,41 +135,76 @@ module.exports = {
},
- extend: function (obj, src) {
+ walk: function (target, source, func, fill) {
+
+ var self = this;
+
+ var walkObj = function (target, source) {
+
+ self.eachIn(source, function (name, obj) {
+ step(target[name], obj, name, target);
+ });
+
+ };
+
+ var step = function (target, source, name, parent) {
+
+ var type = self.typeOf(source);
- this.eachIn(src, function (name, value) {
-
- var type = this.typeOf(value);
-
- switch (type) {
- case 'object':
- obj[name] = obj[name] || {};
- this.extend(obj[name] || {}, value);
- break;
- case 'boolean':
- obj[name] = obj[name] && value;
- break;
- default:
- obj[name] = value;
- break;
+ if (type === 'object') {
+
+ if (!target && parent && fill) {
+ target = parent[name] = {};
+ }
+
+ walkObj(target, source);
+
+ } else {
+ func.call(parent, target, source, name);
}
- return obj;
+ };
- }, this);
+ step(target, source);
+
+ },
+
+ extend: function (obj, src) {
+
+ this.walk(obj, src, function (target, src, name) {
+ this[name] = src;
+ }, true);
+
+ return obj;
},
defaults: function (obj, defaults) {
- this.eachIn(defaults, function (name, value) {
- if (!obj[name]) { obj[name] = value; }
- });
+ this.walk(obj, defaults, function (target, src, name) {
+
+ if (!target) {
+ this[name] = src;
+ }
+
+ }, true);
return obj;
},
+ match: function (obj, test) {
+
+ var isMatch = true;
+
+ this.walk(obj, test, function (target, src) {
+ isMatch = (target === src);
+ });
+
+ return isMatch;
+
+ },
+
pick: function(obj, keys) {
var picked = {};
@@ -207,6 +242,14 @@ var _ = require('lil_');
module.exports = {
+ isA: function (prototype) {
+
+ function D() {}
+ D.prototype = prototype;
+ return this instanceof D;
+
+ },
+
extend: function (props) {
var result = Object.create(this);
@@ -270,8 +313,8 @@ var validator = {
},
- gte: function (name, value, min) {
- return value < min;
+ gte: function (value, min) {
+ return value >= min;
}
};
@@ -356,7 +399,35 @@ module.exports = LilObj.extend({
},
- find: function (next) {
+ add: function (obj) {
+
+ var model;
+ if (obj.isA && obj.isA(this.model)) {
+ model = obj;
+ } else {
+ model = this.model.create(obj);
+ }
+
+ this.$.push(model);
+ },
+
+ remove: function (query) {
+
+ this.$ = this.$.filter(function (model) {
+ return !_.match(model, query);
+ });
+
+ },
+
+ get: function (query) {
+
+ return this.$.filter(function (model) {
+ return _.match(model, query);
+ });
+
+ },
+
+ find: function (query, next) {
var sync = syncr();
sync('find', this, next);
}
Oops, something went wrong.
@@ -36,7 +36,35 @@ module.exports = LilObj.extend({
},
- find: function (next) {
+ add: function (obj) {
+
+ var model;
+ if (obj.isA && obj.isA(this.model)) {
+ model = obj;
+ } else {
+ model = this.model.create(obj);
+ }
+
+ this.$.push(model);
+ },
+
+ remove: function (query) {
+
+ this.$ = this.$.filter(function (model) {
+ return !_.match(model, query);
+ });
+
+ },
+
+ get: function (query) {
+
+ return this.$.filter(function (model) {
+ return _.match(model, query);
+ });
+
+ },
+
+ find: function (query, next) {
var sync = syncr();
sync('find', this, next);
}
View
@@ -2,13 +2,13 @@
"author": "August Hovland <gushov@gmail.com>",
"name": "lilmodel",
"description": "A li'l model wrapper",
- "version": "0.0.1",
+ "version": "0.0.3",
"repository": {
"type": "git",
"url": "git://github.com/gushov/lilmodel.git"
},
"engines": {
- "node": "~0.6.18"
+ "node": ">= 0.8.x"
},
"dependencies": {
"lil_": ">= 0.0.x",
View
@@ -65,53 +65,21 @@ buster.testCase("lilmodel", {
]
});
+ chef.recipes.add({ name: 'eggs' });
+ var results = chef.recipes.get({ name: 'tacos' });
+ chef.recipes.remove({ name: 'meatball sauce' });
+
chef.save(nextSpy);
assert.calledOnce(this.syncStub);
assert.calledOnce(nextSpy);
assert.equals(chef.name, 'gus');
assert.equals(chef.sousChef.name, 'zoe');
assert.equals(chef.recipes.$[0].name, 'tacos');
+ assert.equals(chef.recipes.$[1].name, 'eggs');
+ assert.equals(chef.recipes.$.length, 2);
+ assert.equals(results.length, 1);
+ assert.equals(results[0], chef.recipes.$[0]);
}
});
-
-//var vlad = require('vladiator');
-
-// var Chef = LilModel.extend({
-
-// defaults: {
-// urlRoot: '/chef'
-// },
-
-// validate: vlad({
-// _id: ['string'],
-// name: ['required', 'string', ['length', 0, 30]],
-// age: ['number', ['gte' , 1]],
-// assistant: ['chef'],
-// recipes: ['array']
-// }),
-
-// assistant: function () {
-// return Chef.create(this.$.assistant);
-// }
-
-// });
-
-
-// module.exports = Chef;
-
-
-// var chefGus = Chef.create({
-// name: 'gus',
-// age: 40
-// });
-
-// chefGus.save(function (err, gus) {
-
-// });
-
-
-// Chef.create({ _id: 'dsfa32234' }).fetch(function (err, zoe) {
-
-// });

0 comments on commit 0a6fe56

Please sign in to comment.