diff --git a/README.md b/README.md index 1b825fa..273f0d7 100644 --- a/README.md +++ b/README.md @@ -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 ===== @@ -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 (sergiofilhow@gmail.com) diff --git a/changelog.md b/changelog.md index e664540..58acb74 100644 --- a/changelog.md +++ b/changelog.md @@ -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. \ No newline at end of file +**[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 + }); diff --git a/lib/acqua.js b/lib/acqua.js index 6766faa..2dd1d47 100644 --- a/lib/acqua.js +++ b/lib/acqua.js @@ -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; @@ -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); } @@ -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); } diff --git a/package.json b/package.json index 55de7dd..06bf536 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/acqua.test.js b/test/acqua.test.js index ee6921d..6335dd7 100644 --- a/test/acqua.test.js +++ b/test/acqua.test.js @@ -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); + }); + + }); + }); \ No newline at end of file