From 54894b306af26bfd78018ea97bb51f70a9f5faee Mon Sep 17 00:00:00 2001 From: Chris Tavares Date: Thu, 25 Jul 2013 20:23:34 -0700 Subject: [PATCH] Added setFunc - give a function to retrieve a value instead of the value --- README.md | 31 +++++++++++++++++++++++++++---- lib/index.js | 9 +++++++-- test/config-test.js | 24 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d2a6067..2a68883 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/index.js b/lib/index.js index d915964..b583a21 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { diff --git a/test/config-test.js b/test/config-test.js index 25f07cd..8277940 100644 --- a/test/config-test.js +++ b/test/config-test.js @@ -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); + }); +}); \ No newline at end of file