Skip to content

Commit

Permalink
allow different separator for memorystore
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Apr 5, 2013
1 parent 818526c commit 039057c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
14 changes: 12 additions & 2 deletions lib/nconf/common.js
Expand Up @@ -19,8 +19,9 @@ var common = exports;
// If given null or undefined it should return an empty path.
// '' should still be respected as a path.
//
common.path = function (key) {
return key == null ? [] : key.split(':');
common.path = function (key, separator) {
separator = separator || ':';
return key == null ? [] : key.split(separator);
};

//
Expand All @@ -31,6 +32,15 @@ common.key = function () {
return Array.prototype.slice.call(arguments).join(':');
};

//
// ### function key (arguments)
// Returns a joined string from the `arguments`,
// first argument is the join delimiter.
//
common.keyed = function () {
return Array.prototype.slice.call(arguments, 1).join(arguments[0]);
};

//
// ### function loadFiles (files, callback)
// #### @files {Object|Array} List of files (or settings object) to load.
Expand Down
11 changes: 6 additions & 5 deletions lib/nconf/stores/memory.js
Expand Up @@ -22,6 +22,7 @@ var Memory = exports.Memory = function (options) {
this.mtimes = {};
this.readOnly = false;
this.loadFrom = options.loadFrom || null;
this.logicalSeparator = options.logicalSeparator || ':';

if (this.loadFrom) {
this.store = common.loadFilesSync(this.loadFrom);
Expand All @@ -35,7 +36,7 @@ var Memory = exports.Memory = function (options) {
//
Memory.prototype.get = function (key) {
var target = this.store,
path = common.path(key);
path = common.path(key, this.logicalSeparator);

//
// Scope into the object to get the appropriate nested context
Expand Down Expand Up @@ -64,7 +65,7 @@ Memory.prototype.set = function (key, value) {
}

var target = this.store,
path = common.path(key);
path = common.path(key, this.logicalSeparator);

if (path.length === 0) {
//
Expand Down Expand Up @@ -115,7 +116,7 @@ Memory.prototype.clear = function (key) {

var target = this.store,
value = target,
path = common.path(key);
path = common.path(key, this.logicalSeparator);

//
// Remove the key from the set of `mtimes` (modified times)
Expand Down Expand Up @@ -163,7 +164,7 @@ Memory.prototype.merge = function (key, value) {

var self = this,
target = this.store,
path = common.path(key),
path = common.path(key, this.logicalSeparator),
fullKey = key;

//
Expand Down Expand Up @@ -197,7 +198,7 @@ Memory.prototype.merge = function (key, value) {
}

return Object.keys(value).every(function (nested) {
return self.merge(common.key(fullKey, nested), value[nested]);
return self.merge(common.keyed(self.logicalSeparator, fullKey, nested), value[nested]);
});
};

Expand Down
2 changes: 2 additions & 0 deletions test/helpers.js
Expand Up @@ -13,6 +13,8 @@ var assert = require('assert'),
nconf = require('../lib/nconf');

exports.assertMerged = function (err, merged) {
console.log(merged);

merged = merged instanceof nconf.Provider
? merged.store.store
: merged;
Expand Down
17 changes: 17 additions & 0 deletions test/stores/memory-store-test.js
Expand Up @@ -104,5 +104,22 @@ vows.describe('nconf/stores/memory').addBatch({
assert.equal(store.get('merge:object:prop4').length, 2);
}
}
},
"When using the nconf memory store with different logical separator": {
topic: new nconf.Memory({logicalSeparator: '||' }),
"when storing with : (colon)": {
"should store the config atomicly": function (store) {
store.set('foo:bar:bazz', 'buzz');
assert.isTrue(typeof store.get('foo:bar') === 'undefined');
assert.equal(store.get('foo:bar:bazz'), 'buzz');
}
},
"when storing with separator": {
"should be able to read the object": function (store) {
store.set('foo||bar||bazz', 'buzz');
assert.equal(store.get('foo||bar').bazz, 'buzz');
assert.equal(store.get('foo').bar.bazz, 'buzz');
}
}
}
}).export(module);

0 comments on commit 039057c

Please sign in to comment.