From eea056f3376d29a02cf7b335058e44932b3d5a69 Mon Sep 17 00:00:00 2001 From: Dennis Wilson Date: Thu, 23 Apr 2015 15:33:02 +0200 Subject: [PATCH 1/4] feat(loaders): value dependencies for runtime generated components --- lib/index.js | 1 + lib/loaders/value.js | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 lib/loaders/value.js 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; + }; +} From 76fa6c919e29ec28da2cab827ad407cbeb4b837a Mon Sep 17 00:00:00 2001 From: Dennis Wilson Date: Thu, 23 Apr 2015 16:57:07 +0200 Subject: [PATCH 2/4] docs(readme): quick api listing --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 557e71c..0c4a9c4 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)__ From 2eed04b4c6cc25b134997c5c65c87dca7529d756 Mon Sep 17 00:00:00 2001 From: Dennis Wilson Date: Thu, 23 Apr 2015 16:58:33 +0200 Subject: [PATCH 3/4] chore(readme): more pretty format for quick api --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0c4a9c4..aa56337 100644 --- a/README.md +++ b/README.md @@ -188,27 +188,27 @@ while mocking out network access entirely. ```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). -```` From 83685168ee710e68da3bae9dc1146ea117749f22 Mon Sep 17 00:00:00 2001 From: Dennis Wilson Date: Wed, 1 Jul 2015 23:32:52 +0200 Subject: [PATCH 4/4] fix(container): inject returns result --- lib/container.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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;