Skip to content

Commit

Permalink
Adding the function exec to run a function injecting dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiofilhowz committed Nov 16, 2014
1 parent bf3d102 commit 3d3d46d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ available functions
`importModule ( $module ) : this function will execute the function call and then add to the context`

`loadDir ( dir ) : loads an entire directory recursively, searching for .js files to import`

`exec ( func ) : this function will execute the "func" function injecting all dependencies based on function args`

usage
=====
Expand Down Expand Up @@ -53,6 +55,12 @@ It's also possible to pass a custom import function to loadDir, for example

**Note**: All load functions are sync and are meant to be called on startup.

It's possible to exec a function loading all dependencies

acqua.exec(function (dependencyOne, dependencyTwo) {
// function call
});

author
=====
Sérgio Marcelino ([email protected])
Expand Down
10 changes: 9 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ First Release of Acqua

# Version 0.0.2

**[BUG]** Fixed a problem that was sharing every function in the same object instance, causing conflicts in 2 modules with the same function name.
**[BUG]** Fixed a problem that was sharing every function in the same object instance, causing conflicts in 2 modules with the same function name.

# Version 0.0.3

**[FEATURE]** Adding the function exec to run a function injecting dependencies, example:

acqua.exec(function (dependencyOne, dependencyTwo) {
// function call
});
22 changes: 18 additions & 4 deletions lib/acqua.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ Acqua.prototype.getParamNames = function (func) {
Acqua.prototype.getFunctionName = function (func) {
var array = /^function\s+([\w\$]+)\(/.exec(func.toString());
return array === null ? null : array[1];
}
};

Acqua.prototype.execModule = function ($module, modulePath) {
Acqua.prototype.getDependencies = function (fn) {

var params = this.getParamNames($module),
var params = this.getParamNames(fn),
dependencies = [],
self = this;

Expand All @@ -66,6 +66,20 @@ Acqua.prototype.execModule = function ($module, modulePath) {
dependencies.push(dependency);
});

return dependencies;

};

Acqua.prototype.exec = function (fn) {

var dependencies = this.getDependencies(fn);
return fn.apply({}, dependencies);

};

Acqua.prototype.execModule = function ($module, modulePath) {

var dependencies = this.getDependencies($module);
if (_.isString(modulePath)) {
this.log('Importing module: ' + modulePath);
}
Expand All @@ -85,7 +99,7 @@ Acqua.prototype.importModule = function (modulePath) {
result = this.execModule($module, modulePath);

if (name === null) {
this.log('─ Not a named function, just executing the function, this module will not be added to the context');
this.log('─ Not a named function, just executing the function, this module will not be added to the context');
} else {
this.add(name, result);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acqua",
"version": "0.0.2",
"version": "0.0.3",
"description": "A NodeJS Module for Dependency Injection",
"main": "index.js",
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions test/acqua.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ describe('Tests on function Acqua', function () {

});

it('should inject dependencies correctly', function () {

var acqua = new Acqua();

acqua.add('one', 1);
acqua.add('two', 2);
acqua.add('three', 3);

acqua.exec(function (one, two, three) {
expect(one).to.be.equal(1);
expect(two).to.be.equal(2);
expect(three).to.be.equal(3);
});

});

});

0 comments on commit 3d3d46d

Please sign in to comment.