resourceful
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -10,2 +10,3 @@ var resourceful = exports; | ||
resourceful.defineProperty = require('./resourceful/core').defineProperty; | ||
resourceful.init = require('./resourceful/init'); | ||
@@ -12,0 +13,0 @@ resourceful.use = require('./resourceful/core').use; |
@@ -72,3 +72,3 @@ /* | ||
common.pluralize = function (s) { | ||
return s + 's'; | ||
return /s$/.test(s) ? s : s + 's'; | ||
}; |
var util = require('util'), | ||
events = require('events'), | ||
common = require('./common'); | ||
common = require('./common'), | ||
init = require('./init'); | ||
@@ -27,3 +28,4 @@ var resourceful = exports; | ||
} | ||
} else if (typeof engine === 'object') { | ||
} | ||
else if (typeof engine === 'object') { | ||
this.engine = engine; | ||
@@ -51,4 +53,4 @@ } | ||
case 'number': options.port = parseInt(a, 10); break; | ||
case 'string': options.uri = a; break; | ||
case 'object': options = a; break; | ||
case 'string': options.uri = a; break; | ||
case 'object': options = a; break; | ||
} | ||
@@ -65,3 +67,4 @@ }); | ||
engine = resourceful.engines[common.capitalize(protocol)]; | ||
} else { | ||
} | ||
else { | ||
engine = resourceful.engine || this.engine; | ||
@@ -107,3 +110,3 @@ } | ||
}); | ||
this._properties.resource = name; | ||
@@ -211,3 +214,7 @@ | ||
enumerable: true | ||
}); | ||
}); | ||
if (typeof obj[property] === 'undefined') { | ||
obj[property] = init(obj, property, schema); | ||
} | ||
}; | ||
@@ -214,0 +221,0 @@ |
@@ -167,5 +167,6 @@ var events = require('events'), | ||
} | ||
return id ? | ||
this._request('get', id, callback) | ||
: callback && callback(new Error('key is undefined')); | ||
this._request('get', id, callback) | ||
: callback && callback(new Error('key is undefined')); | ||
}; | ||
@@ -178,21 +179,33 @@ | ||
var instance = new(this)(attrs), | ||
validate = this.prototype.validate(instance, this.schema); | ||
var that = this; | ||
this.runBeforeHooks("create", attrs, callback, function () { | ||
var instance = new(that)(attrs); | ||
if (!validate.valid) { | ||
if (callback) { | ||
callback(validate.errors); | ||
} | ||
return; | ||
} | ||
// could happen after validate, but would unnecessarily remove the validation safety net | ||
that.runAfterHooks("create", null, instance, function (e, res) { | ||
if (e) { | ||
return that.emit('error', e); | ||
} | ||
instance.save(function (e, res) { | ||
if (res) { | ||
instance._id = instance._id || res.id; | ||
instance._rev = instance._rev || res.rev; | ||
} | ||
var validate = that.prototype.validate(instance, that.schema); | ||
if (callback) { | ||
callback(e, instance); | ||
} | ||
if (!validate.valid) { | ||
that.emit('error', validate.errors); | ||
if (callback) { | ||
callback(validate.errors); | ||
} | ||
return; | ||
} | ||
instance.save(function (e, res) { | ||
if (res) { | ||
instance._id = instance._id || res.id; | ||
instance._rev = instance._rev || res.rev; | ||
} | ||
if (callback) { | ||
callback(e, instance); | ||
} | ||
}); | ||
}); | ||
}); | ||
@@ -238,6 +251,8 @@ }; | ||
} | ||
return this._request("filter", | ||
name, | ||
{ options: options, filter: filter, resource: this }, | ||
function () {}); | ||
return this._request("filter", name, { | ||
options: options, | ||
filter: filter, | ||
resource: this | ||
}, function () {}); | ||
}; | ||
@@ -547,6 +562,5 @@ | ||
Resource.prototype.writeProperty = function (k, val, setter) { | ||
return this._properties[k] = setter ? | ||
setter.call(this, val) | ||
: | ||
val; | ||
return this._properties[k] = setter | ||
? setter.call(this, val) | ||
: val; | ||
}; | ||
@@ -553,0 +567,0 @@ |
{ | ||
"name": "resourceful", | ||
"description": "A storage agnostic resource-oriented ODM for building prototypical models with validation and sanitization.", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"url": "http://github.com/flatiron/resourceful", | ||
@@ -6,0 +6,0 @@ "keywords": ["ODM", "database", "couchdb", "model", "resource"], |
@@ -8,3 +8,3 @@ var path = require('path'), | ||
vows.describe('resourceful/hooks').addBatch({ | ||
"a Resource": { | ||
"save-able Resource": { | ||
topic: function () { | ||
@@ -18,8 +18,8 @@ return resourceful.define('resource', function () { | ||
var that = this; | ||
this.hooked = 0; | ||
this.hooked_save = 0; | ||
A.before('save', function (obj, next) { | ||
that.hooked += 1; next(null); | ||
that.hooked_save += 1; next(null); | ||
}); | ||
A.before('save', function (obj, next) { | ||
that.hooked *= 2; next(null); | ||
that.hooked_save *= 2; next(null); | ||
}); | ||
@@ -30,7 +30,7 @@ return A; | ||
topic: function (A) { | ||
new(A)({ _id: '64', counter: 0, title: 'foobar' }).save(this.callback); | ||
new(A)({ _id: '128', counter: 0, title: 'foobar' }).save(this.callback); | ||
}, | ||
"should trigger both hooks in the right order": function (e, res) { | ||
assert.isNull(e); | ||
assert.equal(this.hooked, 2); | ||
assert.equal(this.hooked_save, 2); | ||
} | ||
@@ -40,3 +40,3 @@ } | ||
}, | ||
"another Resource": { | ||
"another save-able Resource": { | ||
topic: function () { | ||
@@ -52,7 +52,7 @@ return resourceful.define('resource2', function () { | ||
A.after('save', function (e, obj, next) { | ||
that.hooked ++; next(null); | ||
that.hooked += 1; next(null); | ||
// TODO: test other stuff | ||
}); | ||
A.after('save', function (e, obj, next) { | ||
that.hooked ++; next(null); | ||
that.hooked *= 2; next(null); | ||
}); | ||
@@ -75,3 +75,77 @@ return A; | ||
} | ||
}, | ||
"create-able Resource": { | ||
topic: function () { | ||
return resourceful.define('resourceCreate', function () { | ||
this.property('title'); | ||
}); | ||
}, | ||
"with 'before' hooks on `create`": { | ||
topic: function (A) { | ||
var that = this; | ||
this.hooked_create = 0; | ||
A.before('create', function (obj, next) { | ||
that.hooked_create += 1; next(null); | ||
}); | ||
A.before('create', function (obj, next) { | ||
that.hooked_create *= 2; next(null); | ||
}); | ||
return A; | ||
}, | ||
"when calling create() on an instance of Article": { | ||
topic: function (A) { | ||
A.create({ _id: '69', counter: 0, title: 'foobar' }, this.callback); | ||
}, | ||
"should trigger both hooks in the right order": function (e, res) { | ||
assert.isNull(e); | ||
assert.equal(this.hooked_create, 2); | ||
} | ||
} | ||
}, | ||
}, | ||
"another create-able Resource": { | ||
topic: function () { | ||
return resourceful.define('resourceAfterCreate', function () { | ||
this.property('title'); | ||
}); | ||
}, | ||
"with 'after' hooks on `create`": { | ||
topic: function (A) { | ||
var that = this; | ||
this.hooked = 0; | ||
A.after('save', function (e, obj, next) { | ||
that.hooked += 1; next(null); | ||
// TODO: test other stuff | ||
}); | ||
A.after('save', function (e, obj, next) { | ||
that.hooked *= 2; next(null); | ||
}); | ||
this.hooked_create = 0; | ||
A.after('create', function (e, obj, next) { | ||
// after.create preceeds after.save | ||
assert.equal(that.hooked, 0); | ||
that.hooked_create += 1; next(null); | ||
// TODO: test other stuff | ||
}); | ||
A.after('create', function (e, obj, next) { | ||
that.hooked_create *= 2; next(null); | ||
}); | ||
return A; | ||
}, | ||
"should just have the two 'after' hooks registered": function (A) { | ||
assert.equal(A.hooks.after.create.length, 2); | ||
assert.equal(A.hooks.before.create.length, 0); | ||
}, | ||
"when calling create() on an instance of Article": { | ||
topic: function (A) { | ||
A.create({ _id: '67', counter: 0, title: 'hookbar' }, this.callback); | ||
}, | ||
"should trigger both hooks in the right order": function (e, res) { | ||
assert.isNull(e); | ||
assert.equal(this.hooked, 2); | ||
assert.equal(this.hooked_create, 2); | ||
} | ||
} | ||
} | ||
} | ||
}).export(module); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112704
25
2963