summaryrefslogtreecommitdiffstats
path: root/lib/models/config.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-22 11:56:34 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-22 11:56:34 +0200
commit4f86a9978b4b27b12e40ab0b5b9e18b8e0615266 (patch)
tree8c955eaf8f96da8a2dd811a75afffaf17dcc4fbc /lib/models/config.js
parentcc78b880cef726201f1670b5c9f7f6f3512c2dbe (diff)
downloadgitbook-4f86a9978b4b27b12e40ab0b5b9e18b8e0615266.zip
gitbook-4f86a9978b4b27b12e40ab0b5b9e18b8e0615266.tar.gz
gitbook-4f86a9978b4b27b12e40ab0b5b9e18b8e0615266.tar.bz2
Add test for config
Diffstat (limited to 'lib/models/config.js')
-rw-r--r--lib/models/config.js53
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;