Skip to content

Instantly share code, notes, and snippets.

@randwa1k

randwa1k/index.js

Created Jul 31, 2014
Embed
What would you like to do?
myApp/node_modules/dependable/index.js
// Generated by CoffeeScript 1.3.3
(function() {
var existsSync, fs, path, _ref;
path = require('path');
fs = require('fs');
existsSync = (_ref = fs.existsSync) != null ? _ref : path.existsSync;
exports.container = function() {
var argList, container, factories, get, haveVisited, load, loaddir, loadfile, notEmpty, register, registerOne, resolve, toFactory;
factories = {};
register = function(name, func) {
var hash, _results;
if (name === Object(name)) {
hash = name;
_results = [];
for (name in hash) {
func = hash[name];
_results.push(registerOne(name, func));
}
return _results;
} else {
return registerOne(name, func);
}
};
registerOne = function(name, func) {
if (!(func != null)) {
throw new Error("cannot register null function");
}
return factories[name] = toFactory(func);
};
load = function(file) {
var exists, stats;
exists = existsSync(file);
if (exists) {
stats = fs.statSync(file);
if (stats.isDirectory()) {
return loaddir(file);
}
}
return loadfile(file);
};
loadfile = function(file) {
var module, name;
module = file.replace(/\.\w+$/, "");
name = path.basename(module).replace(/\-(\w)/g, function(match, letter) {
return letter.toUpperCase();
});
return register(name, require(module));
};
loaddir = function(dir) {
var file, filenames, files, stats, _i, _len, _results;
filenames = fs.readdirSync(dir);
files = filenames.map(function(file) {
return path.join(dir, file);
});
_results = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (!file.match(/\.(js|coffee)$/)) {
continue;
}
stats = fs.statSync(file);
if (stats.isFile()) {
_results.push(loadfile(file));
} else {
_results.push(void 0);
}
}
return _results;
};
toFactory = function(func) {
if (typeof func === "function") {
return {
func: func,
required: argList(func)
};
} else {
return {
func: function() {
return func;
},
required: []
};
}
};
argList = function(func) {
var match, required;
match = func.toString().match(/function.*?\(([\s\S]*?)\)/);
if (!(match != null)) {
throw new Error("could not parse function arguments: " + (func != null ? func.toString() : void 0));
}
required = match[1].split(",").filter(notEmpty).map(function(str) {
return str.trim();
});
return required;
};
notEmpty = function(a) {
return a;
};
get = function(name, overrides, visited) {
var dependencies, factory, instance, isOverridden;
if (visited == null) {
visited = [];
}
isOverridden = overrides != null;
if (haveVisited(visited, name)) {
throw new Error("circular dependency with '" + name + "'");
}
visited = visited.concat(name);
factory = factories[name];
if (!(factory != null)) {
throw new Error("dependency '" + name + "' was not registered");
}
if ((factory.instance != null) && !isOverridden) {
return factory.instance;
}
dependencies = factory.required.map(function(name) {
if ((overrides != null ? overrides[name] : void 0) != null) {
return overrides != null ? overrides[name] : void 0;
} else {
return get(name, overrides, visited);
}
});
instance = factory.func.apply(factory, dependencies);
if (!isOverridden) {
factory.instance = instance;
}
return instance;
};
haveVisited = function(visited, name) {
var isName;
isName = function(n) {
return n === name;
};
return visited.filter(isName).length;
};
resolve = function(overrides, func) {
if (!func) {
func = overrides;
overrides = null;
}
register("__temp", func);
return get("__temp", overrides);
};
container = {
get: get,
resolve: resolve,
register: register,
load: load
};
container.register("_container", container);
return container;
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment