2011-03-31 06:32:47 +00:00
|
|
|
/*
|
|
|
|
* memory.js: Simple memory storage engine for nconf configuration(s)
|
|
|
|
*
|
|
|
|
* (C) 2011, Charlie Robbins
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-05-14 05:47:26 +00:00
|
|
|
var nconf = require('../../nconf');
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function Memory (options)
|
|
|
|
// #### @options {Object} Options for this instance
|
|
|
|
// Constructor function for the Memory nconf store which maintains
|
|
|
|
// a nested json structure based on key delimiters `:`.
|
|
|
|
//
|
|
|
|
// e.g. `my:nested:key` ==> `{ my: { nested: { key: } } }`
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
var Memory = exports.Memory = function (options) {
|
2011-04-02 07:03:16 +00:00
|
|
|
options = options || {};
|
2011-04-02 23:17:04 +00:00
|
|
|
this.type = 'memory';
|
2011-04-02 07:03:16 +00:00
|
|
|
this.store = {};
|
|
|
|
this.mtimes = {};
|
2011-03-31 06:32:47 +00:00
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function get (key)
|
|
|
|
// #### @key {string} Key to retrieve for this instance.
|
|
|
|
// Retrieves the value for the specified key (if any).
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
Memory.prototype.get = function (key) {
|
|
|
|
var target = this.store,
|
2011-04-02 07:03:16 +00:00
|
|
|
path = nconf.path(key);
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Scope into the object to get the appropriate nested context
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
while (path.length > 0) {
|
|
|
|
key = path.shift();
|
|
|
|
if (!target[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = target[key];
|
|
|
|
if (path.length === 0) {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function set (key, value)
|
|
|
|
// #### @key {string} Key to set in this instance
|
|
|
|
// #### @value {literal|Object} Value for the specified key
|
|
|
|
// Sets the `value` for the specified `key` in this instance.
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
Memory.prototype.set = function (key, value) {
|
|
|
|
var target = this.store,
|
2011-04-02 07:03:16 +00:00
|
|
|
path = nconf.path(key);
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Update the `mtime` (modified time) of the key
|
|
|
|
//
|
|
|
|
this.mtimes[key] = Date.now();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Scope into the object to get the appropriate nested context
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
while (path.length > 1) {
|
|
|
|
key = path.shift();
|
|
|
|
if (!target[key]) {
|
|
|
|
target[key] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
target = target[key];
|
|
|
|
}
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
// Set the specified value in the nested JSON structure
|
2011-03-31 06:32:47 +00:00
|
|
|
key = path.shift();
|
|
|
|
target[key] = value;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function clear (key)
|
|
|
|
// #### @key {string} Key to remove from this instance
|
|
|
|
// Removes the value for the specified `key` from this instance.
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
Memory.prototype.clear = function (key) {
|
|
|
|
var target = this.store,
|
2011-04-02 07:03:16 +00:00
|
|
|
path = nconf.path(key);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Remove the key from the set of `mtimes` (modified times)
|
|
|
|
//
|
|
|
|
delete this.mtimes[key];
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Scope into the object to get the appropriate nested context
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
while (path.length > 1) {
|
|
|
|
key = path.shift();
|
|
|
|
if (!target[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = target[key];
|
|
|
|
}
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
// Delete the key from the nested JSON structure
|
2011-03-31 06:32:47 +00:00
|
|
|
key = path.shift();
|
|
|
|
delete target[key];
|
|
|
|
return true;
|
2011-04-02 07:03:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function reset (callback)
|
|
|
|
// Clears all keys associated with this instance.
|
|
|
|
//
|
|
|
|
Memory.prototype.reset = function () {
|
|
|
|
this.mtimes = {};
|
|
|
|
this.store = {};
|
|
|
|
return true;
|
|
|
|
}
|