Merge pull request #17 from christav/after-the-fact-customizer

After the fact customization
This commit is contained in:
Chris Tavares 2014-05-23 10:55:06 -07:00
Родитель 2dbd4eec40 47b5698218
Коммит 1ddf9fc879
3 изменённых файлов: 71 добавлений и 1 удалений

Просмотреть файл

@ -108,11 +108,23 @@ c3.configure('test', function (c) {
c.useSql('testmachine', 'testdb');
});
c3.configure('production', function (c) {
c.useSql('realDatabase', 'actualDb');
});
// Can also customize after creation, customizations
// will affect any child configs too!
c3 = envconf.createConfig();
var prod = c3('prod');
c3.customize(addConfigHelpers);
c3.configure('production', function (c) {
c.useSql('realDatabase', 'actualDb');
});
```
## Saving and Restoring config values
Are you making changes to a global configuration in your unit tests, and want

Просмотреть файл

@ -29,9 +29,10 @@ function Snapshot(settings, environments) {
});
}
function Config(parent, defaultEnvVar, customizer) {
function Config(parent, defaultEnvVar, initialCustomizer) {
var environments = { };
var settings = { };
var customizer = initialCustomizer;
if (!defaultEnvVar) {
defaultEnvVar = 'NODE_ENV';
@ -55,6 +56,26 @@ function Config(parent, defaultEnvVar, customizer) {
}
};
config.customize = function (newCustomizer) {
newCustomizer(this);
if (!customizer) {
customizer = newCustomizer;
} else {
customizer = (function (oldCustomizer) {
return function (c) {
oldCustomizer(c);
newCustomizer(c);
};
}(customizer));
}
Object.keys(environments).forEach(function (envName) {
environments[envName].customize(newCustomizer);
});
return this;
};
config.set = function (setting, value) {
if (arguments.length === 2) {
settings[setting] = function () { return value; };

Просмотреть файл

@ -269,6 +269,10 @@ describe('Config customization', function () {
config.customValue = ++count;
}
function afterCreateCustomizer(config) {
config.wasCustomizedAfterCreation = true;
}
var spy;
beforeEach(function () {
@ -306,6 +310,39 @@ describe('Config customization', function () {
spy.callCount.should.equal(2);
});
it('should customize after creation', function () {
var c = envconf.createConfig();
c.customize(afterCreateCustomizer);
c.wasCustomizedAfterCreation.should.be.true;
});
it('should customize existing child environments', function () {
var c = envconf.createConfig();
var dev = c('dev');
var localdev = dev('local');
c.customize(afterCreateCustomizer);
dev.wasCustomizedAfterCreation.should.be.true;
localdev.wasCustomizedAfterCreation.should.be.true;
});
it('should customize new child environments after customizing', function () {
var c = envconf.createConfig();
c.customize(afterCreateCustomizer);
c('dev').wasCustomizedAfterCreation.should.be.true;
});
it('should add customizers instead of replacing', function () {
var c = envconf.createConfig({customizer: spy});
c.customize(afterCreateCustomizer);
c('dev').customValue.should.equal(2);
c('dev').wasCustomizedAfterCreation.should.be.true;
});
});
describe('Snapshot and restore', function () {