Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplified the callbacks on `proxy`

  • Loading branch information...
commit 4bca8b97cab3fe29b04bfd1f4b5a7967dff25869 1 parent 5fefbbb
Pau Ramon Revilla authored
22 lib/helpers/collection_proxy.js
View
@@ -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});
@@ -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 () {};
}
@@ -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);
}
});
11 lib/model.js
View
@@ -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;
@@ -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;
};
2  test/helpers_collection_proxy_test.js
View
@@ -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);
32 test/model_test.js
View
@@ -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);
@@ -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
@@ -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);
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.