Commit 7f4bdc7f authored by Marco Mariani's avatar Marco Mariani

support for key_schema.types (castTo) and comparators (defaultMatch)

parent 0dffe5c1
......@@ -15,6 +15,13 @@
function SimpleQuery(spec, key_schema) {
Query.call(this);
// XXX check for correctness of key_schema:
// XXX 'keys' must exist
// XXX 'types' is optional
// XXX 'comparators' is optional
// XXX anything else is invalid
// XXX each key can have readFrom, castTo, defaultMatch
// (can be checked in the match function)
this._key_schema = key_schema || {};
/**
......@@ -52,19 +59,33 @@ inherits(SimpleQuery, Query);
* #crossLink "Query/match:method"
*/
SimpleQuery.prototype.match = function (item, wildcard_character) {
var object_value = null, matchMethod = null, value = null, key = this.key;
var object_value = null,
defaultMatch = null,
castTo = null,
matchMethod = null,
value = null,
key = this.key;
matchMethod = this[this.operator];
if (this._key_schema[key] !== undefined) {
key = this._key_schema[key];
if (this._key_schema.keys && this._key_schema.keys[key] !== undefined) {
key = this._key_schema.keys[key];
}
if (typeof key === 'object') {
object_value = item[key.readFrom];
// defaultMatch overrides the default '=' operator
matchMethod = (key.defaultMatch || matchMethod);
defaultMatch = key.defaultMatch;
// defaultMatch can be a string
if (typeof defaultMatch === 'string') {
// XXX raise error if defaultMatch not in comparators
defaultMatch = this._key_schema.comparators[defaultMatch];
}
// defaultMatch overrides the default '=' operator
matchMethod = (defaultMatch || matchMethod);
// but an explicit operator: key overrides DefaultMatch
if (this._spec && this._spec.operator) {
......@@ -72,9 +93,16 @@ SimpleQuery.prototype.match = function (item, wildcard_character) {
}
value = this.value;
if (key.castTo) {
value = key.castTo(value);
object_value = key.castTo(object_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];
}
value = castTo(value);
object_value = castTo(object_value);
}
} else {
object_value = item[key];
......
......@@ -15,70 +15,73 @@
}(['complex_queries', 'qunit'], function (complex_queries) {
"use strict";
var dateType = function (obj) {
var translationEqualityMatcher = function (data) {
return function (object_value, value) {
value = data[value];
return (object_value === value);
};
};
/*jslint unparam: true*/
var key_schema = {
types: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
};
}
},
var sameDay = function (a, b) {
comparators: {
sameDay: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth()) &&
(a.getDay() === b.getDay())
);
};
var sameMonth = function (a, b) {
},
sameMonth: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth())
);
};
var sameYear = function (a, b) {
},
sameYear: function (a, b) {
return (a.getFullYear() === b.getFullYear());
};
var translationEqualityMatcher = function (data) {
return function (object_value, value) {
value = data[value];
return (object_value === value);
};
};
var equalState = translationEqualityMatcher({'ouvert': 'open'});
},
equalState: translationEqualityMatcher({'ouvert': 'open'})
},
/*jslint unparam: true*/
var key_schema = {
keys: {
case_insensitive_identifier: {
readFrom: 'identifier',
defaultMatch: 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
castTo: 'dateType',
defaultMatch: 'sameDay'
},
date_month: {
readFrom: 'date',
castTo: dateType,
defaultMatch: sameMonth
castTo: 'dateType',
defaultMatch: 'sameMonth'
},
date_year: {
readFrom: 'date',
castTo: dateType,
defaultMatch: sameYear
castTo: 'dateType',
defaultMatch: 'sameYear'
},
translated_state: {
readFrom: 'state',
defaultMatch: equalState
defaultMatch: 'equalState'
}
}
};
/*jslint unparam: false*/
......
......@@ -69,19 +69,25 @@
}
var dateType = function (obj) {
var key_schema = {
types: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
}, key_schema = {
}
},
keys: {
mydate: {
readFrom: 'date',
castTo: dateType
castTo: 'dateType'
}
}
};
test("AllDocs", function () {
expect(3);
var o = {}, jio = jIO.createJIO({
......
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