UNPKG

4.36 kBJavaScriptView Raw
1/**
2 * Copyright 2014 Skytap Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 **/
16
17var _ = require('underscore');
18
19/**
20 * class Config
21 *
22 * Module to handle configuration settings.
23 **/
24var Config = {
25
26 configs: {},
27
28 //////////////////////////////////////////////////////////////////////////
29 // Public methods ///////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////////////////
31
32 /**
33 * Config.get(path) -> Mixed
34 * - path (String): Dot-delimited configuration path. Example: 'api.ratelimit'
35 *
36 * Get a config value.
37 **/
38 get : function (path) {
39 if (!path) {
40 throw new Error('You must supply a config path');
41 }
42
43 var config = this.configs,
44 parts = path.split(".");
45
46 for (var i in parts) {
47 var currentPart = parts[i];
48 if (!config.hasOwnProperty(currentPart)) {
49 throw new Error('No config value found for: ' + path);
50 }
51 config = config[currentPart];
52 }
53
54 return config;
55 },
56
57 /**
58 * Config.getAll() -> Object
59 *
60 * Get all available config values.
61 **/
62 getAll : function () {
63 return this.configs;
64 },
65
66 /**
67 * Config.load(environment, configs) -> Object
68 * - environment (String): Select configs specific to this environment
69 * - configs (Object): The configs to process
70 *
71 * Loads environment-specific config values.
72 **/
73 load : function(environment, configs) {
74 _.extend(
75 this.configs,
76 this._processConfigs(environment, configs)
77 );
78 },
79
80 /**
81 * Config.set(path, value)
82 * - path (String): Dot-delimited configuration path. Example: 'api.ratelimit'
83 * - value (Mixed): Config value
84 *
85 * Set a config value.
86 **/
87 set : function (path, value) {
88 var config = this.configs,
89 parts = path.split(".");
90
91 parts.forEach(function (currentPart, index) {
92 if (index === parts.length - 1) {
93 config[currentPart] = value;
94 return;
95 }
96
97 if (!config.hasOwnProperty(currentPart)) {
98 config[currentPart] = {};
99 }
100
101 config = config[currentPart];
102 });
103 },
104
105 //////////////////////////////////////////////////////////////////////////
106 // Pseudo-private methods ///////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////////////////
108
109 /**
110 * Config._processConfigs(environment, configs) -> Object
111 * - environment (String): Select configs specific to this environment
112 * - configs (Object): The configs to process
113 *
114 * Produces a config object that contains environment-specific config values.
115 **/
116 _processConfigs : function(environment, configs) {
117 var self = this,
118 results = {},
119 derivedValue;
120
121 _.each(configs, function (value, key) {
122 // literal config value
123 if (typeof(value) === 'string' || typeof(value) === 'number') {
124 results[key] = value;
125 return;
126 }
127
128 if (value.hasOwnProperty(environment)) {
129 // environment-specific config
130 derivedValue = value[environment];
131 } else if (value.hasOwnProperty('default')) {
132 // fallback if environment-specific config isn't found
133 derivedValue = value['default'];
134 } else {
135 // nested object
136 derivedValue = self._processConfigs(environment, value);
137 }
138
139 results[key] = derivedValue;
140 });
141
142 return results;
143 },
144};
145
146module.exports = Config;
\No newline at end of file