Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Nested operations

  • Loading branch information...
commit c4ec14fcb47a29c4f17f0129e43cffe6b8991966 1 parent 91c76c6
mde authored
View
12 lib/query/comparison.js
@@ -17,7 +17,7 @@ comparison.create = function () {
return inst;
};
-ComparisonBase = new (function () {
+ComparisonBase = function () {
this.initialize = function (field, value, datatype, opts) {
this.parent = null;
this.descendants = [];
@@ -28,7 +28,7 @@ ComparisonBase = new (function () {
this.opts = opts || {};
};
-})();
+};
comparisonTypes = {
EqualToComparison: function () {
@@ -66,11 +66,11 @@ comparisonTypes = {
};
(function () {
- var type;
+ var ctor;
for (var t in comparisonTypes) {
- type = comparisonTypes[t];
- type.prototype = ComparisonBase;
- type.prototype.constructor = type;
+ ctor = comparisonTypes[t];
+ ctor.prototype = new ComparisonBase();
+ ctor.prototype.constructor = ctor;
}
})();
View
17 lib/query/operation.js
@@ -17,7 +17,7 @@ operation.create = function () {
};
-OperationBase = new (function () {
+OperationBase = function () {
this.initialize = function () {
var operands = Array.prototype.slice.call(arguments);
@@ -112,10 +112,10 @@ OperationBase = new (function () {
return false;
};
-})();
+};
operationTypes = {
- AndOperation: function (operands) {
+ AndOperation: function () {
this.matches = function (record) {
return this.operands.every(function (op) {
@@ -232,9 +232,14 @@ operationTypes = {
}
};
-for (var t in operationTypes) {
- operationTypes[t].prototype = OperationBase;
-}
+(function () {
+ var ctor;
+ for (var t in operationTypes) {
+ ctor = operationTypes[t];
+ ctor.prototype = new OperationBase();
+ ctor.prototype.constructor = ctor;
+ }
+})();
// Export the specific constructors as well as the `create` function
utils.mixin(operation, operationTypes);
View
29 lib/query/query.js
@@ -24,6 +24,13 @@ query.Query.prototype = new (function () {
, 'lte': 'LessThanOrEqual'
}
+ , _operationTypes = {
+ 'and': true
+ , 'or': true
+ , 'not': true
+ , 'null': true
+ }
+
, _createIsValidField = function () {
var reg = {
id: true
@@ -40,18 +47,24 @@ query.Query.prototype = new (function () {
};
}
- , _createConditions = function (conditions) {
- var op = new operation.create('and')
- , comp;
- for (var key in conditions) {
- // TODO: Handle associations
- comp = _createComparison.apply(this, [key, conditions[key]]);
- op.add(comp);
+ , _createConditions = function (conditions, key) {
+ var type = key || 'and'
+ , op = new operation.create(type)
+ , operand;
+ // TODO: Handle associations
+ for (var k in conditions) {
+ if (_operationTypes[k]) {
+ operand = _createConditions.apply(this, [conditions[k], k]);
+ }
+ else {
+ operand = _createComparison.apply(this, [conditions[k], k]);
+ }
+ op.add(operand);
}
return op;
}
- , _createComparison = function (key, val) {
+ , _createComparison = function (val, key) {
var type
, descr = model.descriptionRegistry[
this.model.modelName].properties[key]
View
2  test/create_user.js
@@ -12,7 +12,7 @@ _params = {
firstName: 'Neil'
};
-var tests = {
+tests = {
'test validity': function () {
var user = User.create(_params);
View
20 test/query/query.js
@@ -4,14 +4,26 @@ var model = require('../../lib/index')
, comparison = require('../../lib/query/comparison')
, utils = require('utilities')
, assert = require('assert')
- , User = require('../fixtures/user').User
+ , Zooby = require('../fixtures/zooby').Zooby
, tests;
var tests = {
- 'test condition': function () {
- var query = new Query(User, {login: {like: 'foo'}, firstName: null}, {});
+ 'test condition is AndOperation': function () {
+ var query = new Query(Zooby, {foo: {like: 'foo'}, bar: null}, {});
assert.ok(query.conditions instanceof operation.AndOperation);
- //console.log(query.conditions.toString());
+ }
+
+, 'test nested conditions': function () {
+ var conditions = {foo: 'bar', baz: 'qux', or: {foo: '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 comparison.EqualToComparison);
+ assert.ok(operands[2] instanceof operation.OrOperation);
+ operands = operands[2].operands;
+ assert.ok(operands[0] instanceof comparison.EqualToComparison);
+ assert.ok(operands[1] instanceof comparison.EqualToComparison);
}
};

0 comments on commit c4ec14f

Please sign in to comment.
Something went wrong with that request. Please try again.