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