Skip to content

Commit

Permalink
[api test] Added .sources option for nconf.Provider for readonly …
Browse files Browse the repository at this point in the history
…configuration data
  • Loading branch information
indexzero committed Sep 25, 2011
1 parent 0234e17 commit d0aee0d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/nconf/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var Provider = exports.Provider = function (options) {
this._env  = options.env || false;
this._reserved = Object.keys(Provider.prototype);
this._stores = [];
this.sources = [];

//
// Add the default `system` store for working with
Expand All @@ -38,6 +39,10 @@ var Provider = exports.Provider = function (options) {
//
this.add('system', options);

//
// Add any stores passed in through the options
// to this instance.
//
if (options.type) {
this.add(options.type, options);
}
Expand All @@ -47,7 +52,20 @@ var Provider = exports.Provider = function (options) {
else if (options.stores) {
Object.keys(options.stores).forEach(function (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));
});
}
};
Expand Down Expand Up @@ -122,6 +140,8 @@ Provider.prototype.add = function (name, options) {
if (this[name].loadSync) {
this[name].loadSync();
}

return this;
};

//
Expand All @@ -141,6 +161,7 @@ Provider.prototype.remove = function (name) {

delete this[name];
this._stores.splice(this._stores.indexOf(name), 1);
return this;
};

//
Expand Down Expand Up @@ -270,21 +291,19 @@ Provider.prototype.merge = function () {
// Responds with an Object representing all keys associated in this instance.
//
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) {
var store = self[name];

function loadStoreSync(store) {
if (!store.loadSync) {
throw new Error('nconf store ' + store.type + ' has no loadSync() method');
}

return store.loadSync();
}

function loadStore(name, next) {
var store = self[name];

function loadStore(store, next) {
if (!store.load && !store.loadSync) {
return next(new Error('nconf store ' + store.type + ' has no load() method'));
}
Expand All @@ -300,10 +319,10 @@ Provider.prototype.load = function (callback) {
// then do so.
//
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));
});
};
Expand Down
17 changes: 17 additions & 0 deletions test/provider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ vows.describe('nconf/provider').addBatch({
provider.merge(override);
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());
}
}
}
}
Expand Down

0 comments on commit d0aee0d

Please sign in to comment.