diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index a67509f..b0ec3f3 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -24,8 +24,9 @@ var Provider = exports.Provider = function (options) { // Setup default options for working with `stores`, // `overrides`, `process.env` and `process.argv`. // - options = options || {}; - this.sources = []; + options = options || {}; + this._stores = {}; + this._sources = []; // // Add any stores passed in through the options @@ -43,6 +44,19 @@ var Provider = exports.Provider = function (options) { 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)); + }); + } }; // @@ -86,7 +100,7 @@ Provider.prototype.use = function (name, options) { }); } - var store = this.sources[name], + var store = this._stores[name], update = store && !sameOptions(store); if (!store || update) { @@ -118,10 +132,10 @@ Provider.prototype.add = function (name, options) { throw new Error('Cannot add store with unknown type: ' + type); } - this.sources[name] = this.create(type, options); + this._stores[name] = this.create(type, options); - if (this.sources[name].loadSync) { - this.sources[name].loadSync(); + if (this._stores[name].loadSync) { + this._stores[name].loadSync(); } return this; @@ -135,7 +149,7 @@ Provider.prototype.add = function (name, options) { // this was used in the call to `.add()`. // Provider.prototype.remove = function (name) { - delete this.sources[name]; + delete this._stores[name]; return this; }; @@ -171,14 +185,14 @@ Provider.prototype.get = function (key, callback) { // the entire set of stores, but up until there is a defined value. // var current = 0, - names = Object.keys(this.sources), + names = Object.keys(this._stores), self = this, response; async.whilst(function () { return typeof response === 'undefined' && current < names.length; }, function (next) { - var store = self.sources[names[current]]; + var store = self._stores[names[current]]; current++; if (store.get.length >= 2) { @@ -268,7 +282,7 @@ Provider.prototype.merge = function () { // Provider.prototype.load = function (callback) { var self = this, - stores = Object.keys(this.sources).map(function (name) { return self.sources[name] }); + stores = Object.keys(this._stores).map(function (name) { return self._stores[name] }); function loadStoreSync(store) { if (!store.loadSync) { @@ -317,11 +331,11 @@ Provider.prototype.load = function (callback) { // then do so. // if (!callback) { - mergeSources(loadBatch(self.sources)); + mergeSources(loadBatch(self._sources)); return loadBatch(stores); } - loadBatch(self.sources, function (err, data) { + loadBatch(self._sources, function (err, data) { if (err) { return callback(err); } @@ -331,7 +345,7 @@ Provider.prototype.load = function (callback) { }); } - return self.sources.length + return self._sources.length ? loadSources() : loadBatch(stores, callback); }; @@ -350,10 +364,10 @@ Provider.prototype.save = function (value, callback) { } var self = this, - names = Object.keys(this.sources); + names = Object.keys(this._stores); function saveStoreSync(name) { - var store = self.sources[name]; + var store = self._stores[name]; if (!store.saveSync) { throw new Error('nconf store ' + store.type + ' has no saveSync() method'); @@ -363,7 +377,7 @@ Provider.prototype.save = function (value, callback) { } function saveStore(name, next) { - var store = self.sources[name]; + var store = self._stores[name]; if (!store.save && !store.saveSync) { return next(new Error('nconf store ' + store.type + ' has no save() method')); @@ -404,7 +418,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) { response; function runAction (name, next) { - var store = self.sources[name]; + var store = self._stores[name]; if (destructive && store.readOnly) { return next(); @@ -416,15 +430,15 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) { } if (callback) { - return async.forEach(Object.keys(this.sources), runAction, function (err) { + return async.forEach(Object.keys(this._stores), runAction, function (err) { return err ? callback(err) : callback(); }); } - Object.keys(this.sources).forEach(function (name) { + Object.keys(this._stores).forEach(function (name) { if (typeof response === 'undefined') { - var store = self.sources[name]; + var store = self._stores[name]; if (destructive && store.readOnly) { return; diff --git a/test/fixtures/scripts/nconf-change-argv.js b/test/fixtures/scripts/nconf-change-argv.js index 1648da2..1aaed1a 100644 --- a/test/fixtures/scripts/nconf-change-argv.js +++ b/test/fixtures/scripts/nconf-change-argv.js @@ -11,6 +11,6 @@ var nconf = require('../../../lib/nconf').argv(); // Remove 'badValue', 'evenWorse' and 'OHNOEZ' // process.argv.splice(3, 3); -nconf.sources['argv'].loadArgv(); +nconf._stores['argv'].loadArgv(); process.stdout.write(nconf.get('something')); diff --git a/test/nconf-test.js b/test/nconf-test.js index 482a543..2cb8a39 100644 --- a/test/nconf-test.js +++ b/test/nconf-test.js @@ -28,7 +28,7 @@ vows.describe('nconf').addBatch({ "the use() method": { "should instaniate the correct store": function () { nconf.use('memory'); - assert.instanceOf(nconf.sources['memory'], nconf.stores.Memory); + assert.instanceOf(nconf._stores['memory'], nconf.stores.Memory); } }, "it should": { diff --git a/test/provider-test.js b/test/provider-test.js index 4b592cb..f2440da 100644 --- a/test/provider-test.js +++ b/test/provider-test.js @@ -24,13 +24,13 @@ vows.describe('nconf/provider').addBatch({ "calling the use() method with the same store type and different options": { topic: new nconf.Provider().use('file', { file: files[0] }), "should use a new instance of the store type": function (provider) { - var old = provider.sources['file']; + var old = provider._stores['file']; - assert.equal(provider.sources.file.file, files[0]); + assert.equal(provider._stores.file.file, files[0]); provider.use('file', { file: files[1] }); - assert.notStrictEqual(old, provider.sources.file); - assert.equal(provider.sources.file.file, files[1]); + assert.notStrictEqual(old, provider._stores.file); + assert.equal(provider._stores.file.file, files[1]); } }, "when 'argv' is true": helpers.assertSystemConf({ @@ -71,7 +71,7 @@ vows.describe('nconf/provider').addBatch({ "should have the result merged in": function (provider) { provider.load(); provider.merge(override); - helpers.assertMerged(null, provider.sources.file.store); + helpers.assertMerged(null, provider._stores.file.store); } } }