Permalink
Browse files

gardening

  • Loading branch information...
1 parent a451662 commit c9e9c73b648000f0f04555884e6fc16129ee212f @masylum committed May 13, 2012
Showing with 25 additions and 9 deletions.
  1. +25 −9 lib/model.js
View
@@ -89,6 +89,8 @@ module.exports = function (db, collection_name) {
/**
* Validates a mongo document and inserts it
*
+ * TODO: Refactor
+ *
* @param {Object} document
* @param {Object} options
* @param {Function} callback
@@ -121,8 +123,13 @@ module.exports = function (db, collection_name) {
}
MODEL.validate({}, document, function (error, validator) {
- if (error) return callback(error);
- if (validator.hasErrors()) return callback(null, validator);
+ if (error) {
+ return callback(error);
+ }
+
+ if (validator.hasErrors()) {
+ return callback(null, validator);
+ }
args.push(function cb(error, documents) {
if (error) {
@@ -237,6 +244,7 @@ module.exports = function (db, collection_name) {
if (options.namespace) {
fn.namespace = options.namespace;
}
+
delete options.namespace;
if (MODEL.namespaces && MODEL.namespaces[options.namespace]) {
@@ -250,17 +258,22 @@ module.exports = function (db, collection_name) {
}
MODEL.mongo('findOne', query, function (error, document) {
- if (error) return callback(error);
+ if (error) {
+ return callback(error);
+ }
MODEL.validate(document, update, function (error, validator) {
- if (error) return callback(error);
+ if (error) {
+ return callback(error);
+ }
if (validator.hasErrors()) {
- return callback(null, validator);
+ callback(null, validator);
} else {
args.push(function cb(error, _) {
callback(error, validator);
});
+
args.unshift(fn);
args[1] = {_id: document._id};
MODEL.mongo.apply(MODEL, args);
@@ -273,6 +286,7 @@ module.exports = function (db, collection_name) {
/**
* Get the document ready to embed to this model according to the skeleton
+ *
* TODO: Refactor this method! Or take it away as a helper
*
* @param {String} name
@@ -291,7 +305,9 @@ module.exports = function (db, collection_name) {
function parseAttr(scope, attr) {
return dot_notation ? ((scope ? scope + '.' : '') + attr) : attr;
}
- /* 0: not visible
+
+ /**
+ * 0: not visible
* 1: partial visible, needs more inspection
* 2: fully visible
*/
@@ -302,7 +318,7 @@ module.exports = function (db, collection_name) {
_.some(MODEL.skeletons[name], function (el) {
var match;
if (attr.indexOf('.') !== -1) {
- match = attr.match(new RegExp(el + "(\..*)?"));
+ match = attr.match(new RegExp(el + '(\..*)?'));
if (match) {
ret = 2;
}
@@ -384,7 +400,7 @@ module.exports = function (db, collection_name) {
update = MODEL.getEmbeddedDocument(document_name, document, document_name, true);
// options are optional
- if (typeof options === "function") {
+ if (typeof options === 'function') {
callback = options;
options = {};
} else {
@@ -418,7 +434,7 @@ module.exports = function (db, collection_name) {
update[document_name] = MODEL.getEmbeddedDocument(document_name, document);
// options are optional
- if (typeof options === "function") {
+ if (typeof options === 'function') {
callback = options;
options = {};
} else {

0 comments on commit c9e9c73

Please sign in to comment.