Skip to content

Commit

Permalink
Simplified the callbacks on proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed May 13, 2012
1 parent 5fefbbb commit 4bca8b9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 48 deletions.
22 changes: 10 additions & 12 deletions lib/helpers/collection_proxy.js
Expand Up @@ -2,7 +2,7 @@ var PROXY = {}
, _ = require('underscore');

function _apply(collection, fn, args) {
return collection[fn].apply(collection, args);
collection[fn].apply(collection, args);
}

Object.defineProperty(PROXY, 'namespacer', {value: require('./namespacer'), writable: true});
Expand All @@ -14,16 +14,19 @@ Object.defineProperty(PROXY, 'mapper', {value: require('./mapper'), writable: tr
* @param {Object} model
* @param {Object} options
* @param {Array} args
* @param {Function} callback
*/
PROXY.proxy = function (model, options, args, callback) {
PROXY.proxy = function (model, options, args) {
var fn = options.method
, callback
, arguments_length = arguments.length;

model.getCollection(function (error, collection) {
var is_overwritten = typeof PROXY[fn] !== 'undefined'
, is_native = typeof collection[fn] !== 'undefined';

// noop
if (arguments_length < 4) {
if (typeof args[args.length - 1] === 'function') {
callback = args[args.length - 1];
} else {
callback = function () {};
}

Expand All @@ -42,15 +45,10 @@ PROXY.proxy = function (model, options, args, callback) {
}

// overwritten method with hooks, or custom method
if (typeof PROXY[fn] !== 'undefined' && (typeof collection[fn] !== 'undefined' ? options.hooks : true)) {
return PROXY[fn](model, collection, args, callback);

if (is_overwritten && (is_native ? options.hooks : true)) {
PROXY[fn](model, collection, args, callback);
// driver method or hooks disabled
} else {
if (!_.isFunction(args[args.length - 1])) {
args.push(callback);
}

_apply(collection, fn, args);
}
});
Expand Down
11 changes: 2 additions & 9 deletions lib/model.js
Expand Up @@ -44,7 +44,7 @@ module.exports = function (db, collection_name) {
* @param {String|Object} options
* @param {Mixed} args
* @param {Function} callback
* @returns itself
* @return itself
*/
MODEL.mongo = function (args) {
var parsed_method, options, callback;
Expand All @@ -70,14 +70,7 @@ module.exports = function (db, collection_name) {
, mapping: true
}, options);

if (typeof args[args.length - 1] === 'function') {
callback = args[args.length - 1];
} else {
callback = function () {}; // noop
args.push(callback);
}

MODEL.collection_proxy.proxy(MODEL, options, args, callback);
MODEL.collection_proxy.proxy(MODEL, options, args);

return MODEL;
};
Expand Down
2 changes: 1 addition & 1 deletion test/helpers_collection_proxy_test.js
Expand Up @@ -128,7 +128,7 @@ describe('collection proxy test', function () {
['find', 'update', 'insert'].forEach(function (method) {
stubGetCollection(self);
stub = self.stub(Collection, method, function (args, callback) {
assert.equal(typeof callback, 'function');
assert.equal(typeof callback, 'undefined');
});

CollectionProxy.proxy(Model, {method: method, hooks: false}, args);
Expand Down
32 changes: 6 additions & 26 deletions test/model_test.js
Expand Up @@ -47,10 +47,10 @@ describe('Models', function () {
, stub, self = this
, query = {name: 'zemba'};

stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args) {
assert.deepEqual(_options, {hooks: true, namespacing: true, mapping: true, method: 'findArray'});
assert.deepEqual(_args[0], query);
assert.equal(_callback, callback);
assert.deepEqual(_args[1], callback);
});

User.mongo('findArray', query, callback);
Expand All @@ -62,7 +62,7 @@ describe('Models', function () {
, stub, self = this
, query = {name: 'zemba'};

stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args) {
assert.deepEqual(_options, {
hooks: true
, namespacing: true
Expand All @@ -71,7 +71,7 @@ describe('Models', function () {
, namespace: 'public'
});
assert.deepEqual(_args[0], query);
assert.equal(_callback, callback);
assert.deepEqual(_args[1], callback);
});

User.mongo('findArray:public', query, callback);
Expand All @@ -83,7 +83,7 @@ describe('Models', function () {
, stub, self = this
, query = {name: 'zemba'};

stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args) {
assert.deepEqual(_options, {
hooks: false
, namespacing: true
Expand All @@ -92,33 +92,13 @@ describe('Models', function () {
, namespace: 'public'
});
assert.deepEqual(_args[0], query);
assert.equal(_callback, callback);
assert.deepEqual(_args[1], callback);
});

User.mongo({method: 'findArray', namespace: 'public', hooks: false}, query, callback);
sinon.assert.calledOnce(stub);
}));

it('`mongo` can be called without a callback', sinon.test(function () {
var query = {name: 'zemba'}
, stub, self = this;

stub = self.stub(User.collection_proxy, 'proxy', function (_model, _options, _args, _callback) {
assert.deepEqual(_options, {
hooks: true
, namespacing: true
, mapping: true
, method: 'findArray'
});
assert.deepEqual(_args[0], query);
assert.equal(typeof _args[_args.length - 1], 'function');
assert.equal(typeof _callback, 'function');
});

User.mongo('findArray', query);
sinon.assert.calledOnce(stub);
}));

it('`validate` validates a mongo document', sinon.test(function () {
var document = {}
, self = this, stub
Expand Down

0 comments on commit 4bca8b9

Please sign in to comment.