Skip to content

Commit

Permalink
Revert "added; obj literal support to $pull"
Browse files Browse the repository at this point in the history
This reverts commit f49ceb8.

not yet ready
  • Loading branch information
aheckmann committed Sep 28, 2011
1 parent 22d35c1 commit a464546
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 94 deletions.
17 changes: 0 additions & 17 deletions lib/document.js
Expand Up @@ -831,23 +831,6 @@ Document.prototype.equals = function (doc) {
return this.get('_id') === doc.get('_id');
};

/**
* Returns true if this Document has matching
* modified path values that `doc` has.
*
* @param {Document} doc
* @return {Boolean}
* @api public
*/

Document.prototype.matches = function (doc) {
var paths = Object.keys(this._activePaths.states.modify||{});

return paths.every(function (path) {
return deepEqual(this.getValue(path), doc.getValue(path));
}, this);
};

/**
* Module exports.
*/
Expand Down
13 changes: 6 additions & 7 deletions lib/schema.js
Expand Up @@ -4,10 +4,9 @@
*/

var EventEmitter = require('events').EventEmitter
// lazily require these modules (race conditions)
, VirtualType
, VirtualType = require('./virtualtype')
, utils = require('./utils')
, NamedScope
, utils
, Query
, Types

Expand Down Expand Up @@ -530,11 +529,11 @@ function ObjectId () {
*/

module.exports = exports = Schema;
exports.ObjectId = ObjectId;

// require down here because of reference issues
VirtualType = require('./virtualtype');
utils = require('./utils');
exports.Types = Types = require('./schema/index');
NamedScope = require('./namedscope');
NamedScope = require('./namedscope')
Query = require('./query');

exports.ObjectId = ObjectId;

74 changes: 14 additions & 60 deletions lib/types/array.js
Expand Up @@ -3,10 +3,9 @@
* Module dependencies.
*/

var EmbeddedDocument = require('./embedded')
, Document = require('../document')
, ObjectId = require('./objectid')
, deepEqual // lazy require
var EmbeddedDocument = require('./embedded');
var Document = require('../document');
var ObjectId = require('./objectid');

/**
* Mongoose Array constructor.
Expand Down Expand Up @@ -236,66 +235,27 @@ MongooseArray.prototype.remove = function () {

MongooseArray.prototype.pull =
MongooseArray.prototype.$pull = function () {
var values = Array.prototype.map.call(arguments, function (arg) {
var id = arg && arg._id
, ret = this._cast(arg);

// casting EmbeddedDocuments adds _id by default
if (ret instanceof EmbeddedDocument && !id) {
delete ret._activePaths.states.modify._id;
delete ret._doc._id;
}

return ret;
}, this);

var oldArr = this._parent.get(this._path)
var values = Array.prototype.map.call(arguments, this._cast, this)
, oldArr = this._parent.get(this._path)
, i = oldArr.length
, found
, vkeys
, mkeys
, mem;

while (i--) {
mem = oldArr[i];
if (mem instanceof EmbeddedDocument) {
found = values.some(function (v) {
return v.matches(mem);
});

} else if ('Object' === mem.constructor.name) {

// comparing object literals.
vkeys || (vkeys = values.map(function (v) {
return Object.keys(v);
}));

mkeys = Object.keys(mem);

found = vkeys.some(function (vkeys, ii) {
var v = vkeys.length
, key;

if (mkeys.length < v) return false;

while (v--) {
key = vkeys[v];
if (!deepEqual(mem[key], values[ii][key])) {
return false;
}
}
return true;
});
} else {
found = ~values.indexOf(mem);
}

if (found) {
if (values.some(function (v) { return v.equals(mem); } )) {
oldArr.splice(i, 1);
}
} else if (~values.indexOf(mem)) {
oldArr.splice(i, 1);
}
}

this._registerAtomic('$pullAll', values);
if (values[0] instanceof EmbeddedDocument) {
this._registerAtomic('$pullAll', values.map( function (v) { return {_id: v._id}; } ));
} else {
this._registerAtomic('$pullAll', values);
}

return this;
};
Expand Down Expand Up @@ -388,9 +348,3 @@ MongooseArray.prototype.indexOf = function(obj){
*/

module.exports = MongooseArray;

/**
* Lazy require.
*/

deepEqual = require('../utils').deepEqual;
10 changes: 0 additions & 10 deletions lib/utils.js
Expand Up @@ -4,7 +4,6 @@

var EventEmitter = require('events').EventEmitter
, ObjectId = require('./types/objectid')
, MongooseArray // lazy require

/**
* Produces a collection name from a model name
Expand Down Expand Up @@ -159,9 +158,6 @@ exports.deepEqual = function deepEqual (a, b) {
return a.valueOf() === b.valueOf();
}

if (a instanceof MongooseArray) a = a.toObject();
if (b instanceof MongooseArray) b = b.toObject();

try {
var ka = Object.keys(a),
kb = Object.keys(b),
Expand Down Expand Up @@ -372,9 +368,3 @@ exports.args = function (args, slice, sliceEnd) {

return ret;
}

/**
* Lazy require.
*/

MongooseArray = require('./types/array');

0 comments on commit a464546

Please sign in to comment.