memory.js | |
---|---|
/*
* memory.js: Simple memory storage engine for nconf configuration(s)
*
* (C) 2011, Charlie Robbins
*
*/
var nconf = require('../../nconf'); | |
function Memory (options)@options {Object} Options for this instanceConstructor function for the Memory nconf store which maintains
a nested json structure based on key delimiters e.g. | var Memory = exports.Memory = function (options) {
options = options || {};
this.type = 'memory';
this.store = {};
this.mtimes = {};
}; |
function get (key)@key {string} Key to retrieve for this instance.Retrieves the value for the specified key (if any). | Memory.prototype.get = function (key) {
var target = this.store,
path = nconf.path(key); |
Scope into the object to get the appropriate nested context | while (path.length > 0) {
key = path.shift();
if (!(target && key in target)) {
return;
}
target = target[key];
if (path.length === 0) {
return target;
}
}
}; |
function set (key, value)@key {string} Key to set in this instance@value {literal|Object} Value for the specified keySets the | Memory.prototype.set = function (key, value) {
var target = this.store,
path = nconf.path(key);
|
Update the | this.mtimes[key] = Date.now();
|
Scope into the object to get the appropriate nested context | while (path.length > 1) {
key = path.shift();
if (!target[key] || typeof target[key] !== 'object') {
target[key] = {};
}
target = target[key];
}
|
Set the specified value in the nested JSON structure | key = path.shift();
target[key] = value;
return true;
}; |
function clear (key)@key {string} Key to remove from this instanceRemoves the value for the specified | Memory.prototype.clear = function (key) {
var target = this.store,
path = nconf.path(key);
|
Remove the key from the set of | delete this.mtimes[key];
|
Scope into the object to get the appropriate nested context | while (path.length > 1) {
key = path.shift();
if (!target[key]) {
return;
}
target = target[key];
}
|
Delete the key from the nested JSON structure | key = path.shift();
delete target[key];
return true;
}; |
function merge (key, value)@key {string} Key to merge the value into@value {literal|Object} Value to merge into the keyMerges the properties in | Memory.prototype.merge = function (key, value) { |
If the key is not an | if (typeof value !== 'object' || Array.isArray(value)) {
return this.set(key, value);
}
var self = this,
target = this.store,
path = nconf.path(key),
fullKey = key;
|
Update the | this.mtimes[key] = Date.now();
|
Scope into the object to get the appropriate nested context | while (path.length > 1) {
key = path.shift();
if (!target[key]) {
target[key] = {};
}
target = target[key];
} |
Set the specified value in the nested JSON structure | key = path.shift();
|
If the current value at the key target is not an | if (typeof target[key] !== 'object' || Array.isArray(target[key])) {
target[key] = value;
return true;
}
return Object.keys(value).every(function (nested) {
return self.merge(fullKey + ':' + nested, value[nested]);
});
}; |
function reset (callback)Clears all keys associated with this instance. | Memory.prototype.reset = function () {
this.mtimes = {};
this.store = {};
return true;
}
|