Permalink
Browse files

[api merge] Manual merge of #74 since yawnt/sync-hooks branch was del…

…eted on the remote. Fixes #50
  • Loading branch information...
1 parent d3facee commit 86dc87f49eeab5743f5aab7f60e5f04fe4ed09af @indexzero indexzero committed Jun 20, 2012
Showing with 186 additions and 5 deletions.
  1. +22 −4 lib/resourceful/resource.js
  2. +1 −1 test/{hooks-test.js → hooks-async-test.js}
  3. +163 −0 test/hooks-sync-test.js
@@ -40,7 +40,7 @@ Resource.runBeforeHooks = function (method, obj, callback, finish) {
(function loop(hooks) {
var hook = hooks.shift();
- if (hook) {
+ if (hook && hook.length === 2) {
hook(obj, function (e, obj) {
if (e || obj) {
if (callback) {
@@ -51,7 +51,16 @@ Resource.runBeforeHooks = function (method, obj, callback, finish) {
loop(hooks);
}
});
- } else {
+ }
+ else if (hook && hook.length === 1) {
+ var res = hook(obj);
+ if(res === true) {
+ loop(hooks);
+ } else {
+ if (callback) { callback(res, obj); }
+ }
+ }
+ else {
finish();
}
})(this.hooks.before[method].slice(0));
@@ -63,12 +72,21 @@ Resource.runAfterHooks = function (method, e, obj, finish) {
(function loop(hooks) {
var hook = hooks.shift();
- if (hook) {
+ if (hook && hook.length === 3) {
hook(e, obj, function (e, obj) {
if (e) { finish(e, obj); }
else { loop(hooks); }
});
- } else {
+ }
+ else if (hook && hook.length === 2) {
+ var res = hook(e, obj);
+ if (res === true) {
+ loop(hooks);
+ } else {
+ finish(res, obj);
+ }
+ }
+ else {
finish();
}
})(this.hooks.after[method].slice(0));
@@ -2,7 +2,7 @@ var assert = require('assert'),
vows = require('vows'),
resourceful = require('../lib/resourceful');
-vows.describe('resourceful/hooks').addBatch({
+vows.describe('resourceful/hooks/async').addBatch({
"save-able Resource": {
topic: function () {
return resourceful.define('resource', function () {
@@ -0,0 +1,163 @@
+var assert = require('assert'),
+ vows = require('vows'),
+ resourceful = require('../lib/resourceful');
+
+vows.describe('resourceful/hooks/sync').addBatch({
+ "a Resource (save)": {
+ topic: function () {
+ return resourceful.define('Resource', function () {
+ this.property('name');
+ });
+ },
+ "synchronous 'before' hooks on `save`": {
+ topic: function (R) {
+ var that = this;
+ that.run = 0;
+ R.before('save', function (obj) {
+ that.run += 2;
+ obj.counter += 2;
+ return true;
+ });
+ R.before('save', function (obj) {
+ that.run *= 2;
+ obj.counter *= 2;
+ return true;
+ });
+ return R;
+ },
+ "when calling save() on an instance of Article": {
+ topic: function (R) {
+ new(R)({ _id: '256', counter: 0, name: 'a-name' }).save(this.callback);
+ },
+ "should trigger both hooks in the right order": function (e, res) {
+ assert.isNull(e);
+ assert.equal(res.counter, 4);
+ assert.equal(this.run, 4);
+ }
+ }
+ }
+ },
+ "another Resource (save)": {
+ topic: function () {
+ return resourceful.define('Resource2', function () {
+ this.property('name');
+ });
+ },
+ "synchronous 'after' hooks on `save`": {
+ topic: function (R) {
+ var that = this;
+ that.run = 0;
+ R.after('save', function (e, obj) {
+ that.run += 2;
+ obj.counter += 2;
+ return true;
+ });
+ R.after('save', function (e, obj) {
+ that.run *= 2;
+ obj.counter *= 2;
+ return true;
+ });
+ return R;
+ },
+ "should have two 'after' hooks registered": function (R) {
+ assert.equal(R.hooks.after.save.length, 2);
+ assert.equal(R.hooks.before.save.length, 0);
+ },
+ "when calling save() on an instance of Article": {
+ topic: function (R) {
+ new(R)({ _id: '678', counter: 0, title: 'a-name2' }).save(this.callback);
+ },
+ "should trigger both hooks in the right order": function (e, res) {
+ assert.isNull(e);
+ assert.equal(this.run, 4);
+ assert.equal(res.counter, 4);
+ }
+ }
+ }
+ },
+ "a Resource (create)": {
+ topic: function () {
+ return resourceful.define('Resource3', function () {
+ this.property('title');
+ });
+ },
+ "with synchronous 'before' hooks on `create`": {
+ topic: function (R) {
+ var that = this;
+ that.run = 0;
+ R.before('create', function (obj) {
+ that.run += 2;
+ obj.counter += 2;
+ return true;
+ });
+ R.before('create', function (obj) {
+ that.run *= 2;
+ obj.counter *= 2;
+ return true;
+ });
+ return R;
+ },
+ "when calling create() on an instance of Article": {
+ topic: function (R) {
+ R.create({ _id: '69', counter: 0, title: 'a-name3' }, this.callback);
+ },
+ "should trigger both hooks in the right order": function (e, res) {
+ assert.isNull(e);
+ assert.equal(this.run, 4);
+ assert.equal(res.counter, 4);
+ }
+ }
+ }
+ },
+ "another Resource (create)": {
+ topic: function () {
+ return resourceful.define('Resource4', function () {
+ this.property('title');
+ });
+ },
+ "with synchronous 'after' hooks on `create`": {
+ topic: function (R) {
+ var that = this;
+ that.save = 0;
+ R.after('save', function (e, obj) {
+ that.save += 2;
+ obj.counter += 2;
+ return true;
+ });
+ R.after('save', function (e, obj) {
+ that.save *= 2;
+ obj.counter *= 2;
+ return true;
+ });
+ this.create = 0;
+ R.after('create', function (e, obj) {
+ assert.equal(that.save, 0);
+ that.create += 2;
+ obj.counter += 2;
+ return true;
+ });
+ R.after('create', function (e, obj) {
+ that.create *= 2;
+ obj.counter *= 2;
+ return true;
+ });
+ return R;
+ },
+ "should have two 'after' hooks registered": function (R) {
+ assert.equal(R.hooks.after.create.length, 2);
+ assert.equal(R.hooks.before.create.length, 0);
+ },
+ "when calling create() on an instance of Article": {
+ topic: function (R) {
+ R.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.save, 4);
+ assert.equal(this.create, 4);
+ assert.equal(res.counter, 12);
+ }
+ }
+ }
+ }
+}).export(module);

0 comments on commit 86dc87f

Please sign in to comment.