Merge pull request #9 from christav/setFunc

Added setFunc - give a function to retrieve a value instead of the value
This commit is contained in:
Chris Tavares 2013-07-26 13:20:51 -07:00
Родитель ac999a8cf9 54894b306a
Коммит 429aa20db9
3 изменённых файлов: 58 добавлений и 6 удалений

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

@ -5,8 +5,7 @@ This module makes it easy to use express-style configuration for any application
It allows your users to define separate configuration environments in code
and switch between sets of configuration via a single environment variable.
Usage:
## Basic Usage
```javascript
@ -50,8 +49,28 @@ process.env.MY_LIBRARY_VAR = 'production';
c.default.get('settingTwo').should.equal('prodValue');
```
Do you want to add helper methods for your specific configuration? It's easy
with a config customizer:
## Setting Getters
Instead of a setting a simple value, you can instead use the setFunc method to
provide a function that will run when the value is requested:
```javascript
var c2 = envconf.createConfig();
c2.configure('development', function (c) {
c.setFunc('settingOne', function () { return 'This value came from a function'; });
});
c2.get('settingOne').should.equal('This value came from a function');
```
This can be handy if you want to have a default value that you need to derive from
ambient state.
## Customizing the config object
Do you want to add helper methods for your specific configuration? Or set specific
values in every configuration? It's easy with a config customizer:
```javascript
@ -73,6 +92,8 @@ c3.configure('production', function (c) {
});
```
## Saving and Restoring config values
Are you making changes to a global configuration in your unit tests, and want
to ensure you've restored the state after your test? Use a snapshot:
@ -98,6 +119,8 @@ c4.get('originalValue').should.equal('one');
Snapshot/restore also saves and restores any child configurations.
## Temporary Configs
Similarly, you might want to set up a configuration and then be able
to throw it away without giving it a name. Easy:

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

@ -56,7 +56,12 @@ function Config(parent, defaultEnvVar, customizer) {
};
config.set = function (setting, value) {
settings[setting] = value;
settings[setting] = function () { return value; };
return this;
};
config.setFunc = function (setting, getter) {
settings[setting] = getter;
return this;
};
@ -67,7 +72,7 @@ function Config(parent, defaultEnvVar, customizer) {
}
return undefined;
}
return settings[setting];
return settings[setting]();
};
config.has = function (setting) {

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

@ -372,3 +372,27 @@ describe('Temporary environments', function () {
customizer.callCount.should.equal(2);
});
});
describe('Setting getters', function () {
var c;
beforeEach(function () {
c = envconf.createConfig();
});
it('should call getter when retrieving', function () {
function getter() { return 'a value'; }
var spy = sinon.spy(getter);
c.setFunc('setting', spy);
c.get('setting').should.equal('a value');
spy.callCount.should.equal(1);
});
it('should not be invoked when set using set method', function () {
function getter() { return 'a value'; }
var spy = sinon.spy(getter);
c.set('setting', spy);
c.get('setting').should.equal(spy);
spy.callCount.should.equal(0);
});
});