Commit c76af5ed authored by Marco Mariani's avatar Marco Mariani

custom keys: respect naming conventions

parent 749d779e
......@@ -10,17 +10,17 @@ var checkKeySchema = function (key_schema) {
throw new TypeError("SimpleQuery().create(): " +
"key_schema is not of type 'object'");
}
// keys is mandatory
if (key_schema.keys === undefined) {
// key_set is mandatory
if (key_schema.key_set === undefined) {
throw new TypeError("SimpleQuery().create(): " +
"key_schema has no 'keys' property");
"key_schema has no 'key_set' property");
}
for (prop in key_schema) {
if (key_schema.hasOwnProperty(prop)) {
switch (prop) {
case 'keys':
case 'types':
case 'comparators':
case 'key_set':
case 'cast_lookup':
case 'match_lookup':
break;
default:
throw new TypeError("SimpleQuery().create(): " +
......@@ -83,16 +83,16 @@ inherits(SimpleQuery, Query);
var checkKey = function (key) {
var prop;
if (key.readFrom === undefined) {
throw new TypeError("Custom key is missing the readFrom property");
if (key.read_from === undefined) {
throw new TypeError("Custom key is missing the read_from property");
}
for (prop in key) {
if (key.hasOwnProperty(prop)) {
switch (prop) {
case 'readFrom':
case 'castTo':
case 'defaultMatch':
case 'read_from':
case 'cast_to':
case 'default_match':
break;
default:
throw new TypeError("Custom key has unknown property '" +
......@@ -108,50 +108,50 @@ var checkKey = function (key) {
*/
SimpleQuery.prototype.match = function (item, wildcard_character) {
var object_value = null,
defaultMatch = null,
castTo = null,
default_match = null,
cast_to = null,
matchMethod = null,
value = null,
key = this.key;
matchMethod = this[this.operator];
if (this._key_schema.keys && this._key_schema.keys[key] !== undefined) {
key = this._key_schema.keys[key];
if (this._key_schema.key_set && this._key_schema.key_set[key] !== undefined) {
key = this._key_schema.key_set[key];
}
if (typeof key === 'object') {
checkKey(key);
object_value = item[key.readFrom];
object_value = item[key.read_from];
// defaultMatch overrides the default '=' operator
defaultMatch = key.defaultMatch;
// default_match overrides the default '=' operator
default_match = key.default_match;
// defaultMatch can be a string
if (typeof defaultMatch === 'string') {
// XXX raise error if defaultMatch not in comparators
defaultMatch = this._key_schema.comparators[defaultMatch];
// 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];
}
// defaultMatch overrides the default '=' operator
matchMethod = (defaultMatch || matchMethod);
// default_match overrides the default '=' operator
matchMethod = (default_match || matchMethod);
// but an explicit operator: key overrides DefaultMatch
// but an explicit operator: key overrides default_match
if (this._spec && this._spec.operator) {
matchMethod = this[this.operator];
}
value = this.value;
castTo = key.castTo;
if (castTo) {
// castTo can be a string
if (typeof castTo === 'string') {
// XXX raise error if castTo not in types
castTo = this._key_schema.types[castTo];
cast_to = key.cast_to;
if (cast_to) {
// cast_to can be a string
if (typeof cast_to === 'string') {
// XXX raise error if cast_to not in cast_lookup
cast_to = this._key_schema.cast_lookup[cast_to];
}
value = castTo(value);
object_value = castTo(object_value);
value = cast_to(value);
object_value = cast_to(object_value);
}
} else {
object_value = item[key];
......
......@@ -25,7 +25,7 @@
/*jslint unparam: true*/
var key_schema = {
types: {
cast_lookup: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
......@@ -35,7 +35,7 @@
}
},
comparators: {
match_lookup: {
sameDay: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
......@@ -55,32 +55,32 @@
equalState: translationEqualityMatcher({'ouvert': 'open'})
},
keys: {
key_set: {
case_insensitive_identifier: {
readFrom: 'identifier',
defaultMatch: function (object_value, value, wildcard_character) {
read_from: 'identifier',
default_match: function (object_value, value, wildcard_character) {
// XXX do this with a regexp and wildcard support
return (object_value.toLowerCase() === value.toLowerCase());
}
},
date_day: {
readFrom: 'date',
castTo: 'dateType',
defaultMatch: 'sameDay'
read_from: 'date',
cast_to: 'dateType',
default_match: 'sameDay'
},
date_month: {
readFrom: 'date',
castTo: 'dateType',
defaultMatch: 'sameMonth'
read_from: 'date',
cast_to: 'dateType',
default_match: 'sameMonth'
},
date_year: {
readFrom: 'date',
castTo: 'dateType',
defaultMatch: 'sameYear'
read_from: 'date',
cast_to: 'dateType',
default_match: 'sameYear'
},
translated_state: {
readFrom: 'state',
defaultMatch: 'equalState'
read_from: 'state',
default_match: 'equalState'
}
}
};
......@@ -176,7 +176,7 @@
{'identifier': '100', 'number': '100'}
];
}, key_schema = {
types: {
cast_lookup: {
intType: function (value) {
if (typeof value === 'string') {
return parseInt(value, 10);
......@@ -184,10 +184,10 @@
return value;
}
},
keys: {
key_set: {
number: {
readFrom: 'number',
castTo: 'intType'
read_from: 'number',
cast_to: 'intType'
}
}
};
......
......@@ -60,16 +60,16 @@
try {
complex_queries.QueryFactory.create({type: 'simple'}, {});
ok(false, 'key_schema.keys is not checked');
ok(false, 'key_schema.key_set is not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"SimpleQuery().create(): key_schema has no 'keys' property",
"SimpleQuery().create(): key_schema has no 'key_set' property",
'wrong exception message');
}
try {
complex_queries.QueryFactory.create({type: 'simple'}, {keys: {}, foobar: {}});
complex_queries.QueryFactory.create({type: 'simple'}, {key_set: {}, foobar: {}});
ok(false, 'unknown key_schema properties are not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
......@@ -92,11 +92,11 @@
key: {},
value: 'a'
}).exec(doc_list);
ok(false, 'key.readFrom is not checked');
ok(false, 'key.read_from is not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"Custom key is missing the readFrom property",
"Custom key is missing the read_from property",
'wrong exception message');
}
......@@ -104,7 +104,7 @@
complex_queries.QueryFactory.create({
type: 'simple',
key: {
readFrom: 'identifier',
read_from: 'identifier',
foobar: ''
},
value: 'a'
......
......@@ -17,7 +17,7 @@
module('Custom Key Queries');
test('Simple Key with readFrom', function () {
test('Simple Key with read_from', function () {
/*jslint unparam: true*/
var doc_list, docList = function () {
return [
......@@ -27,11 +27,11 @@
];
}, keys = {
title: {
readFrom: 'identifier'
read_from: 'identifier'
},
case_insensitive_identifier: {
readFrom: 'identifier',
defaultMatch: function (object_value, value, wildcard_character) {
read_from: 'identifier',
default_match: function (object_value, value, wildcard_character) {
return (object_value.toLowerCase() === value.toLowerCase());
}
}
......@@ -103,19 +103,19 @@
var keys = {
day: {
readFrom: 'date',
castTo: dateCast,
defaultMatch: sameDay
read_from: 'date',
cast_to: dateCast,
default_match: sameDay
},
month: {
readFrom: 'date',
castTo: dateCast,
defaultMatch: sameMonth
read_from: 'date',
cast_to: dateCast,
default_match: sameMonth
},
year: {
readFrom: 'date',
castTo: dateCast,
defaultMatch: sameYear
read_from: 'date',
cast_to: dateCast,
default_match: sameYear
}
};
......@@ -173,8 +173,8 @@
];
}, keys = {
mydate: {
readFrom: 'date',
castTo: dateCast
read_from: 'date',
cast_to: dateCast
}
};
......@@ -250,7 +250,7 @@
});
test('Simple Key with both defaultMatch and operator attributes', function () {
test('Simple Key with both default_match and operator attributes', function () {
var doc_list, docList = function () {
return [
{'identifier': '1', 'date': '2013-01-01'},
......@@ -259,9 +259,9 @@
];
}, keys = {
mydate: {
readFrom: 'date',
castTo: dateCast,
defaultMatch: function alwaysTrue() { return true; }
read_from: 'date',
cast_to: dateCast,
default_match: function alwaysTrue() { return true; }
}
};
......@@ -315,8 +315,8 @@
complex_queries.QueryFactory.create({
type: 'simple',
key: {
readFrom: 'number',
castTo: intType
read_from: 'number',
cast_to: intType
},
operator: '>',
value: '19'
......@@ -329,8 +329,8 @@
complex_queries.QueryFactory.create({
type: 'simple',
key: {
readFrom: 'number',
castTo: intType
read_from: 'number',
cast_to: intType
},
operator: '<',
value: '19'
......@@ -346,16 +346,16 @@
query_list: [{
type: 'simple',
key: {
readFrom: 'number',
castTo: intType
read_from: 'number',
cast_to: intType
},
operator: '<',
value: '19'
}, {
type: 'simple',
key: {
readFrom: 'number',
castTo: intType
read_from: 'number',
cast_to: intType
},
operator: '=',
value: '19'
......@@ -387,8 +387,8 @@
equalState = translationEqualityMatcher({'ouvert': 'open'}),
keys = {
translated_state: {
readFrom: 'state',
defaultMatch: equalState
read_from: 'state',
default_match: equalState
}
};
......@@ -479,8 +479,8 @@
];
}, keys = {
identifier: {
readFrom: 'identifier',
defaultMatch: function (object_value, value, wildcard_character) {
read_from: 'identifier',
default_match: function (object_value, value, wildcard_character) {
// XXX todo: regexp & support wildcard_character
return accentFold(object_value) === accentFold(value);
}
......
......@@ -70,7 +70,7 @@
var key_schema = {
types: {
cast_lookup: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
......@@ -79,10 +79,10 @@
return new Date(obj);
}
},
keys: {
key_set: {
mydate: {
readFrom: 'date',
castTo: 'dateType'
read_from: 'date',
cast_to: 'dateType'
}
}
};
......
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