Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Export the current Environment.
- **/
- exports.env = null;
- /**
- * Export the last error that occured, if any.
- **/
- exports._lastError = null;
- /**
- * Storage for current settings object.
- **/
- var SETTINGS;
- /**
- * These are the various environment based settings.
- *
- * @return {Object} The loaded settings object.
- * @throws {Error} If the settings file cannot be found.
- **/
- var environments = {
- development: function() {
- return require('./settings/development').settings;
- },
- production: function() {
- return require('./settings/production').settings;
- },
- local: function() {
- return require('./settings/local').settings;
- }
- };
- /**
- * Cache for cache()
- **/
- var _cache = {};
- /**
- * Takes a given key and stores it in the cache
- * for faster lookup times.
- *
- * @param {String} key The key name.
- * @param {Mixed} value The value for the key.
- * @return {Mixed} If value: the value; Else key exists boolean.
- **/
- function cache(key, value) {
- if (!value) {
- return _cache[key];
- } else {
- return _cache[key] = value;
- }
- }
- /**
- * Parses a string to figure out its value within an object
- *
- * @param {Object} settings The settings object to search within.
- * @param {String} key The key to search for.
- * @param {Mixed} value If acting as a Setter, the value to set.
- * @return {Mixed} The current value of object at key.
- **/
- function resolve(settings, key, value) {
- if (_cache[key] === undefined || arguments.length === 3) {
- var current = settings;
- for (var node, parts = key.split('.');
- parts.length && (node = parts.shift()); ) {
- if (parts.length !== 0) {
- current = current[node] = (current[node] || {});
- } else {
- current = (value ? (current[node] = value) : current[node]);
- }
- }
- return cache(key, current);
- } else {
- return cache(key);
- }
- }
- /**
- * Sets the current environment and loads up the corresponding settings.
- *
- * @param {String} env The environment to switch to.
- * @return {Boolean} The environment was successfully switched.
- **/
- exports.setEnv = function(env) {
- if (typeof environments[env] === 'function') {
- try {
- SETTINGS = environments[env]();
- exports.env = env;
- return true;
- } catch (e) {
- exports._lastError = e;
- return false;
- }
- } else {
- return false;
- }
- };
- /**
- * Returns the currently loaded environment.
- *
- * @return {String} The currently loaded environment.
- * @this The `settings` object.
- **/
- exports.getEnv = function() {
- return exports.env;
- };
- /**
- * Set the current environment to that defined in process.env.SERVER_ENV;
- *
- * Defaults to 'local' environment.
- *
- * @return {Boolean} The environment was successfully switched.
- **/
- exports.setAutoEnv = function() {
- if (process.env.SERVER_ENV) {
- return exports.setEnv(process.env.SERVER_ENV);
- } else {
- return exports.setEnv('local');
- }
- };
- /**
- * Returns the value of the setting at `key`.
- *
- * If only we had proxies, of well, here's to ES Harmony
- *
- * @param {String} key An object tree, levels separated by a period.
- * @return {Mixed} The value for the key.
- **/
- exports.get = function(key) {
- if (!SETTINGS) {
- throw new Error('setEnv must be called before settings can be accessed');
- }
- return resolve(SETTINGS, key);
- };
- /**
- * Sets the value of the setting at key with `value`.
- *
- * @param {String} key An object tree, levels separated by a period.
- * @param {Mixed} value The value to set the key to.
- **/
- exports.set = function(key, value) {
- if (!SETTINGS) {
- throw new Error('setEnv must be called before settings can be changed');
- }
- resolve(SETTINGS, key, value);
- };
- /**
- * The toJSON method for JSON.stringify
- *
- * @return {String} The JSON encoded `SETTINGS` object.
- **/
- exports.toJSON = function() {
- return SETTINGS;
- };
Add Comment
Please, Sign In to add comment