[api test] Added `.sources` option for `nconf.Provider` for readonly configuration data

master
indexzero 2011-09-25 00:01:44 -04:00
parent 0234e17804
commit d0aee0d451
2 changed files with 46 additions and 10 deletions

View File

@ -30,6 +30,7 @@ var Provider = exports.Provider = function (options) {
this._env  = options.env || false; this._env  = options.env || false;
this._reserved = Object.keys(Provider.prototype); this._reserved = Object.keys(Provider.prototype);
this._stores = []; this._stores = [];
this.sources = [];
// //
// Add the default `system` store for working with // Add the default `system` store for working with
@ -38,6 +39,10 @@ var Provider = exports.Provider = function (options) {
// //
this.add('system', options); this.add('system', options);
//
// Add any stores passed in through the options
// to this instance.
//
if (options.type) { if (options.type) {
this.add(options.type, options); this.add(options.type, options);
} }
@ -47,7 +52,20 @@ var Provider = exports.Provider = function (options) {
else if (options.stores) { else if (options.stores) {
Object.keys(options.stores).forEach(function (name) { Object.keys(options.stores).forEach(function (name) {
var store = options.stores[name]; var store = options.stores[name];
self.add(store.name || store.type, store); self.add(store.name || name || store.type, store);
});
}
//
// Add any read-only sources to this instance
//
if (options.source) {
this.sources.push(this.create(options.source.type || options.source.name, options.source));
}
else if (options.sources) {
Object.keys(options.sources).forEach(function (name) {
var source = options.sources[name];
self.sources.push(self.create(source.type || source.name || name, source));
}); });
} }
}; };
@ -122,6 +140,8 @@ Provider.prototype.add = function (name, options) {
if (this[name].loadSync) { if (this[name].loadSync) {
this[name].loadSync(); this[name].loadSync();
} }
return this;
}; };
// //
@ -141,6 +161,7 @@ Provider.prototype.remove = function (name) {
delete this[name]; delete this[name];
this._stores.splice(this._stores.indexOf(name), 1); this._stores.splice(this._stores.indexOf(name), 1);
return this;
}; };
// //
@ -270,11 +291,11 @@ Provider.prototype.merge = function () {
// Responds with an Object representing all keys associated in this instance. // Responds with an Object representing all keys associated in this instance.
// //
Provider.prototype.load = function (callback) { Provider.prototype.load = function (callback) {
var self = this; var self = this,
stores = this._stores.map(function (name) { return self[name] })
.concat(this.sources);
function loadStoreSync(name) { function loadStoreSync(store) {
var store = self[name];
if (!store.loadSync) { if (!store.loadSync) {
throw new Error('nconf store ' + store.type + ' has no loadSync() method'); throw new Error('nconf store ' + store.type + ' has no loadSync() method');
} }
@ -282,9 +303,7 @@ Provider.prototype.load = function (callback) {
return store.loadSync(); return store.loadSync();
} }
function loadStore(name, next) { function loadStore(store, next) {
var store = self[name];
if (!store.load && !store.loadSync) { if (!store.load && !store.loadSync) {
return next(new Error('nconf store ' + store.type + ' has no load() method')); return next(new Error('nconf store ' + store.type + ' has no load() method'));
} }
@ -300,10 +319,10 @@ Provider.prototype.load = function (callback) {
// then do so. // then do so.
// //
if (!callback) { if (!callback) {
return common.merge(this._stores.map(loadStoreSync)); return common.merge(stores.map(loadStoreSync));
} }
async.map(this._stores, loadStore, function (err, objs) { async.map(stores, loadStore, function (err, objs) {
return err ? callback(err) : callback(null, common.merge(objs)); return err ? callback(err) : callback(null, common.merge(objs));
}); });
}; };

View File

@ -64,6 +64,23 @@ vows.describe('nconf/provider').addBatch({
provider.merge(override); provider.merge(override);
helpers.assertMerged(null, provider.file.store); helpers.assertMerged(null, provider.file.store);
} }
},
"when sources are passed in": {
topic: new nconf.Provider({
sources: {
user: {
type: 'file',
file: files[0]
},
global: {
type: 'file',
file: files[1]
}
}
}),
"should have the result merged in": function (provider) {
helpers.assertMerged(null, provider.load());
}
} }
} }
} }