Commit 5b0db3bd authored by Marco Mariani's avatar Marco Mariani

Custom operators: default_match -> equal_match

parent 87caa37b
...@@ -58,7 +58,6 @@ function SimpleQuery(spec, key_schema) { ...@@ -58,7 +58,6 @@ function SimpleQuery(spec, key_schema) {
* @optional * @optional
*/ */
this.operator = spec.operator || "="; this.operator = spec.operator || "=";
this._spec = spec.operator;
/** /**
* Key of the object which refers to the value to compare * Key of the object which refers to the value to compare
...@@ -92,7 +91,7 @@ var checkKey = function (key) { ...@@ -92,7 +91,7 @@ var checkKey = function (key) {
switch (prop) { switch (prop) {
case 'read_from': case 'read_from':
case 'cast_to': case 'cast_to':
case 'default_match': case 'equal_match':
break; break;
default: default:
throw new TypeError("Custom key has unknown property '" + throw new TypeError("Custom key has unknown property '" +
...@@ -108,7 +107,7 @@ var checkKey = function (key) { ...@@ -108,7 +107,7 @@ var checkKey = function (key) {
*/ */
SimpleQuery.prototype.match = function (item, wildcard_character) { SimpleQuery.prototype.match = function (item, wildcard_character) {
var object_value = null, var object_value = null,
default_match = null, equal_match = null,
cast_to = null, cast_to = null,
matchMethod = null, matchMethod = null,
value = null, value = null,
...@@ -124,20 +123,17 @@ SimpleQuery.prototype.match = function (item, wildcard_character) { ...@@ -124,20 +123,17 @@ SimpleQuery.prototype.match = function (item, wildcard_character) {
checkKey(key); checkKey(key);
object_value = item[key.read_from]; object_value = item[key.read_from];
default_match = key.default_match; equal_match = key.equal_match;
// default_match can be a string // equal_match can be a string
if (typeof default_match === 'string') { if (typeof equal_match === 'string') {
// XXX raise error if default_match not in match_lookup // XXX raise error if equal_match not in match_lookup
default_match = this._key_schema.match_lookup[default_match]; equal_match = this._key_schema.match_lookup[equal_match];
} }
// default_match overrides the default '=' operator // equal_match overrides the default '=' operator
matchMethod = (default_match || matchMethod); if (equal_match !== undefined) {
matchMethod = (this.operator === '=') ? equal_match : matchMethod;
// but an explicit operator: parameter overrides default_match
if (this._spec && this._spec.operator) {
matchMethod = this[this.operator];
} }
value = this.value; value = this.value;
......
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
key_set: { key_set: {
case_insensitive_identifier: { case_insensitive_identifier: {
read_from: 'identifier', read_from: 'identifier',
default_match: function (object_value, value, wildcard_character) { equal_match: function (object_value, value, wildcard_character) {
// XXX do this with a regexp and wildcard support // XXX do this with a regexp and wildcard support
return (object_value.toLowerCase() === value.toLowerCase()); return (object_value.toLowerCase() === value.toLowerCase());
} }
...@@ -66,21 +66,21 @@ ...@@ -66,21 +66,21 @@
date_day: { date_day: {
read_from: 'date', read_from: 'date',
cast_to: 'dateType', cast_to: 'dateType',
default_match: 'sameDay' equal_match: 'sameDay'
}, },
date_month: { date_month: {
read_from: 'date', read_from: 'date',
cast_to: 'dateType', cast_to: 'dateType',
default_match: 'sameMonth' equal_match: 'sameMonth'
}, },
date_year: { date_year: {
read_from: 'date', read_from: 'date',
cast_to: 'dateType', cast_to: 'dateType',
default_match: 'sameYear' equal_match: 'sameYear'
}, },
translated_state: { translated_state: {
read_from: 'state', read_from: 'state',
default_match: 'equalState' equal_match: 'equalState'
} }
} }
}; };
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
}, },
case_insensitive_identifier: { case_insensitive_identifier: {
read_from: 'identifier', read_from: 'identifier',
default_match: function (object_value, value, wildcard_character) { equal_match: function (object_value, value, wildcard_character) {
return (object_value.toLowerCase() === value.toLowerCase()); return (object_value.toLowerCase() === value.toLowerCase());
} }
} }
...@@ -105,17 +105,17 @@ ...@@ -105,17 +105,17 @@
day: { day: {
read_from: 'date', read_from: 'date',
cast_to: dateCast, cast_to: dateCast,
default_match: sameDay equal_match: sameDay
}, },
month: { month: {
read_from: 'date', read_from: 'date',
cast_to: dateCast, cast_to: dateCast,
default_match: sameMonth equal_match: sameMonth
}, },
year: { year: {
read_from: 'date', read_from: 'date',
cast_to: dateCast, cast_to: dateCast,
default_match: sameYear equal_match: sameYear
} }
}; };
...@@ -250,7 +250,7 @@ ...@@ -250,7 +250,7 @@
}); });
test('Simple Key with both default_match and operator attributes', function () { test('Simple Key with both equal_match and operator attributes', function () {
var doc_list, docList = function () { var doc_list, docList = function () {
return [ return [
{'identifier': '1', 'date': '2013-01-01'}, {'identifier': '1', 'date': '2013-01-01'},
...@@ -261,7 +261,7 @@ ...@@ -261,7 +261,7 @@
mydate: { mydate: {
read_from: 'date', read_from: 'date',
cast_to: dateCast, cast_to: dateCast,
default_match: function alwaysTrue() { return true; } equal_match: function alwaysTrue() { return true; }
} }
}; };
...@@ -282,13 +282,25 @@ ...@@ -282,13 +282,25 @@
complex_queries.QueryFactory.create({ complex_queries.QueryFactory.create({
type: 'simple', type: 'simple',
key: keys.mydate, key: keys.mydate,
operator: '>=', operator: '=',
value: '2013-02-02' value: '2013-02-02'
}).exec(doc_list); }).exec(doc_list);
deepEqual(doc_list, [ deepEqual(doc_list, [
{'identifier': '1', 'date': '2013-01-01'}, {'identifier': '1', 'date': '2013-01-01'},
{'identifier': '2', 'date': '2013-02-02'}, {'identifier': '2', 'date': '2013-02-02'},
{'identifier': '3', 'date': '2013-03-03'} {'identifier': '3', 'date': '2013-03-03'}
], "The catch-all filter overrides the default '=' operator");
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '>=',
value: '2013-02-02'
}).exec(doc_list);
deepEqual(doc_list, [
{'identifier': '2', 'date': '2013-02-02'},
{'identifier': '3', 'date': '2013-03-03'}
], 'An explicit operator should override the catch-all filter'); ], 'An explicit operator should override the catch-all filter');
}); });
...@@ -388,7 +400,7 @@ ...@@ -388,7 +400,7 @@
keys = { keys = {
translated_state: { translated_state: {
read_from: 'state', read_from: 'state',
default_match: equalState equal_match: equalState
} }
}; };
...@@ -416,6 +428,7 @@ ...@@ -416,6 +428,7 @@
], 'It should be possible to look for a translated string with operator ='); ], 'It should be possible to look for a translated string with operator =');
// XXX not implemented yet
// doc_list = docList(); // doc_list = docList();
// complex_queries.QueryFactory.create({ // complex_queries.QueryFactory.create({
// type: 'simple', // type: 'simple',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment