Skip to content

Commit

Permalink
[refactor] Expose all store prototypes on nconf.*. Expose store ins…
Browse files Browse the repository at this point in the history
…tances on Provider.stores and Provider.sources
  • Loading branch information
indexzero committed Nov 24, 2011
1 parent c3cebe7 commit 16a18bf
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 102 deletions.
15 changes: 13 additions & 2 deletions lib/nconf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ var fs = require('fs'),
//
require('pkginfo')(module, 'version');

//
// Setup all stores as lazy-loaded getters.
//
fs.readdirSync(__dirname + '/nconf/stores').forEach(function (file) {
var store = file.replace('.js', ''),
name = common.capitalize(store);

nconf.__defineGetter__(name, function () {
return require('./nconf/stores/' + store)[name];
});
});

//
// Expose the various components included with nconf
//
Expand All @@ -24,5 +36,4 @@ nconf.path = common.path;
nconf.loadFiles = common.loadFiles;
nconf.loadFilesSync = common.loadFilesSync;
nconf.formats = require('./nconf/formats');
nconf.stores = require('./nconf/stores');
nconf.Provider = Provider;
nconf.Provider = Provider;
112 changes: 56 additions & 56 deletions lib/nconf/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
*/

var async = require('async'),
common = require('./common'),
stores = require('./stores');


common = require('./common');

//
// ### function Provider (options)
Expand All @@ -25,38 +22,9 @@ var Provider = exports.Provider = function (options) {
// `overrides`, `process.env` and `process.argv`.
//
options = options || {};
this._stores = {};
this._sources = [];

//
// Add any stores passed in through the options
// to this instance.
//
if (options.type) {
this.add(options.type, options);
}
else if (options.store) {
this.add(options.store.name || options.store.type, options.store);
}
else if (options.stores) {
Object.keys(options.stores).forEach(function (name) {
var store = options.stores[name];
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));
});
}
this.stores = {};
this.sources = [];
this.init(options);
};

//
Expand Down Expand Up @@ -100,7 +68,7 @@ Provider.prototype.use = function (name, options) {
});
}

var store = this._stores[name],
var store = this.stores[name],
update = store && !sameOptions(store);

if (!store || update) {
Expand Down Expand Up @@ -128,14 +96,14 @@ Provider.prototype.add = function (name, options) {
options = options || {};
var type = options.type || name;

if (Object.keys(stores).indexOf(common.capitalize(type)) === -1) {
if (!require('../nconf')[common.capitalize(type)]) {
throw new Error('Cannot add store with unknown type: ' + type);
}

this._stores[name] = this.create(type, options);
this.stores[name] = this.create(type, options);

if (this._stores[name].loadSync) {
this._stores[name].loadSync();
if (this.stores[name].loadSync) {
this.stores[name].loadSync();
}

return this;
Expand All @@ -149,7 +117,7 @@ Provider.prototype.add = function (name, options) {
// this was used in the call to `.add()`.
//
Provider.prototype.remove = function (name) {
delete this._stores[name];
delete this.stores[name];
return this;
};

Expand All @@ -161,7 +129,39 @@ Provider.prototype.remove = function (name) {
// specified `options`.
//
Provider.prototype.create = function (type, options) {
return new stores[common.capitalize(type.toLowerCase())](options);
return new (require('../nconf')[common.capitalize(type.toLowerCase())])(options);
};

Provider.prototype.init = function (options) {
//
// Add any stores passed in through the options
// to this instance.
//
if (options.type) {
this.add(options.type, options);
}
else if (options.store) {
this.add(options.store.name || options.store.type, options.store);
}
else if (options.stores) {
Object.keys(options.stores).forEach(function (name) {
var store = options.stores[name];
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));
});
}
};

//
Expand All @@ -185,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._stores),
names = Object.keys(this.stores),
self = this,
response;

async.whilst(function () {
return typeof response === 'undefined' && current < names.length;
}, function (next) {
var store = self._stores[names[current]];
var store = self.stores[names[current]];
current++;

if (store.get.length >= 2) {
Expand Down Expand Up @@ -282,7 +282,7 @@ Provider.prototype.merge = function () {
//
Provider.prototype.load = function (callback) {
var self = this,
stores = Object.keys(this._stores).map(function (name) { return self._stores[name] });
stores = Object.keys(this.stores).map(function (name) { return self.stores[name] });

function loadStoreSync(store) {
if (!store.loadSync) {
Expand Down Expand Up @@ -331,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);
}
Expand All @@ -345,7 +345,7 @@ Provider.prototype.load = function (callback) {
});
}

return self._sources.length
return self.sources.length
? loadSources()
: loadBatch(stores, callback);
};
Expand All @@ -364,10 +364,10 @@ Provider.prototype.save = function (value, callback) {
}

var self = this,
names = Object.keys(this._stores);
names = Object.keys(this.stores);

function saveStoreSync(name) {
var store = self._stores[name];
var store = self.stores[name];

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

function saveStore(name, next) {
var store = self._stores[name];
var store = self.stores[name];

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

function runAction (name, next) {
var store = self._stores[name];
var store = self.stores[name];

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

if (callback) {
return async.forEach(Object.keys(this._stores), runAction, function (err) {
return async.forEach(Object.keys(this.stores), runAction, function (err) {
return err ? callback(err) : callback();
});
}


Object.keys(this._stores).forEach(function (name) {
Object.keys(this.stores).forEach(function (name) {
if (typeof response === 'undefined') {
var store = self._stores[name];
var store = self.stores[name];

if (destructive && store.readOnly) {
return;
Expand Down
22 changes: 0 additions & 22 deletions lib/nconf/stores.js

This file was deleted.

5 changes: 2 additions & 3 deletions lib/nconf/stores/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

var util = require('util'),
optimist = require('optimist'),
Memory = require('./memory').Memory;

//
Expand Down Expand Up @@ -45,8 +44,8 @@ Argv.prototype.loadArgv = function () {
argv;

argv = typeof this.options === 'object'
? optimist(process.argv.slice(2)).options(this.options).argv
: optimist(process.argv.slice(2)).argv;
? require('optimist')(process.argv.slice(2)).options(this.options).argv
: require('optimist')(process.argv.slice(2)).argv;

if (!argv) {
return;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/scripts/nconf-change-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ var nconf = require('../../../lib/nconf').argv();
// Remove 'badValue', 'evenWorse' and 'OHNOEZ'
//
process.argv.splice(3, 3);
nconf._stores['argv'].loadArgv();
nconf.stores['argv'].loadArgv();
process.stdout.write(nconf.get('something'));

2 changes: 1 addition & 1 deletion test/nconf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ vows.describe('nconf').addBatch({
"the use() method": {
"should instaniate the correct store": function () {
nconf.use('memory');
assert.instanceOf(nconf._stores['memory'], nconf.stores.Memory);
assert.instanceOf(nconf.stores['memory'], nconf.Memory);
}
},
"it should": {
Expand Down
10 changes: 5 additions & 5 deletions test/provider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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._stores['file'];
var old = provider.stores['file'];

assert.equal(provider._stores.file.file, files[0]);
assert.equal(provider.stores.file.file, files[0]);
provider.use('file', { file: files[1] });

assert.notStrictEqual(old, provider._stores.file);
assert.equal(provider._stores.file.file, files[1]);
assert.notStrictEqual(old, provider.stores.file);
assert.equal(provider.stores.file.file, files[1]);
}
},
"when 'argv' is true": helpers.assertSystemConf({
Expand Down Expand Up @@ -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._stores.file.store);
helpers.assertMerged(null, provider.stores.file.store);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/stores/argv-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var vows = require('vows'),
nconf = require('../../lib/nconf');

vows.describe('nconf/stores/argv').addBatch({
"An instance of nconf.stores.Argv": {
topic: new nconf.stores.Argv(),
"An instance of nconf.Argv": {
topic: new nconf.Argv(),
"should have the correct methods defined": function (argv) {
assert.isFunction(argv.loadSync);
assert.isFunction(argv.loadArgv);
Expand Down
4 changes: 2 additions & 2 deletions test/stores/env-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var vows = require('vows'),
nconf = require('../../lib/nconf');

vows.describe('nconf/stores/env').addBatch({
"An instance of nconf.stores.Env": {
topic: new nconf.stores.Env(),
"An instance of nconf.Env": {
topic: new nconf.Env(),
"should have the correct methods defined": function (env) {
assert.isFunction(env.loadSync);
assert.isFunction(env.loadEnv);
Expand Down
Loading

0 comments on commit 16a18bf

Please sign in to comment.