Permalink
Browse files

added serialize function

  • Loading branch information...
1 parent ed354f7 commit 6e0fa6e72e518d66b7fa40ed4050d74efac45cef @gushov committed Feb 5, 2013
Showing with 65 additions and 4 deletions.
  1. +4 −0 README.md
  2. +23 −1 dist/lilmodel.js
  3. +2 −2 dist/lilmodel.min.js
  4. +8 −0 lib/lilmodel/collection.js
  5. +14 −0 lib/lilmodel/model.js
  6. +1 −1 package.json
  7. +13 −0 test/lilmodel-test.js
View
4 README.md
@@ -78,6 +78,8 @@ dunkel.save(function (err) {
### _model_.destroy(callback, context)
+### _model_.serialize()
+
### collection.extend(config)
### collection.create(propertiesList)
@@ -92,6 +94,8 @@ dunkel.save(function (err) {
### _collection_.find(query, callback, context)
+### _collection_.serialize()
+
## License
Copyright (c) 2012 August Hovland
Licensed under the MIT license.
View
24 dist/lilmodel.js
@@ -1,4 +1,4 @@
-/*! lilmodel - v0.0.9 - 2013-01-25
+/*! lilmodel - v0.0.10 - 2013-02-05
* Copyright (c) 2013 August Hovland <gushov@gmail.com>; Licensed MIT */
(function (ctx) {
@@ -589,6 +589,14 @@ module.exports = arr.extend({
this.query = query;
sync('find', this, parser(ctx, this, next));
+ },
+
+ serialize: function () {
+
+ return _.map(this, function (elem) {
+ return elem.serialize();
+ });
+
}
});
@@ -714,6 +722,20 @@ module.exports = obj.extend({
var sync = syncr();
sync('destroy', this, parser(ctx, this, next));
+ },
+
+ serialize: function () {
+
+ return _.map(this.$, function (name, value) {
+
+ if (this.children && this.children[name]) {
+ return value.serialize();
+ } else {
+ return value;
+ }
+
+ }, this);
+
}
});
View
4 dist/lilmodel.min.js
@@ -1,3 +1,3 @@
-/*! lilmodel - v0.0.9 - 2013-01-25
+/*! lilmodel - v0.0.10 - 2013-02-05
* Copyright (c) 2013 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){var r=this.typeOf(e),i;r==="array"&&e.length?e.forEach(t,n):r==="object"&&(i=e?Object.keys(e):[],i.forEach(function(r,i){t.call(n,r,e[r],i)}))},every:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.every(t,n):r==="object"?(i=e?Object.keys(e):[],i.every(function(r,i){return t.call(n,r,e[r],i)})):!1},some:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.some(t,n):r==="object"?(i=e?Object.keys(e):[],i.some(function(r,i){return t.call(n,r,e[r],i)})):!1},map:function(e,t,n){var r=this.typeOf(e),i=[];return r==="array"&&e.length?e.map(t,n):(r==="object"&&(i={},this.each(e,function(e,n,r){i[e]=t.call(this,e,n,r)},n)),i)},withOut:function(e,t){var n=[];return this.each(e,function(e){e!==t&&n.push(e)}),n},walk:function(e,t,n,r){var i=this,s=function(e,t){i.each(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(){var e=Array.prototype.slice.call(arguments),t=e.shift();return this.each(e,function(e){this.each(e,function(e,n){t[e]=n})},this),t},defaults:function(e,t){return this.each(t,function(t,n){var r=this.typeOf(e[t]);if(r==="undefined"||r==="null")e[t]=n},this),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}}},!0),provide("lilobj/arr",function(e,t,n){function i(){var e=[];return e.push.apply(e,arguments),e.__proto__=i.prototype,e}var r=e("lil_");i.prototype=[],i.prototype.isA=function(e){function t(){}return t.prototype=e,this instanceof t},i.prototype.extend=function(e){i.prototype=this;var t=new i;return r.each(e,function(n){t[n]=e[n]}),t},i.prototype.create=function(){i.prototype=this;var e=new i;return e.construct!==undefined&&e.construct.apply(e,arguments),e},t.exports=new i},!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.each(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"},object:function(e){return typeof e=="object"},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.map(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.each(e,function(e,t){this[e]=t},this),this.validate()},validate:function(){var e={isValid:!0,error:{}};return i.each(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){var r=this.typeOf(e),i;r==="array"&&e.length?e.forEach(t,n):r==="object"&&(i=e?Object.keys(e):[],i.forEach(function(r,i){t.call(n,r,e[r],i)}))},every:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.every(t,n):r==="object"?(i=e?Object.keys(e):[],i.every(function(r,i){return t.call(n,r,e[r],i)})):!1},some:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.some(t,n):r==="object"?(i=e?Object.keys(e):[],i.some(function(r,i){return t.call(n,r,e[r],i)})):!1},map:function(e,t,n){var r=this.typeOf(e),i=[];return r==="array"&&e.length?e.map(t,n):(r==="object"&&(i={},this.each(e,function(e,n,r){i[e]=t.call(this,e,n,r)},n)),i)},withOut:function(e,t){var n=[];return this.each(e,function(e){e!==t&&n.push(e)}),n},walk:function(e,t,n,r){var i=this,s=function(e,t){i.each(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(){var e=Array.prototype.slice.call(arguments),t=e.shift();return this.each(e,function(e){this.each(e,function(e,n){t[e]=n})},this),t},defaults:function(e,t){return this.each(t,function(t,n){var r=this.typeOf(e[t]);if(r==="undefined"||r==="null")e[t]=n},this),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}}},!0),provide("lilobj/arr",function(e,t,n){function i(){var e=[];return e.push.apply(e,arguments),e.__proto__=i.prototype,e}var r=e("lil_");i.prototype=[],i.prototype.isA=function(e){function t(){}return t.prototype=e,this instanceof t},i.prototype.extend=function(e){i.prototype=this;var t=new i;return r.each(e,function(n){t[n]=e[n]}),t},i.prototype.create=function(){i.prototype=this;var e=new i;return e.construct!==undefined&&e.construct.apply(e,arguments),e},t.exports=new i},!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.each(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"},object:function(e){return typeof e=="object"},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))},serialize:function(){return i.map(this,function(e){return e.serialize()})}})},!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.map(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.each(e,function(e,t){this[e]=t},this),this.validate()},validate:function(){var e={isValid:!0,error:{}};return i.each(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))},serialize:function(){return i.map(this.$,function(e,t){return this.children&&this.children[e]?t.serialize():t},this)}})},!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
8 lib/lilmodel/collection.js
@@ -89,6 +89,14 @@ module.exports = arr.extend({
this.query = query;
sync('find', this, parser(ctx, this, next));
+ },
+
+ serialize: function () {
+
+ return _.map(this, function (elem) {
+ return elem.serialize();
+ });
+
}
});
View
14 lib/lilmodel/model.js
@@ -115,6 +115,20 @@ module.exports = obj.extend({
var sync = syncr();
sync('destroy', this, parser(ctx, this, next));
+ },
+
+ serialize: function () {
+
+ return _.map(this.$, function (name, value) {
+
+ if (this.children && this.children[name]) {
+ return value.serialize();
+ } else {
+ return value;
+ }
+
+ }, this);
+
}
});
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.10",
+ "version": "0.0.11",
"repository": {
"type": "git",
"url": "git://github.com/gushov/lilmodel.git"
View
13 test/lilmodel-test.js
@@ -62,8 +62,21 @@ buster.testCase("lilmodel", {
var results = chef.recipes.get({ name: 'tacos' });
chef.recipes.remove({ name: 'meatball sauce' });
+ var serialized = chef.serialize();
assert.equals(chef.name, 'gus');
assert.equals(chef.sousChef.name, 'zoe');
+ assert.equals(serialized, {
+ name: 'gus',
+ style: 'classic',
+ sousChef: {
+ name: 'zoe',
+ style: 'classic'
+ },
+ recipes: [
+ { name: 'tacos' },
+ { name: 'eggs' }
+ ]
+ });
chef.name = 'august';
chef.sousChef = { name: 'zozo' };

0 comments on commit 6e0fa6e

Please sign in to comment.