From 56ee1227ab425ef6baa3bf415fd31109012b300f Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 15 May 2013 18:28:25 +0200 Subject: [PATCH] Modifiers arguments must be objects --- lib/model.js | 26 +++++++++++++++++++++++++- test/model.test.js | 9 +++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index 7d5f2c8..70f7d6e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -125,8 +125,20 @@ function deepCopy (obj) { } +/** + * Set field to value in a model + * Create it if it doesn't exist + * @param {Object} obj The model to set a field for + * @param {Object} mod mod is of the form { k: v }. k is the field, v its value + * k can contain dots, in that case that means we will set a subfield + */ +modifierFunctions.$set = function (obj, mod) { + var field, value; + + +}; + modifierFunctions.$inc = function () {}; -modifierFunctions.$set = function () {}; /** @@ -154,7 +166,19 @@ function modify (obj, updateQuery) { // Apply modifiers modifiers = _.uniq(keys); modifiers.forEach(function (m) { + var keys; + if (!modifierFunctions[m]) { throw "Unknown modifier " + m; } + + try { + keys = Object.keys(updateQuery[m]); + } catch (e) { + throw "Modifier " + m + "'s argument must be an object"; + } + + keys.forEach(function (k) { + modifierFunctions[m](obj, k, updateQuery[m][k]); + }); }); } diff --git a/test/model.test.js b/test/model.test.js index 1984bec..e2fc1dd 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -240,6 +240,15 @@ describe('Model', function () { }).should.throw(); }); + it('Throw an error if a modifier is used with a non-object argument', function () { + var obj = { some: 'thing' } + , updateQuery = { $set: 'this exists' }; + + (function () { + model.modify(obj, updateQuery); + }).should.throw(); + }); + }); // ==== End of 'Modifying documents' ==== // });