diff --git a/README.md b/README.md index 557e71c..aa56337 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,34 @@ Note that now the `mysql` module is injected by Electrolyte, rather than explicitly `require()`'d. This makes it easy to write tests for this component while mocking out network access entirely. +## Quick-API + +```javascript +IoC.loader(***loader***); +```` + +Registers a Component to the IoC Container. ***loader*** is the result, given by on of the following component loaders. + +```javascript +IoC.node_modules(***moduleName***); +```` + +Creates a loader for a previously installed NPM package. Returns the loader, which is a function. + +```javascript +IoC.node(***searchPath***); +```` + +Creates a loader for a any script below the given directory. Let's say you directory is named ***app/***, and it contains subdirectories with a component named ***app/services/hello.js***, you will be able to @require "services/hello" later. Returns the loader, which is a function. + +```javascript +IoC.value(***componentName***, ***component***); +```` + +Creates a loader for a given value/component. Pretty useful when you are generating objects during runtime (configuration files etc). + + + ## Examples - __[Express](https://github.com/jaredhanson/electrolyte/tree/master/examples/express)__ diff --git a/lib/container.js b/lib/container.js index 75547ae..2f68c4e 100644 --- a/lib/container.js +++ b/lib/container.js @@ -81,6 +81,24 @@ Container.prototype.create = function(id, parent) { return comp.create(this); } +/** + */ +Container.prototype.inject = function(fn) { + var dependencies = fn['@require'].map(function(id) { + var result; + + if (!this._o[id]) { + this._loadModule(id); + } + + result = this._o[id] ? this._o[id].obj : this._o[id]; + + return result; + }, this); + + return fn.apply(fn, dependencies); +}; + Container.prototype.factory = function(id, dependencies, fn, sid) { if (typeof dependencies == 'function') { fn = dependencies; diff --git a/lib/index.js b/lib/index.js index 2b723d2..4923fd5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,3 +7,4 @@ exports.Container = Container; exports.node = require('./loaders/node'); exports.node_modules = require('./loaders/node_modules'); +exports.value = require('./loaders/value'); diff --git a/lib/loaders/value.js b/lib/loaders/value.js new file mode 100644 index 0000000..f237b4f --- /dev/null +++ b/lib/loaders/value.js @@ -0,0 +1,5 @@ +module.exports = function(key, value) { + return function(id) { + return id === key ? value : undefined; + }; +}