Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #134 from Medium/kyle-inject-ctor
Browse files Browse the repository at this point in the history
Modify .ctor to inject params
  • Loading branch information
kylehg committed Jul 17, 2015
2 parents a8a0582 + 34b2b9d commit 12bea51
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
27 changes: 19 additions & 8 deletions lib/NodeDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,8 @@ NodeDefinition.prototype.fn = function (fn) {
* @param {function(new:Object, ...)} Ctor
*/
NodeDefinition.prototype.ctor = function (Ctor) {
this._setArgsFromFunction(Ctor)

/** @constructor */
function SubCtor(argList) {
Ctor.apply(this, argList)
Expand All @@ -596,17 +598,28 @@ NodeDefinition.prototype.ctor = function (Ctor) {
* @param {!Function} fn
*/
NodeDefinition.prototype.inject = function (fn) {
this._setArgsFromFunction(fn)
return this.fn(fn)
}

/**
* Re-arrange the node params from the function definition
*
* If the parameters can't be found, this will throw an error.
*
* @param {!Function} fn
*/
NodeDefinition.prototype._setArgsFromFunction = function (fn) {
var paramNames = utils.parseFnParams(fn)

var shortArgsNames = []
var i = 0
for (i = 0; i < this._args.length; i++) {
shortArgsNames.push(utils.getNodeInjectorName(this._args[i].aliasRealName))
}
var shortArgsNames = this._args.map(function (arg) {
return utils.getNodeInjectorName(arg.aliasRealName)
})

// Sort the arguments so that the parameters get injected in the
// Sort the arguments so that the parameters get injected in the
// right order.
var newArgs = []
var i
for (i = 0; i < paramNames.length; i++) {
var idx = shortArgsNames.indexOf(paramNames[i])
if (idx === -1) {
Expand All @@ -621,8 +634,6 @@ NodeDefinition.prototype.inject = function (fn) {
// Push any args that are not included.
newArgs.push.apply(newArgs, this._args)
this._args = newArgs

return this.fn(fn)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shepherd",
"description": "asynchronous dependency injection for node.js",
"version": "2.4.2",
"version": "2.5.0",
"homepage": "https://github.com/Medium/shepherd",
"authors": [
"Jeremy Stanley <[email protected]> (https://github.com/azulus)",
Expand Down
23 changes: 23 additions & 0 deletions test/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,26 @@ builder.add(function testContructor(test) {
test.equal(2, myType.b)
})
})


builder.add(function testContructorInjects(test) {
function MyType(a, b) {
this.a = a
this.b = b
}

graph.add('myType')
.args('b', 'c', 'a')
.ctor(MyType)

return graph.newBuilder()
.builds('myType')
.using({'a': graph.literal(1)}, {'b': graph.literal(2)}, {'c': graph.literal(3)})
.run()
.then(function (data) {
var myType = data['myType']
test.ok(myType instanceof MyType)
test.equal(1, myType.a)
test.equal(2, myType.b)
})
})

0 comments on commit 12bea51

Please sign in to comment.