Skip to content

Commit

Permalink
fix embeds and inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
pmlopes committed Jul 10, 2012
1 parent c7e14e4 commit 7336f6d
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function extend(obj, proto) {
if (Array.isArray(obj)) {
var i;
for (i = 0; i < obj.length; i++) {
Object.defineProperty(obj[i], key, {value: proto[key]});
extend(obj[i], proto);
}
} else {
Object.defineProperty(obj, key, {value: proto[key]});
Expand Down Expand Up @@ -82,38 +82,43 @@ module.exports = function (mongoCollection, schemaDef) {
* @param {Object} [json] if provided will update the current instance with the json properties
*/
var Model = function (json) {

var filterUndefined = function (target, source) {
if (source !== undefined && source !== null) {
var key, value;
// update new ones
for (key in source) {
if (source.hasOwnProperty(key)) {
value = source[key];
if (value !== undefined) {
if (typeof value === 'object') {
if (value === null || value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Date || value instanceof ObjectID) {
target[key] = value;
} else {
if (target[key] === undefined) {
if (Array.isArray(value)) {
target[key] = [];
} else {
target[key] = {};
if (Model.$schema !== undefined) {
var filterUndefined = function (target, source) {
if (source !== undefined && source !== null) {
var key, value;
// update new ones
for (key in source) {
if (source.hasOwnProperty(key)) {
value = source[key];
if (value !== undefined) {
if (typeof value === 'object') {
if (value === null || value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Date || value instanceof ObjectID) {
target[key] = value;
} else {
if (target[key] === undefined) {
if (Array.isArray(value)) {
target[key] = [];
} else {
target[key] = {};
}
}
filterUndefined(target[key], value);
}
filterUndefined(target[key], value);
} else {
target[key] = value;
}
} else {
target[key] = value;
}
}
}
}
}
};
};

filterUndefined(this, json);
filterUndefined(this, json);
// inheritance
if (Model.$embeds !== undefined) {
extend(this, Model.$embeds);
}
}
};

var odm = this;
Expand Down Expand Up @@ -718,26 +723,6 @@ module.exports = function (mongoCollection, schemaDef) {
odm.update(mongoCollection, query, document, options, callback);
};

/**
* Enhances a basic object to be a model of this class
*
* @param {Object} obj
* @return {Model} obj as enhanced model
*/
Model.asModel = function (obj) {
if (obj && typeof obj === 'object') {
var protokey;
for (protokey in Model.prototype) {
if (protokey !== 'save' && protokey !== 'update' && protokey !== 'insert' && protokey !== 'remove' && protokey !== 'reload' && protokey !== 'validate') {
if (Model.prototype.hasOwnProperty(protokey)) {
Object.defineProperty(obj, protokey, {value: Model.prototype[protokey]});
}
}
}
}
return obj;
};

/**
* Clones the Type prototype to this model class
* @param path {String} Path to be updated
Expand All @@ -763,6 +748,14 @@ module.exports = function (mongoCollection, schemaDef) {
}
}
}

for (protokey in type.$embeds) {
if (protokey !== 'save' && protokey !== 'update' && protokey !== 'insert' && protokey !== 'remove' && protokey !== 'reload' && protokey !== 'validate') {
if (type.$embeds.hasOwnProperty(protokey)) {
result[protokey] = type.$embeds[protokey];
}
}
}
};

return Model;
Expand Down

0 comments on commit 7336f6d

Please sign in to comment.