dependency-injection
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -29,3 +29,3 @@ // Generated by CoffeeScript 1.6.3 | ||
DI.prototype.autowireArguments = function(method, args) { | ||
var arg, factory, i, methodArgs, num, result, self, _i, _ref, | ||
var arg, customArg, factory, i, methodArgs, num, result, self, _i, _ref, | ||
_this = this; | ||
@@ -49,3 +49,7 @@ if (args == null) { | ||
} else { | ||
if (typeof args[i] === 'undefined' || args[i] === '...') { | ||
customArg = typeof args[i] !== 'undefined'; | ||
if (!customArg || (customArg && (args[i] === '...' || args[i][0] === '@'))) { | ||
if (customArg && args[i][0] === '@') { | ||
arg = args[i].substr(1); | ||
} | ||
factory = false; | ||
@@ -101,3 +105,3 @@ if (arg.match(/Factory$/) !== null) { | ||
if (method.match(/^inject/) !== null) { | ||
service[method].apply(service, this.autowireArguments(service[method], [])); | ||
this.inject(service[method], service); | ||
} | ||
@@ -108,2 +112,14 @@ } | ||
DI.prototype.inject = function(fn, scope) { | ||
var args; | ||
if (scope == null) { | ||
scope = {}; | ||
} | ||
if (!(fn instanceof Function)) { | ||
throw new Error('Inject method can be called only on functions.'); | ||
} | ||
args = this.autowireArguments(fn, []); | ||
return fn.apply(scope, args); | ||
}; | ||
DI.prototype.findDefinitionByName = function(name, need) { | ||
@@ -124,2 +140,6 @@ if (need == null) { | ||
DI.prototype.getByName = function(name) { | ||
return this.get(name); | ||
}; | ||
DI.prototype.get = function(name) { | ||
return this.findDefinitionByName(name).getInstance(); | ||
@@ -126,0 +146,0 @@ }; |
{ | ||
"name": "dependency-injection", | ||
"description": "Dependency injection with configuration and autowire for node js and browser", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "David Kudera", |
@@ -62,3 +62,3 @@ # Dependency injection | ||
``` | ||
di.getByName('application'); | ||
di.get('application'); | ||
di.create('application'); | ||
@@ -90,3 +90,3 @@ di.getFactory('application'); | ||
### getByName | ||
### get | ||
@@ -158,3 +158,3 @@ Some services may be "singleton" type (not really singleton but with one instance in whole application), which application | ||
Now in most cases you just have to use getByName method just once for create instance of your base application service | ||
Now in most cases you just have to use `get` method just once for create instance of your base application service | ||
and other services will be automatically injected. | ||
@@ -192,2 +192,24 @@ | ||
## Links to other services | ||
When you have got for example foreign library registered as service in this DI and want to autowire some other service into | ||
it, you have to use their names of methods arguments. | ||
Another possibility is to set these services in your config. | ||
``` | ||
{ | ||
"services": { | ||
"foreignLibrary": { | ||
"service": "path/to/service", | ||
"arguments": [ | ||
"@translator" | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
Now this `foreignLibrary` will gets your `translator` service in constructor. | ||
## Autowiring DI | ||
@@ -242,4 +264,19 @@ | ||
## Inject method | ||
For simple injecting services into your functions, you can use method `inject`. | ||
``` | ||
di.inject(function(application) { | ||
application.doSomeMagic(); | ||
}); | ||
``` | ||
## Changelog | ||
* 1.6.0 | ||
+ Added `get` method, `getByName` is now deprecated | ||
+ Added `inject` method | ||
+ Autowiring with @ | ||
* 1.5.2 | ||
@@ -246,0 +283,0 @@ + Add setup into properties |
@@ -61,3 +61,3 @@ // Generated by CoffeeScript 1.6.3 | ||
}); | ||
return it('should return array with services from params if they are not in definition', function() { | ||
it('should return array with services from params if they are not in definition', function() { | ||
var app; | ||
@@ -67,2 +67,10 @@ app = new Application([]); | ||
}); | ||
return it('should inject another service by at char', function() { | ||
var fn; | ||
fn = function(variable) { | ||
return variable; | ||
}; | ||
di.addService('array', Array); | ||
return di.autowireArguments(fn, ['@array']).should.be.eql([[]]); | ||
}); | ||
}); | ||
@@ -107,6 +115,6 @@ describe('#createInstance()', function() { | ||
}); | ||
describe('#getByName()', function() { | ||
describe('#get()', function() { | ||
it('should return instance of Application with all dependencies', function() { | ||
var app; | ||
app = di.getByName('application'); | ||
app = di.get('application'); | ||
app.should.be.an.instanceOf(Application); | ||
@@ -118,6 +126,6 @@ app.namespace.should.be.equal('simq'); | ||
it('should return always the same instance of Application', function() { | ||
return di.getByName('application').should.be.equal(di.getByName('application')); | ||
return di.get('application').should.be.equal(di.get('application')); | ||
}); | ||
it('should return info array without instantiating it', function() { | ||
return di.getByName('info').should.be.eql(['hello']); | ||
return di.get('info').should.be.eql(['hello']); | ||
}); | ||
@@ -127,3 +135,3 @@ it('should not set services which are not autowired', function() { | ||
return (function() { | ||
return di.getByName('application'); | ||
return di.get('application'); | ||
}).should["throw"](); | ||
@@ -133,3 +141,3 @@ }); | ||
di.findDefinitionByName('application').addSetup('setDi'); | ||
return di.getByName('application').di.should.be.equal(di); | ||
return di.get('application').di.should.be.equal(di); | ||
}); | ||
@@ -139,3 +147,3 @@ it('should autowire di container factory into Application instance', function() { | ||
di.findDefinitionByName('application').addSetup('setDiFactory'); | ||
factory = di.getByName('application').diFactory; | ||
factory = di.get('application').diFactory; | ||
factory.should.be.an.instanceOf(Function); | ||
@@ -146,3 +154,3 @@ return factory().should.be.equal(di); | ||
di.findDefinitionByName('application').addSetup('info', 'by property'); | ||
return di.getByName('application').info.should.be.equal('by property'); | ||
return di.get('application').info.should.be.equal('by property'); | ||
}); | ||
@@ -155,3 +163,3 @@ }); | ||
}); | ||
return describe('#getFactory()', function() { | ||
describe('#getFactory()', function() { | ||
return it('should return callable factory for Application', function() { | ||
@@ -164,2 +172,16 @@ var factory; | ||
}); | ||
return describe('#inject()', function() { | ||
it('should inject some service into annonymous function', function(done) { | ||
di.addService('array', Array); | ||
return di.inject(function(array) { | ||
array.should.be.eql([]); | ||
return done(); | ||
}); | ||
}); | ||
return it('should throw an error if inject method is not called on function', function() { | ||
return (function() { | ||
return di.inject(''); | ||
}).should["throw"](); | ||
}); | ||
}); | ||
}); | ||
@@ -166,0 +188,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39021
498
324