Permalink
Browse files

Handle 'not' operation

  • Loading branch information...
mde
mde committed Aug 16, 2012
1 parent c4ec14f commit a81f7ba1ca3a4ab0926381678dcaa58e4ceff304
Showing with 28 additions and 3 deletions.
  1. +1 −1 lib/query/operation.js
  2. +9 −2 lib/query/query.js
  3. +18 −0 test/query/query.js
View
@@ -186,7 +186,7 @@ operationTypes = {
if (this.operand === this) {
throw new Error('Operand for Not operation can\'t be a self-reference.');
}
- this.constructor.prototype.add.apply(self, arguments);
+ this.constructor.prototype.add.apply(this, arguments);
};
this.minimize = function () {
View
@@ -49,12 +49,19 @@ query.Query.prototype = new (function () {
, _createConditions = function (conditions, key) {
var type = key || 'and'
- , op = new operation.create(type)
+ , op = operation.create(type)
, operand;
+
// TODO: Handle associations
for (var k in conditions) {
if (_operationTypes[k]) {
- operand = _createConditions.apply(this, [conditions[k], k]);
+ if (k == 'not') {
+ operand = operation.create(k);
+ operand.add(_createConditions.apply(this, [conditions[k], 'and']));
+ }
+ else {
+ operand = _createConditions.apply(this, [conditions[k], k]);
+ }
}
else {
operand = _createComparison.apply(this, [conditions[k], k]);
View
@@ -26,6 +26,24 @@ var tests = {
assert.ok(operands[1] instanceof comparison.EqualToComparison);
}
+, 'test nested conditions': function () {
+ var conditions = {foo: 'bar', baz: 'qux', or: {foo: 'baz', baz: 'zoobie'}}
+ , query = new Query(Zooby, conditions, {})
+ , operands;
+ }
+
+, 'test not condition': function () {
+ var conditions = {foo: 'bar', not: {bar: 'baz', baz: 'zoobie'}}
+ , query = new Query(Zooby, conditions, {})
+ , operands;
+
+ operands = query.conditions.operands;
+ assert.ok(operands[0] instanceof comparison.EqualToComparison);
+ assert.ok(operands[1] instanceof operation.NotOperation);
+ operands = operands[1].operands;
+ assert.ok(operands[0] instanceof operation.AndOperation);
+ }
+
};
module.exports = tests;

0 comments on commit a81f7ba

Please sign in to comment.