Permalink
Browse files

Addeed `approve` to the public API. Translations are not approved by …

…default
  • Loading branch information...
1 parent 2a808b9 commit b2ae70aca00d8319fb98c0ae76c9f3f7fea8f231 @masylum committed Apr 3, 2011
View
@@ -54,7 +54,8 @@ Dialect is a painless nodejs module to manage your translations.
* `config (key, value)`: Exposes configuration values.
* `get (query)`: Gets a translation cached in memory.
* `set (query, translation, callback)`: Sets a translation on the store.
-* `sync (locale, repeat, callback)`: Syncs the store with the memory cache.
+* `approve (approve?, query, callback)`: Approve or rejects a translation.
+* `sync (locale, repeat, callback)`: Syncs all the approved translations of the store to the memory cache.
* `connect (callback)`: Connects to the database store.
### Plurals
@@ -12,6 +12,9 @@ dialect.connect(function () {
dialect.set({original: 'Fight', locale: 'es', context: 'name'}, 'Lucha');
dialect.set({original: 'Fight', locale: 'es', context: 'verb'}, 'Luchar');
+ dialect.approve({original: 'Fight', locale: 'es', context: 'name'}, true);
+ dialect.approve({original: 'Fight', locale: 'es', context: 'verb'}, true);
+
dialect.sync({interval: 3600}, function (err, foo) {
console.log(_(['Fight', {context: 'name'}]));
console.log(_(['Fight', {context: 'verb'}]));
@@ -9,11 +9,12 @@ var dialect = require('..').dialect({
dialect.connect(function () {
console.log(_(original));
+
dialect.set({original: original, locale: 'es'}, translation);
+ dialect.approve({original: original, locale: 'es'}, true);
dialect.sync({interval: 3600}, function (err, foo) {
console.log(_(original));
console.log(_('Inexistant'));
- process.exit();
});
});
View
@@ -14,6 +14,10 @@ dialect.connect(function () {
dialect.set({original: '{count} Beer', locale: 'es', plural: 2}, '{count} pivi');
dialect.set({original: '{count} Beer', locale: 'es', plural: 3}, '{count} piva');
+ dialect.approve({original: '{count} Beer', locale: 'es', plural: 1}, true);
+ dialect.approve({original: '{count} Beer', locale: 'es', plural: 2}, true);
+ dialect.approve({original: '{count} Beer', locale: 'es', plural: 3}, true);
+
dialect.sync({interval: 3600}, function (err, foo) {
[1, 2, 3].forEach(function (i) {
console.log(_(['{count} Beer', '{count} Beers', {count: i}]));
View
@@ -108,6 +108,30 @@ module.exports = function (options) {
};
/**
+ * Approves or rejects a translation
+ *
+ * @param {Object} query {original, locale [, count] [, context]}
+ * @param {Boolean} aproved
+ * @param {Function} callback.
+ *
+ * @return dialect
+ */
+ DIALECT.approve = function (query, approved, cb) {
+ if (!query || !query.original || !query.locale) {
+ throw Error("Original string and target locale are mandatory");
+ }
+
+ if (typeof approved === 'function' || approved === undefined) {
+ throw Error("Approved is mandatory");
+ }
+
+ // Database
+ DIALECT.store.approve(query, !!approved, cb);
+
+ return DIALECT;
+ };
+
+ /**
* Sets the translation to the store.
*
* @param {Object} query {original, locale [, count] [, context]}
View
@@ -56,7 +56,7 @@ module.exports.IO = function (DIALECT) {
throw Error("You must provide a locale");
}
- DIALECT.store.get({locale: locale}, function (err, data) {
+ DIALECT.store.get({locale: locale, approved: true}, function (err, data) {
var dictionary = {},
key = null,
i = null;
@@ -5,21 +5,30 @@ module.exports = function (options) {
return o && Object.keys(o).length > 0;
},
- _quotize = function (f) {
+ _parse = function (f) {
+ var handleTypes = function (el) {
+ switch (typeof el) {
+ case 'string':
+ return "'" + el + "'";
+ case 'boolean':
+ return el ? 1 : 0;
+ default:
+ return el;
+ }
+ };
+
if (Array.isArray(f)) {
- return f.map(function (e) {
- return typeof e === 'string' ? "'" + e + "'": e;
- });
+ return f.map(handleTypes);
} else {
- return typeof f === 'string' ? "'" + f + "'": f;
+ return handleTypes(f);
}
},
_parseOptions = function (query) {
var result = [];
if (typeof query !== 'object' || query === null) {
- return [(query === null ? ' IS ' : ' = ') + _quotize(query)];
+ return [(query === null ? ' IS ' : ' = ') + _parse(query)];
} else {
Object.keys(query).forEach(function (key) {
var value = query[key], ops;
@@ -31,7 +40,7 @@ module.exports = function (options) {
'$in': ' IN ',
'$nin': ' NOT IN '
};
- result.push(ops[key] + '(' + _quotize(value).join(',') + ')');
+ result.push(ops[key] + '(' + _parse(value).join(',') + ')');
break;
case '$exists':
result.push(' IS ' + (value ? 'NOT ' : '') + 'null');
@@ -48,7 +57,7 @@ module.exports = function (options) {
'$lte': ' <= ',
'$gte': ' >= '
};
- result.push(ops[key] + _quotize(value));
+ result.push(ops[key] + _parse(value));
break;
default:
throw Error('`' + key + '` not implemented yet');
@@ -171,7 +180,7 @@ module.exports = function (options) {
values.push(doc[i]);
}
- sql += ' VALUES (' + _quotize(values).join(', ') + ')';
+ sql += ' VALUES (' + _parse(values).join(', ') + ')';
return sql;
};
@@ -191,19 +200,23 @@ module.exports = function (options) {
updates = [],
_parseUpdate = function (key, value) {
- var i = Object.keys(value)[0],
- v = value[i];
-
- switch (key) {
- case '$set':
- return i + ' = ' + _quotize(v);
- case '$inc':
- if (v !== 0) {
- return i + ' = ' + i + (v > 0 ? ' + ' : ' - ') + v;
- } else {
- return '';
+ var els = [];
+
+ Object.keys(value).forEach(function (i) {
+ var v = value[i];
+
+ switch (key) {
+ case '$set':
+ els.push(i + ' = ' + _parse(v));
+ break;
+ case '$inc':
+ if (v !== 0) {
+ els.push(i + ' = ' + i + (v > 0 ? ' + ' : ' - ') + v);
+ }
}
- }
+ });
+
+ return els.join(', ');
};
SQLizer.sql = 'UPDATE ' + SQLizer.table + ' SET ';
View
@@ -110,6 +110,7 @@ module.exports = function (options) {
if (!data) {
doc.translation = translation;
+ doc.approved = false;
STORE.collection.insert(doc, callback);
} else {
callback(Error('This translation already exists'), null);
@@ -135,12 +136,32 @@ module.exports = function (options) {
callback = _default(callback);
query = query || {};
+ translation.approved = false;
+
STORE.collection.update(query, {'$set': translation}, {upsert: true}, callback);
return STORE;
};
/**
+ * Approve or rejects a translation
+ *
+ * @param {Object} query {original, locale}
+ * @param {String} translation
+ * @param {Function} callback
+ * @return store
+ */
+ STORE.approve = function (query, approved, callback) {
+
+ callback = _default(callback);
+ query = query || {};
+
+ STORE.collection.update(query, {'$set': {approved: approved}}, {}, callback);
+
+ return STORE;
+ };
+
+ /**
* Destroy the translation
*
* @param {Object} query {original, locale}
View
@@ -103,6 +103,7 @@ module.exports = function (options) {
if (!data || data.length === 0) {
doc.translation = translation;
+ doc.approved = false;
STORE.db.execute(sqlizer({table: _table}).insert(doc).sql, callback);
} else {
callback(Error('This translation already exists'), null);
@@ -127,7 +128,28 @@ module.exports = function (options) {
callback = _default(callback);
- STORE.db.execute(sqlizer({table: _table}).update(query, {'$set': {translation: translation}}).sql, callback);
+ STORE.db.execute(sqlizer({table: _table}).update(
+ query,
+ {'$set': {translation: translation, approved: false}}).sql,
+ callback
+ );
+
+ return STORE;
+ };
+
+ /**
+ * Approve or rejects a translation
+ *
+ * @param {Object} query {original, locale}
+ * @param {Boolean} approved
+ * @param {Function} callback
+ * @return store
+ */
+ STORE.approve = function (query, approved, callback) {
+
+ callback = _default(callback);
+
+ STORE.db.execute(sqlizer({table: _table}).update(query, {'$set': {approved: approved}}).sql, callback);
return STORE;
};
View
@@ -47,7 +47,7 @@ testosterone
// config
////////////////////////////////////////////
- .add(' GIVEN a call to config \n' +
+ .add('GIVEN a call to config \n' +
' WHEN just param `key` is given \n' +
' THEN it should return `_option[key]`', function () {
var options = {store: {mongodb: {}}, base_locale: 'en'},
@@ -190,6 +190,68 @@ testosterone
})
////////////////////////////////////////////
+ // approve
+ ////////////////////////////////////////////
+
+ .add('GIVEN a call to `approve` \n' +
+ ' WHEN `query.original` or `query.locale` are missing \n' +
+ ' THEN an error should be thrown', function () {
+
+ var options = {locales: ['en', 'es'], store: {mongodb: {}}},
+ d = dialect(options);
+
+ assert.throws(function () {
+ d.approve(null, true);
+ });
+
+ assert.throws(function () {
+ d.approve({original: 'foo'}, true);
+ });
+
+ assert.throws(function () {
+ d.approve({locale: 'foo'}, true);
+ });
+
+ assert.throws(function () {
+ d.approve({original: 'foo', locale: 'foo'}, true);
+ });
+ })
+
+ .add(' WHEN `query.original` and `query.locale` are valid \n' +
+ ' THEN should approve the translation to the `store`', function () {
+
+ var store = {mongodb: {}},
+ query = {original: 'hello', locale: 'foo'},
+ translation = 'foola',
+ options = {locales: ['en', 'es'], store: store},
+ callback = function (err, data) {
+ assert.equal(err, 'foo');
+ assert.equal(data, 'bar');
+ },
+ d = dialect(options);
+
+ gently.expect(d.store, 'approve', 2, function (q, u, cb) {
+ assert.deepEqual(q, query);
+ assert.deepEqual(u, true);
+ assert.deepEqual(cb, callback);
+ cb('foo', 'bar');
+ });
+
+ d.approve({original: 'hello', locale: 'foo'}, true, callback);
+ d.approve({original: 'hello', locale: 'foo'}, 'fleiba', callback);
+
+ gently.expect(d.store, 'approve', 2, function (q, u, cb) {
+ assert.deepEqual(q, query);
+ assert.deepEqual(u, false);
+ assert.deepEqual(cb, callback);
+ cb('foo', 'bar');
+ });
+
+ d.approve({original: 'hello', locale: 'foo'}, '', callback);
+ d.approve({original: 'hello', locale: 'foo'}, false, callback);
+ })
+
+ ////////////////////////////////////////////
// get
////////////////////////////////////////////
View
@@ -51,7 +51,7 @@ testosterone
' THEN it should get the dictionary from the store \n' +
' AND cache it on memory', function () {
gently.expect(dialect.store, 'get', function (query, cb) {
- assert.deepEqual(query, {locale: 'es'});
+ assert.deepEqual(query, {locale: 'es', approved: true});
assert.ok(query, cb);
cb(null, [{original: 'hello', translation: 'hola'}]);
});
@@ -65,7 +65,7 @@ testosterone
// with plural and contexts
gently.expect(dialect.store, 'get', function (query, cb) {
- assert.deepEqual(query, {locale: 'es'});
+ assert.deepEqual(query, {locale: 'es', approved: true});
assert.ok(query, cb);
cb(null, [{original: 'hello', translation: 'hola', context: 'salute', plural: 1}]);
});
View
@@ -12,6 +12,8 @@ testosterone
.add('parseDoc', function () {
assert.equal(sqlizer.parseDoc({j: 4}), 'j = 4');
assert.equal(sqlizer.parseDoc({j: '4'}), "j = '4'");
+ assert.equal(sqlizer.parseDoc({j: true}), "j = 1");
+ assert.equal(sqlizer.parseDoc({j: false}), "j = 0");
assert.equal(sqlizer.parseDoc({j: {'$exists': true}, f: null}), 'j IS NOT null AND f IS null');
assert.equal(sqlizer.parseDoc({j: {'$exists': false}}), 'j IS null');
assert.equal(sqlizer.parseDoc({j: {'$ne': null}}), 'j IS NOT null');
@@ -80,6 +82,7 @@ testosterone
.add('update', function () {
assert.equal(sqlizer.update({}, {'$set': {j: 4}}).sql, 'UPDATE test SET j = 4');
+ assert.equal(sqlizer.update({}, {'$set': {j: 4, i: false}}).sql, 'UPDATE test SET j = 4, i = 0');
assert.equal(sqlizer.update({}, {'$inc': {j: 2}}).sql, 'UPDATE test SET j = j + 2');
assert.equal(sqlizer.update({d: 'foo'}, {'$set': {j: 'bar'}}).sql, "UPDATE test SET j = 'bar' WHERE d = 'foo'");
assert.equal(sqlizer.update({d: 4, f: 3}, {'$inc': {j: 2}}).sql, 'UPDATE test SET j = j + 2 WHERE d = 4 AND f = 3');
Oops, something went wrong.

0 comments on commit b2ae70a

Please sign in to comment.