Comparing version 1.0.2 to 1.1.0
@@ -26,4 +26,5 @@ // Copyright (c) 2011 Daniel Ennis <aikar@aikar.co> | ||
var gettersIndex = []; | ||
function defineGetter(obj, objpath, fn) { | ||
obj = obj || global | ||
var nameIndex = {}; | ||
var fileIndex = {}; | ||
function getIndex(obj) { | ||
var idx = gettersIndex.indexOf(obj); | ||
@@ -36,3 +37,11 @@ if (idx == -1) { | ||
getters[idx] = {}; | ||
nameIndex[idx] = {}; | ||
fileIndex[idx] = {}; | ||
} | ||
return idx; | ||
} | ||
function defineGetter(obj, objpath, fn, fullPath) { | ||
obj = obj || global | ||
var idx = getIndex(obj); | ||
var getp1 = getters[idx]; | ||
@@ -42,6 +51,18 @@ var getp2 = getters[idx]; | ||
var tmpobj = obj; | ||
var curnamearray = []; | ||
objpath.forEach(function(val) { | ||
curnamearray.push(val) | ||
var curname = curnamearray.join('.'); | ||
if (typeof getp1[val] != 'object' || !getp1[val].children) { | ||
getp1[val] = { | ||
var self = getp1[val] = { | ||
parent: getp2, | ||
name: curname, | ||
children: {}, | ||
getter: function() { | ||
if (self.parent && self.parent.getter) { | ||
self.parent.getter(); | ||
return self.getter() | ||
} | ||
return undefined; | ||
}, | ||
create: function() { | ||
@@ -51,11 +72,11 @@ return {}; | ||
} | ||
nameIndex[idx][curname] = getp1[val]; | ||
} | ||
if (tmpobj && !tmpobj.__lookupGetter__(val)) { | ||
if (typeof tmpobj == 'object' && tmpobj[val] == 'object') { | ||
tmpobj = tmpobj[val] | ||
} else { | ||
if (typeof tmpobj == 'object' && tmpobj && !tmpobj.__lookupGetter__(val)) { | ||
if (tmpobj[val] === undefined) { | ||
(function applyGetter(val, currentp, currentobj) { | ||
currentobj.__defineGetter__(val, function() { | ||
currentp.getter = function () { | ||
delete currentobj[val]; | ||
var ret = currentp.create(currentobj, val); | ||
//console.error("getter executed", val, Object.keys(ret)) | ||
// if the callback function set the var for us, don't overwrite it. | ||
@@ -68,6 +89,13 @@ if (currentobj[val] == undefined) { | ||
}); | ||
currentp.getter = function() { | ||
return currentobj[val]; | ||
}; | ||
//console.error("returning", Object.keys(currentobj[val])); | ||
return currentobj[val]; | ||
}); | ||
}; | ||
currentobj.__defineGetter__(val, currentp.getter); | ||
})(val, getp1[val], tmpobj); | ||
tmpobj = null; | ||
} else { | ||
tmpobj = tmpobj[val] | ||
} | ||
@@ -82,2 +110,3 @@ } else { | ||
getp2.create = fn; | ||
fileIndex[idx][fullPath] = getp2; | ||
} | ||
@@ -136,5 +165,25 @@ | ||
} | ||
defineGetter(obj, objpath, load); | ||
defineGetter(obj, objpath, load, fullPath); | ||
} | ||
module.exports = registerAutoloader; | ||
module.exports = { | ||
autoload: registerAutoloader, | ||
loadByName: function(name, obj) { | ||
obj = obj || global; | ||
var idx = getIndex(obj); | ||
var p = nameIndex[idx][name] | ||
if (p) { | ||
return p.getter(); | ||
} | ||
return null; | ||
}, | ||
loadByFile: function(file, obj) { | ||
obj = obj || global; | ||
var idx = getIndex(obj); | ||
var p = fileIndex[idx][file] | ||
if (p) { | ||
return p.getter(); | ||
} | ||
return null; | ||
} | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "Autoloads JS Files (designed for Joose)", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"homepage": "http://aikar.co", | ||
@@ -8,0 +8,0 @@ "main": "autoloader.js", |
## About | ||
Autoloads JS files based on name when the class is needed. | ||
This style is subject to change and this module is not advertised yet. | ||
This is designed to be used with Joose classes in the "Foo.Bar.Baz" format | ||
This module removes the needs of using require() all over your files. Simply | ||
define the autoloader to your codebase, and use the names relative to the files. | ||
ie lib/Foo/Bar/Baz.js when you load 'lib/' makes Foo.Bar.Baz | ||
require('./lib/Foo/Bar/Baz.js') automatically and return the value. | ||
## Install | ||
Install with npm install autoloader | ||
Install with npm install autoloader | ||
## Usage | ||
Folder structure: | ||
/lib/ | ||
Foo/ | ||
Foo.js | ||
Bar.js | ||
test.js | ||
package.json (main: 'test.js') | ||
File contents: | ||
Foo.js: | ||
module.export = function() { | ||
console.log("Foo") | ||
Foo.Bar(); | ||
}; | ||
Bar.js: | ||
module.export = function() { | ||
console.log("Foo.Bar") | ||
}; | ||
test.js: | ||
require('autoloader').autoload(__dirname + '/lib') | ||
Foo(); | ||
loading the module would print to screen: | ||
Foo | ||
Foo.Bar | ||
ALL MODULES MUST RETURN AN OBJECT/FUNCTION. It can not return Scalar Values! | ||
## Custom Loaders | ||
If you pass a function as the 2nd argument, autoloader will execute that before | ||
requiring the file with the following arguments: | ||
function (fullPath, className, object, key) { } | ||
fullPath will be the full path to the module file to load, classname would | ||
be 'Foo.Bar.Baz' for example, object would be the object to add a new value | ||
to, and key is the key of object to assign the response. | ||
so if you load 'Foo', object is global, and key is Foo, likewise if you load | ||
Foo.Bar, object is Foo and key is 'Bar'. | ||
You can then do what ever magic it is you need to do, and optionally assign it yourself. | ||
If you don't assign it yourself, simply return the value and autoloader will | ||
assign it for you. | ||
## License | ||
The MIT License | ||
Copyright (c) 2011 Daniel Ennis <aikar@aikar.co> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
9824
4
0
173
95