diff options
Diffstat (limited to 'lib/models/config.js')
-rw-r--r-- | lib/models/config.js | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/lib/models/config.js b/lib/models/config.js index fd4201d..e32ccdc 100644 --- a/lib/models/config.js +++ b/lib/models/config.js @@ -6,7 +6,7 @@ var File = require('./file'); var Config = Immutable.Record({ file: File(), values: Immutable.Map() -}); +}, 'Config'); Config.prototype.getPath = function() { return this.get('path'); @@ -24,13 +24,35 @@ Config.prototype.getValues = function() { */ Config.prototype.getValue = function(keyPath, def) { var values = this.getValues(); - if (is.string(keyPath)) keyPath = keyPath.split('.'); + keyPath = Config.keyToKeyPath(keyPath); + + if (!values.hasIn(keyPath)) { + return def; + } + + return values.getIn(keyPath); +}; + +/** + Update a configuration value + + @param {String} key + @param {Mixed} value + @return {Mixed} +*/ +Config.prototype.setValue = function(keyPath, value) { + keyPath = Config.keyToKeyPath(keyPath); + + value = Immutable.fromJS(value); - return values.getIn(keyPath) || def; + var values = this.getValues(); + values = values.setIn(keyPath, value); + + return this.set('values', values); }; /** - Create a new config, throw error if invalid + Create a new config for a file @param {File} file @param {Object} values @@ -43,5 +65,28 @@ Config.create = function(file, values) { }); }; +/** + Create a new config + + @param {Object} values + @returns {Config} +*/ +Config.createWithValues = function(values) { + return new Config({ + values: Immutable.fromJS(values) + }); +}; + + +/** + Convert a keyPath to an array of keys + + @param {String|Array} + @return {Array} +*/ +Config.keyToKeyPath = function(keyPath) { + if (is.string(keyPath)) keyPath = keyPath.split('.'); + return keyPath; +} module.exports = Config; |