Skip to content
Browse files

overridable from file/object

  • Loading branch information...
1 parent 93fb4e7 commit 7cae44c2d53f04ec58f9bdff46b4421cd0762877 @mgutz committed
View
21 lib/settings.js
@@ -13,10 +13,8 @@ Settings = function(pathOrModule, options) {
this.options = options != null ? options : {};
if (typeof pathOrModule === 'string') {
this.path = pathOrModule;
- this.environments = require(pathOrModule);
- } else {
- this.environments = pathOrModule;
}
+ this.environments = Settings.loadModule(pathOrModule);
if (this.options.globalKey != null) {
this._settings = this.getEnvironment();
global.__defineGetter__(this.options.globalKey, __bind(function() {
@@ -43,6 +41,23 @@ Settings.prototype.getEnvironment = function(environ) {
if (this.options.globalKey != null) {
this._settings = result;
}
+ result.override = Settings.override;
return result;
};
+Settings.loadModule = function(pathOrModule) {
+ if (typeof pathOrModule === 'string') {
+ return require(pathOrModule);
+ } else {
+ return pathOrModule;
+ }
+};
+Settings.override = function(pathOrModule) {
+ var mod;
+ mod = Settings.loadModule(pathOrModule);
+ if (mod.common != null) {
+ mod = new Settings(mod).getEnvironment();
+ }
+ merger.extend(this, mod);
+ return this;
+};
module.exports = Settings;
View
4 package.json
@@ -1,6 +1,6 @@
{ "name": "settings",
- "version": "0.0.2",
- "description": "Simple, hierarchical environment-based app settings",
+ "version": "0.0.3",
+ "description": "Simple environment-based application settings",
"author": "Mario Gutierrez <mario@mgutz.com>",
"main": "index",
"repository" : "git://github.com/mgutz/node-settings"
View
19 src/lib/settings.coffee
@@ -30,9 +30,8 @@ Settings = (pathOrModule, @options = {}) ->
if typeof pathOrModule == 'string'
@path = pathOrModule
- @environments = require(pathOrModule)
- else
- @environments = pathOrModule
+
+ @environments = Settings.loadModule(pathOrModule)
if @options.globalKey?
@_settings = @getEnvironment()
@@ -71,7 +70,21 @@ Settings.prototype.getEnvironment = (environ) ->
if @options.globalKey?
@_settings = result
+ result.override = Settings.override
result
+Settings.loadModule = (pathOrModule) ->
+ if typeof pathOrModule == 'string'
+ require(pathOrModule)
+ else
+ pathOrModule
+
+Settings.override = (pathOrModule) ->
+ mod = Settings.loadModule(pathOrModule)
+ if mod.common?
+ mod = new Settings(mod).getEnvironment()
+ merger.extend this, mod
+ this
+
module.exports = Settings
View
3 src/test/config/override.coffee
@@ -0,0 +1,3 @@
+module.exports =
+ storage:
+ host: 'override'
View
3 src/test/config/override2.coffee
@@ -0,0 +1,3 @@
+exports.common =
+ storage:
+ host: 'override2'
View
10 src/test/settings.test.coffee
@@ -120,6 +120,16 @@ module.exports =
settings = new Settings(environments, options).getEnvironment('development')
assert.equal 'app/views', settings.framework.views
+ "should be overriden from file": ->
+ settings = _settings.getEnvironment('test')
+ assert.equal 'server_test', settings.storage.database
+ assert.equal 'localhost', settings.storage.host
+ settings.override __dirname + "/config/override"
+ assert.equal 'server_test', settings.storage.database
+ assert.equal 'override', settings.storage.host
+ settings.override __dirname + "/config/override2"
+ assert.equal 'server_test', settings.storage.database
+ assert.equal 'override2', settings.storage.host
View
12 test/config/override.js
@@ -0,0 +1,12 @@
+/*============================================================================
+ Copyright(c) 2010 Mario L Gutierrez <mario@mgutz.com>
+ MIT Licensed
+
+ AUTO-GENERATED. DO NOT EDIT.
+============================================================================*/
+// Original file: src/test/config/override.coffee
+module.exports = {
+ storage: {
+ host: 'override'
+ }
+};
View
12 test/config/override2.js
@@ -0,0 +1,12 @@
+/*============================================================================
+ Copyright(c) 2010 Mario L Gutierrez <mario@mgutz.com>
+ MIT Licensed
+
+ AUTO-GENERATED. DO NOT EDIT.
+============================================================================*/
+// Original file: src/test/config/override2.coffee
+exports.common = {
+ storage: {
+ host: 'override2'
+ }
+};
View
12 test/settings.test.js
@@ -154,5 +154,17 @@ module.exports = {
};
settings = new Settings(environments, options).getEnvironment('development');
return assert.equal('app/views', settings.framework.views);
+ },
+ "should be overriden from file": function() {
+ var settings;
+ settings = _settings.getEnvironment('test');
+ assert.equal('server_test', settings.storage.database);
+ assert.equal('localhost', settings.storage.host);
+ settings.override(__dirname + "/config/override");
+ assert.equal('server_test', settings.storage.database);
+ assert.equal('override', settings.storage.host);
+ settings.override(__dirname + "/config/override2");
+ assert.equal('server_test', settings.storage.database);
+ return assert.equal('override2', settings.storage.host);
}
};

0 comments on commit 7cae44c

Please sign in to comment.
Something went wrong with that request. Please try again.