Permalink
Browse files

Config, Array & Object helper

  • Loading branch information...
1 parent b762c5c commit 6c089e5091af1ce8c010ec00b9059be1823ad041 @vovazolotoy vovazolotoy committed Aug 24, 2011
Showing with 136 additions and 20 deletions.
  1. +1 −6 johana.js
  2. +128 −0 system/prototypes/johana/arr.js
  3. +7 −14 system/prototypes/johana/core.js
View
@@ -42,9 +42,4 @@ require(SYSPATH + 'prototypes/autoload');
// Bootstrap the application
require(APPPATH + 'bootstrap');
-var view = new View('hello');
-var q = {name: 'test1'};
-//View.setGlobal(q);
-view.set('name', '123');
-//view.
-console.log(view.render());
+console.log(Johana.config('vova.name'));
@@ -12,6 +12,134 @@ JohanaArr = function()
};
/**
+ * @var String default delimiter for path()
+ */
+JohanaArr.delimiter = '.';
+
+/**
+ * Gets a value from an object using a dot separated path.
+ *
+ * // Get the value of obj.foo.bar
+ * var value = Arr.path(obj, 'foo.bar');
+ *
+ * Using a wildcard "*" will search intermediate objects and return an object.
+ *
+ * // Get the values of "color" in theme
+ * var colors = Arr.path(obj, 'theme.*.color');
+ *
+ * // Using an array of keys
+ * var colors = Arr.path(array, ['theme', '*', 'color']);
+ *
+ * @param Object object to search
+ * @param mixed key path string (delimiter separated) or array of keys
+ * @param mixed default value if the path is not set
+ * @param String key path delimiter
+ * @return mixed
+ */
+JohanaArr.path = function(obj, path, def, delimiter)
+{
+ def = def || null; delimiter = delimiter || null;
+
+ if (typeof obj != 'object')
+ {
+ // This is not an array!
+ return def;
+ }
+
+ if (typeof path == 'object' && path.length)
+ {
+ // The path has already been separated into keys
+ var keys = path;
+ }
+ else
+ {
+ if (obj[path] !== undefined)
+ {
+ // No need to do extra processing
+ return obj[path];
+ }
+
+ if (delimiter === null)
+ {
+ // Use the default delimiter
+ delimiter = Arr.delimiter;
+ }
+
+ // Quote delimiter to RegExp
+ var quoted = delimiter.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
+
+ // Remove starting delimiters and spaces
+ path = path.replace(new RegExp('^[\\s' + quoted + ']+'), '');
+
+ // Remove ending delimiters, spaces, and wildcards
+ path = path.replace(new RegExp('[\\s\\*' +quoted + ']+$'), '');
+
+ var keys = path.split(delimiter);
+ }
+
+ while (keys)
+ {
+ var key = keys.shift();
+
+ if (obj[key] != undefined)
+ {
+ if (keys.length)
+ {
+ if (typeof obj[key] == 'object')
+ {
+ // Dig down into the next part of the path
+ obj = obj[key];
+ }
+ else
+ {
+ // Unable to dig deeper
+ break;
+ }
+ }
+ else
+ {
+ // Found the path requested
+ return obj[key];
+ }
+ }
+ else if (key === '*')
+ {
+ // Handle wildcards
+ var values = [];
+
+ for (var v in obj)
+ {
+ var value = Arr.path(obj[v], keys.join('.'));
+
+ if (value)
+ {
+ values.push(value);
+ }
+ }
+
+ if (values.length)
+ {
+ // Found the values requested
+ return values;
+ }
+ else
+ {
+ // Unable to dig deeper
+ break;
+ }
+ }
+ else
+ {
+ // Unable to dig deeper
+ break;
+ }
+ }
+
+ // Unable to find the value requested
+ return def;
+};
+
+/**
* Merges 2 objects
*
* @param Object initial object
@@ -125,9 +125,9 @@ var _paths = [APPPATH, SYSPATH];
var _files = [];
/**
- * @var Boolean Has the file path cache changed during this execution? Used internally when when caching is true in [Johana.init]
+ * @var Object config cache
*/
-var _filesChanged = false;
+var _config = {};
/**
@@ -294,10 +294,7 @@ JohanaCore.deinit = function()
_modules = _files = [];
_paths = [APPPATH, SYSPATH];
- // Reset file cache status
- _filesChanged = false;
-
- // Kohana is no longer initialized
+ // Johana is no longer initialized
_init = false;
}
};
@@ -344,7 +341,7 @@ JohanaCore.autoLoad = function(lib)
* Changes the currently enabled modules. Module paths may be relative
* or absolute, but must point to a directory:
*
- * Kohana.modules({'modules/foo': MODPATH+'bar'});
+ * Johana.modules({'modules/foo': MODPATH+'bar'});
*
* @param Object list of module paths
* @return Object enabled modules
@@ -561,8 +558,6 @@ JohanaCore.findFile = function(dir, file, ext, list)
// Add the path to the cache
_files[path + (list ? '_list' : '_path')] = found;
- // Files have been changed
- _filesChanged = true;
}
if (benchmark)
@@ -591,8 +586,6 @@ JohanaCore.findFile = function(dir, file, ext, list)
* @param String group name
* @return Config
*/
-var _configCache = {};
-
JohanaCore.config = function(group)
{
var path = false;
@@ -607,10 +600,10 @@ JohanaCore.config = function(group)
group = group.substring(0, dot);
}
- if (_configCache[group] === undefined)
+ if (_config[group] === undefined)
{
// Load the config group into the cache
- _configCache[group] = Johana.conf.load(group);
+ _config[group] = Johana.conf.load(group);
}
if (path !== false)
@@ -619,7 +612,7 @@ JohanaCore.config = function(group)
}
else
{
- return _configCache[group];
+ return _config[group];
}
};

0 comments on commit 6c089e5

Please sign in to comment.