Skip to content

Commit c3cebe7

Browse files
committed
[refactor] Rename .sources to ._stores and bring back ._sources
1 parent 78ce556 commit c3cebe7

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

lib/nconf/provider.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ var Provider = exports.Provider = function (options) {
2424
// Setup default options for working with `stores`,
2525
// `overrides`, `process.env` and `process.argv`.
2626
//
27-
options = options || {};
28-
this.sources = [];
27+
options = options || {};
28+
this._stores = {};
29+
this._sources = [];
2930

3031
//
3132
// Add any stores passed in through the options
@@ -43,6 +44,19 @@ var Provider = exports.Provider = function (options) {
4344
self.add(store.name || name || store.type, store);
4445
});
4546
}
47+
48+
//
49+
// Add any read-only sources to this instance
50+
//
51+
if (options.source) {
52+
this._sources.push(this.create(options.source.type || options.source.name, options.source));
53+
}
54+
else if (options.sources) {
55+
Object.keys(options.sources).forEach(function (name) {
56+
var source = options.sources[name];
57+
self._sources.push(self.create(source.type || source.name || name, source));
58+
});
59+
}
4660
};
4761

4862
//
@@ -86,7 +100,7 @@ Provider.prototype.use = function (name, options) {
86100
});
87101
}
88102

89-
var store = this.sources[name],
103+
var store = this._stores[name],
90104
update = store && !sameOptions(store);
91105

92106
if (!store || update) {
@@ -118,10 +132,10 @@ Provider.prototype.add = function (name, options) {
118132
throw new Error('Cannot add store with unknown type: ' + type);
119133
}
120134

121-
this.sources[name] = this.create(type, options);
135+
this._stores[name] = this.create(type, options);
122136

123-
if (this.sources[name].loadSync) {
124-
this.sources[name].loadSync();
137+
if (this._stores[name].loadSync) {
138+
this._stores[name].loadSync();
125139
}
126140

127141
return this;
@@ -135,7 +149,7 @@ Provider.prototype.add = function (name, options) {
135149
// this was used in the call to `.add()`.
136150
//
137151
Provider.prototype.remove = function (name) {
138-
delete this.sources[name];
152+
delete this._stores[name];
139153
return this;
140154
};
141155

@@ -171,14 +185,14 @@ Provider.prototype.get = function (key, callback) {
171185
// the entire set of stores, but up until there is a defined value.
172186
//
173187
var current = 0,
174-
names = Object.keys(this.sources),
188+
names = Object.keys(this._stores),
175189
self = this,
176190
response;
177191

178192
async.whilst(function () {
179193
return typeof response === 'undefined' && current < names.length;
180194
}, function (next) {
181-
var store = self.sources[names[current]];
195+
var store = self._stores[names[current]];
182196
current++;
183197

184198
if (store.get.length >= 2) {
@@ -268,7 +282,7 @@ Provider.prototype.merge = function () {
268282
//
269283
Provider.prototype.load = function (callback) {
270284
var self = this,
271-
stores = Object.keys(this.sources).map(function (name) { return self.sources[name] });
285+
stores = Object.keys(this._stores).map(function (name) { return self._stores[name] });
272286

273287
function loadStoreSync(store) {
274288
if (!store.loadSync) {
@@ -317,11 +331,11 @@ Provider.prototype.load = function (callback) {
317331
// then do so.
318332
//
319333
if (!callback) {
320-
mergeSources(loadBatch(self.sources));
334+
mergeSources(loadBatch(self._sources));
321335
return loadBatch(stores);
322336
}
323337

324-
loadBatch(self.sources, function (err, data) {
338+
loadBatch(self._sources, function (err, data) {
325339
if (err) {
326340
return callback(err);
327341
}
@@ -331,7 +345,7 @@ Provider.prototype.load = function (callback) {
331345
});
332346
}
333347

334-
return self.sources.length
348+
return self._sources.length
335349
? loadSources()
336350
: loadBatch(stores, callback);
337351
};
@@ -350,10 +364,10 @@ Provider.prototype.save = function (value, callback) {
350364
}
351365

352366
var self = this,
353-
names = Object.keys(this.sources);
367+
names = Object.keys(this._stores);
354368

355369
function saveStoreSync(name) {
356-
var store = self.sources[name];
370+
var store = self._stores[name];
357371

358372
if (!store.saveSync) {
359373
throw new Error('nconf store ' + store.type + ' has no saveSync() method');
@@ -363,7 +377,7 @@ Provider.prototype.save = function (value, callback) {
363377
}
364378

365379
function saveStore(name, next) {
366-
var store = self.sources[name];
380+
var store = self._stores[name];
367381

368382
if (!store.save && !store.saveSync) {
369383
return next(new Error('nconf store ' + store.type + ' has no save() method'));
@@ -404,7 +418,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
404418
response;
405419

406420
function runAction (name, next) {
407-
var store = self.sources[name];
421+
var store = self._stores[name];
408422

409423
if (destructive && store.readOnly) {
410424
return next();
@@ -416,15 +430,15 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
416430
}
417431

418432
if (callback) {
419-
return async.forEach(Object.keys(this.sources), runAction, function (err) {
433+
return async.forEach(Object.keys(this._stores), runAction, function (err) {
420434
return err ? callback(err) : callback();
421435
});
422436
}
423437

424438

425-
Object.keys(this.sources).forEach(function (name) {
439+
Object.keys(this._stores).forEach(function (name) {
426440
if (typeof response === 'undefined') {
427-
var store = self.sources[name];
441+
var store = self._stores[name];
428442

429443
if (destructive && store.readOnly) {
430444
return;

test/fixtures/scripts/nconf-change-argv.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ var nconf = require('../../../lib/nconf').argv();
1111
// Remove 'badValue', 'evenWorse' and 'OHNOEZ'
1212
//
1313
process.argv.splice(3, 3);
14-
nconf.sources['argv'].loadArgv();
14+
nconf._stores['argv'].loadArgv();
1515
process.stdout.write(nconf.get('something'));
1616

test/nconf-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ vows.describe('nconf').addBatch({
2828
"the use() method": {
2929
"should instaniate the correct store": function () {
3030
nconf.use('memory');
31-
assert.instanceOf(nconf.sources['memory'], nconf.stores.Memory);
31+
assert.instanceOf(nconf._stores['memory'], nconf.stores.Memory);
3232
}
3333
},
3434
"it should": {

test/provider-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ vows.describe('nconf/provider').addBatch({
2424
"calling the use() method with the same store type and different options": {
2525
topic: new nconf.Provider().use('file', { file: files[0] }),
2626
"should use a new instance of the store type": function (provider) {
27-
var old = provider.sources['file'];
27+
var old = provider._stores['file'];
2828

29-
assert.equal(provider.sources.file.file, files[0]);
29+
assert.equal(provider._stores.file.file, files[0]);
3030
provider.use('file', { file: files[1] });
3131

32-
assert.notStrictEqual(old, provider.sources.file);
33-
assert.equal(provider.sources.file.file, files[1]);
32+
assert.notStrictEqual(old, provider._stores.file);
33+
assert.equal(provider._stores.file.file, files[1]);
3434
}
3535
},
3636
"when 'argv' is true": helpers.assertSystemConf({
@@ -71,7 +71,7 @@ vows.describe('nconf/provider').addBatch({
7171
"should have the result merged in": function (provider) {
7272
provider.load();
7373
provider.merge(override);
74-
helpers.assertMerged(null, provider.sources.file.store);
74+
helpers.assertMerged(null, provider._stores.file.store);
7575
}
7676
}
7777
}

0 commit comments

Comments
 (0)