Comparing version 0.4.8 to 0.5.0
@@ -84,2 +84,4 @@ /** | ||
this.attributes = attrs || {}; | ||
this.changed = {}; | ||
this.previous = {}; | ||
this.options = options || {}; | ||
@@ -132,5 +134,17 @@ | ||
return this.attributes[key] = value; | ||
this.previous[key] = this.get(key); | ||
this.attributes[key] = value; | ||
this.changed[key] = value; | ||
return value; | ||
}, | ||
setDefaults: function () { | ||
var defaults = this.defaults || {}; | ||
Object.keys(defaults).forEach(function (key) { | ||
if (undefined == this.get(key)) this.set(key, defaults[key]); | ||
}, this); | ||
}, | ||
toJSON: function () { | ||
@@ -141,24 +155,3 @@ return this.attributes; | ||
configure: function () { | ||
for (var name in this) { | ||
var fn = this[name]; | ||
if (fn.hook) { | ||
var action = fn.hook.action; | ||
var when = fn.hook.when; | ||
delete fn.hook; | ||
switch (when) { | ||
case 'before': | ||
case 'after': | ||
this.hooks[when][action].push(fn); | ||
break; | ||
case 'around': | ||
this.hooks['before'][action].push(fn); | ||
this.hooks['after'][action].unshift(fn); | ||
break; | ||
} | ||
} | ||
} | ||
}, | ||
@@ -180,3 +173,5 @@ | ||
this.before(action, method); | ||
this.after(action, method); | ||
if ('string' === typeof method) method = this[method]; | ||
this.hooks.after[action].unshift(method); | ||
}, | ||
@@ -189,2 +184,5 @@ | ||
save: function *() { | ||
// set default values if needed | ||
this.setDefaults(); | ||
var id = this.get('_id'); | ||
@@ -372,8 +370,1 @@ var fn = id ? this.update : this.create; | ||
exports.Model = Model = Class.extend(InstanceMethods, StaticMethods); | ||
/** | ||
* Extending Function prototype | ||
*/ | ||
require('./util'); |
@@ -5,2 +5,3 @@ /** | ||
var ObjectID = require('monk/node_modules/mongoskin').ObjectID; | ||
var Class = require('class-extend'); | ||
@@ -40,5 +41,7 @@ | ||
// 1. if regular expression | ||
// 2. if object and not ObjectID | ||
if (value instanceof RegExp) { | ||
value = { $regex: value }; | ||
} else if ('object' === typeof value) { | ||
} else if ('object' === typeof value && !(value instanceof ObjectID)) { | ||
value = { $elemMatch: value }; | ||
@@ -45,0 +48,0 @@ } |
{ | ||
"name": "mongorito", | ||
"version": "0.4.8", | ||
"version": "0.5.0", | ||
"description": "ES6 generator-based MongoDB ODM. It rocks.", | ||
@@ -5,0 +5,0 @@ "author": "Vadim Demedes <vdemedes@gmail.com>", |
13740
6
421