micromodel
Advanced tools
Comparing version 2.0.2 to 2.1.0
12
model.js
@@ -159,3 +159,3 @@ // Generated by CoffeeScript 1.6.1 | ||
validator = _ref[name]; | ||
if (msg = validator(this[name])) { | ||
if (msg = validator.call(this, this[name])) { | ||
_results.push(((_ref1 = (_base = this.errors)[name]) != null ? _ref1 : _base[name] = []).push(msg)); | ||
@@ -179,2 +179,12 @@ } else { | ||
Model.prototype.addError = function() { | ||
var args, _base, _base1, _name, _ref, _ref1; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
if (args.length < 2) { | ||
return ((_ref = (_base = this.errors).base) != null ? _ref : _base.base = []).push(args[0]); | ||
} else { | ||
return ((_ref1 = (_base1 = this.errors)[_name = args[0]]) != null ? _ref1 : _base1[_name] = []).push(args[1]); | ||
} | ||
}; | ||
Model.prototype.attributes = function() { | ||
@@ -181,0 +191,0 @@ var attrs, name, value; |
@@ -54,3 +54,3 @@ // Generated by CoffeeScript 1.6.1 | ||
sync(Db.prototype, 'collection', 'clear'); | ||
sync(Db.prototype, 'collection', 'clear', 'eval'); | ||
@@ -62,17 +62,3 @@ sync(Collection.prototype, 'insert', 'findOne', 'count', 'remove', 'update', 'ensureIndex', 'indexes', 'drop'); | ||
module.exports.ModelPersistence = function(Model) { | ||
var extractCustomOptions, handleSomeErrors; | ||
extractCustomOptions = function(options) { | ||
var bang, originalId; | ||
options = _(options).clone(); | ||
originalId = options.originalId; | ||
delete options.originalId; | ||
bang = options.bang; | ||
delete options.bang; | ||
return [ | ||
options, { | ||
bang: bang, | ||
originalId: originalId | ||
} | ||
]; | ||
}; | ||
var handleSomeErrors; | ||
handleSomeErrors = function(model, fn) { | ||
@@ -117,3 +103,3 @@ var _base, _ref1; | ||
Model.first = function(selector, options) { | ||
var bang, data, _ref1, _ref2; | ||
var data; | ||
if (selector == null) { | ||
@@ -125,8 +111,5 @@ selector = {}; | ||
} | ||
_ref1 = extractCustomOptions(options), options = _ref1[0], (_ref2 = _ref1[1], bang = _ref2.bang); | ||
data = this.collection().findOne(selector, options); | ||
if (data) { | ||
return new this(data); | ||
} else if (bang) { | ||
throw new Error("document '" + (inspect(selector)) + "', '" + (inspect(options)) + "' not found!"); | ||
} else { | ||
@@ -169,25 +152,15 @@ return null; | ||
Model.prototype.create = function(options) { | ||
var bang, fail, result, _ref1, _ref2, | ||
_this = this; | ||
var _this = this; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
_ref1 = extractCustomOptions(options), options = _ref1[0], (_ref2 = _ref1[1], bang = _ref2.bang); | ||
fail = function() { | ||
if (bang) { | ||
throw new Error("can't create invalid model '" + this + "'!"); | ||
} else { | ||
return false; | ||
} | ||
}; | ||
if (!this.isValid()) { | ||
return fail(); | ||
return false; | ||
} | ||
result = handleSomeErrors(this, function() { | ||
return handleSomeErrors(this, function() { | ||
return _this.collection().insert(_this.toJson(), options); | ||
}); | ||
return result || fail(); | ||
}; | ||
Model.prototype.update = function(options) { | ||
var bang, fail, originalId, result, _ref1, _ref2, | ||
var originalId, | ||
_this = this; | ||
@@ -197,14 +170,9 @@ if (options == null) { | ||
} | ||
_ref1 = extractCustomOptions(options), options = _ref1[0], (_ref2 = _ref1[1], bang = _ref2.bang, originalId = _ref2.originalId); | ||
fail = function() { | ||
if (bang) { | ||
throw new Error("can't update invalid model '" + this + "'!"); | ||
} else { | ||
return false; | ||
} | ||
}; | ||
options = _(options).clone(); | ||
originalId = options.originalId; | ||
delete options.originalId; | ||
if (!this.isValid()) { | ||
return fail(); | ||
return false; | ||
} | ||
result = handleSomeErrors(this, function() { | ||
return handleSomeErrors(this, function() { | ||
return _this.collection().update({ | ||
@@ -214,3 +182,2 @@ id: originalId || _this.id | ||
}); | ||
return result || fail(); | ||
}; | ||
@@ -217,0 +184,0 @@ Model.prototype["delete"] = function(options) { |
{ | ||
"name" : "micromodel", | ||
"main" : "./model", | ||
"version" : "2.0.2", | ||
"version" : "2.1.0", | ||
"homepage" : "http://alexeypetrushin.github.com/micromodel", | ||
@@ -6,0 +6,0 @@ "dependencies" : { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
63098
490