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) {
* @optional
*/
this.operator = spec.operator || "=";
this._spec = spec.operator;
/**
* Key of the object which refers to the value to compare
......@@ -92,7 +91,7 @@ var checkKey = function (key) {
switch (prop) {
case 'read_from':
case 'cast_to':
case 'default_match':
case 'equal_match':
break;
default:
throw new TypeError("Custom key has unknown property '" +
......@@ -108,7 +107,7 @@ var checkKey = function (key) {
*/
SimpleQuery.prototype.match = function (item, wildcard_character) {
var object_value = null,
default_match = null,
equal_match = null,
cast_to = null,
matchMethod = null,
value = null,
......@@ -124,20 +123,17 @@ SimpleQuery.prototype.match = function (item, wildcard_character) {
checkKey(key);
object_value = item[key.read_from];
default_match = key.default_match;
equal_match = key.equal_match;
// default_match can be a string
if (typeof default_match === 'string') {
// XXX raise error if default_match not in match_lookup
default_match = this._key_schema.match_lookup[default_match];
// equal_match can be a string
if (typeof equal_match === 'string') {
// XXX raise error if equal_match not in match_lookup
equal_match = this._key_schema.match_lookup[equal_match];
}
// default_match overrides the default '=' operator
matchMethod = (default_match || matchMethod);
// but an explicit operator: parameter overrides default_match
if (this._spec && this._spec.operator) {
matchMethod = this[this.operator];
// equal_match overrides the default '=' operator
if (equal_match !== undefined) {
matchMethod = (this.operator === '=') ? equal_match : matchMethod;
}
value = this.value;
......
......@@ -58,7 +58,7 @@
key_set: {
case_insensitive_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
return (object_value.toLowerCase() === value.toLowerCase());
}
......@@ -66,21 +66,21 @@
date_day: {
read_from: 'date',
cast_to: 'dateType',
default_match: 'sameDay'
equal_match: 'sameDay'
},
date_month: {
read_from: 'date',
cast_to: 'dateType',
default_match: 'sameMonth'
equal_match: 'sameMonth'
},
date_year: {
read_from: 'date',
cast_to: 'dateType',
default_match: 'sameYear'
equal_match: 'sameYear'
},
translated_state: {
read_from: 'state',
default_match: 'equalState'
equal_match: 'equalState'
}
}
};
......
......@@ -31,7 +31,7 @@
},
case_insensitive_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());
}
}
......@@ -105,17 +105,17 @@
day: {
read_from: 'date',
cast_to: dateCast,
default_match: sameDay
equal_match: sameDay
},
month: {
read_from: 'date',
cast_to: dateCast,
default_match: sameMonth
equal_match: sameMonth
},
year: {
read_from: 'date',
cast_to: dateCast,
default_match: sameYear
equal_match: sameYear
}
};
......@@ -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 () {
return [
{'identifier': '1', 'date': '2013-01-01'},
......@@ -261,7 +261,7 @@
mydate: {
read_from: 'date',
cast_to: dateCast,
default_match: function alwaysTrue() { return true; }
equal_match: function alwaysTrue() { return true; }
}
};
......@@ -282,13 +282,25 @@
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '>=',
operator: '=',
value: '2013-02-02'
}).exec(doc_list);
deepEqual(doc_list, [
{'identifier': '1', 'date': '2013-01-01'},
{'identifier': '2', 'date': '2013-02-02'},
{'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');
});
......@@ -388,7 +400,7 @@
keys = {
translated_state: {
read_from: 'state',
default_match: equalState
equal_match: equalState
}
};
......@@ -416,6 +428,7 @@
], 'It should be possible to look for a translated string with operator =');
// XXX not implemented yet
// doc_list = docList();
// complex_queries.QueryFactory.create({
// 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