Skip to content

Commit

Permalink
added method getByPath
Browse files Browse the repository at this point in the history
  • Loading branch information
sakren committed Dec 12, 2013
1 parent 56b0a27 commit d2496fa
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ service is clearly is.

This method will create one instance of service and store it. Every other time, this instance will be returned.

### getByPath

Same as `get` method, but this accepts path to node module (like in your service configuration)

### create

Method create will just create new instance of service and will not store it.
Expand Down Expand Up @@ -345,6 +349,7 @@ $ npm test

* 2.0.0
+ Removed autowiring into `inject` methods (BC break!)
+ Added method `getByPath`

* 1.8.0
+ Better tests (mocha does not need to be installed globally)
Expand Down
22 changes: 22 additions & 0 deletions lib/DI.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dependency-injection",
"description": "Dependency injection with configuration and autowire for node js and browser",
"version": "1.8.0",
"version": "2.0.0",
"author": {
"name": "David Kudera",
"email": "sakren@gmail.com"
Expand Down
20 changes: 20 additions & 0 deletions src/DI.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class DI

services: null

paths: null

reserved: ['di']

creating: null
Expand All @@ -18,13 +20,18 @@ class DI
@services =
di: di

@paths = {}
@creating = []


addService: (name, service, args = []) ->
if name in @reserved
throw new Error "DI: name '#{name}' is reserved by DI."

if typeof service == 'string'
service = require.resolve(service)
@paths[service] = name

@services[name] = new Service(@, name, service, args)
return @services[name]

Expand Down Expand Up @@ -73,6 +80,19 @@ class DI
return @get(name)


getByPath: (path) ->
error = false
try
path = require.resolve(path)
catch e
error = true

if typeof @paths[path] != 'undefined' && !error
return @get(@paths[path])

return null


get: (name) ->
return @findDefinitionByName(name).getInstance()

Expand Down
36 changes: 34 additions & 2 deletions test/browser/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@
DI = (function() {
DI.prototype.services = null;

DI.prototype.paths = null;

DI.prototype.reserved = ['di'];

DI.prototype.creating = null;
Expand All @@ -494,6 +496,7 @@
this.services = {
di: di
};
this.paths = {};
this.creating = [];
}

Expand All @@ -504,6 +507,10 @@
if (__indexOf.call(this.reserved, name) >= 0) {
throw new Error("DI: name '" + name + "' is reserved by DI.");
}
if (typeof service === 'string') {
service = require.resolve(service);
this.paths[service] = name;
}
this.services[name] = new Service(this, name, service, args);
return this.services[name];
};
Expand Down Expand Up @@ -570,6 +577,21 @@
return this.get(name);
};

DI.prototype.getByPath = function(path) {
var e, error;
error = false;
try {
path = require.resolve(path);
} catch (_error) {
e = _error;
error = true;
}
if (typeof this.paths[path] !== 'undefined' && !error) {
return this.get(this.paths[path]);
}
return null;
};

DI.prototype.get = function(name) {
return this.findDefinitionByName(name).getInstance();
};
Expand Down Expand Up @@ -1234,6 +1256,16 @@
}).to["throw"](Error, 'Circular reference detected for services: first, second, third, fourth.');
});
});
describe('#getByPath()', function() {
it('should return service by require path', function() {
di.addService('app', "" + dir + "/Application");
return expect(di.getByPath("" + dir + "/Application")).to.be.an["instanceof"](Application);
});
return it('should return null for not auto required services', function() {
di.addService('info', ['hello']).setInstantiate(false);
return expect(di.getByPath('info')).to.not.exists;
});
});
describe('#create()', function() {
return it('should return always new instance of Application', function() {
return expect(di.create('application')).to.not.be.equal(di.create('application'));
Expand Down Expand Up @@ -1596,7 +1628,7 @@
return {
"name": "dependency-injection",
"description": "Dependency injection with configuration and autowire for node js and browser",
"version": "1.8.0",
"version": "2.0.0",
"author": {
"name": "David Kudera",
"email": "sakren@gmail.com"
Expand Down Expand Up @@ -1704,7 +1736,7 @@
, 'recursive-merge': function(exports, module) { module.exports = window.require('recursive-merge/lib/Merge.js'); }

});
require.__setStats({"/lib/Service.js":{"atime":1386858310000,"mtime":1386858307000,"ctime":1386858307000},"/lib/Helpers.js":{"atime":1386856820000,"mtime":1386856806000,"ctime":1386856806000},"/lib/DI.js":{"atime":1386858310000,"mtime":1386858307000,"ctime":1386858307000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1386835303000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1386835303000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"/test/browser/tests/DI.coffee":{"atime":1386857629000,"mtime":1386857625000,"ctime":1386857625000},"/test/browser/tests/Helpers.coffee":{"atime":1386857560000,"mtime":1386857556000,"ctime":1386857556000},"/lib/DIConfigurator.js":{"atime":1386856820000,"mtime":1386856806000,"ctime":1386856806000},"/test/data/Application.coffee":{"atime":1386857310000,"mtime":1386857310000,"ctime":1386857310000},"/test/data/Http.coffee":{"atime":1386835304000,"mtime":1384940373000,"ctime":1384940373000},"/package.json":{"atime":1386857523000,"mtime":1386857522000,"ctime":1386857522000},"easy-configuration/package.json":{"atime":1386835040000,"mtime":1385450929000,"ctime":1385450929000}});
require.__setStats({"/lib/Service.js":{"atime":1386858310000,"mtime":1386858307000,"ctime":1386858307000},"/lib/Helpers.js":{"atime":1386856820000,"mtime":1386856806000,"ctime":1386856806000},"/lib/DI.js":{"atime":1386862672000,"mtime":1386862668000,"ctime":1386862668000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1386835303000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1386835303000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1386835304000,"mtime":1385411214000,"ctime":1385450928000},"/test/browser/tests/DI.coffee":{"atime":1386862718000,"mtime":1386862696000,"ctime":1386862696000},"/test/browser/tests/Helpers.coffee":{"atime":1386857560000,"mtime":1386857556000,"ctime":1386857556000},"/lib/DIConfigurator.js":{"atime":1386856820000,"mtime":1386856806000,"ctime":1386856806000},"/test/data/Application.coffee":{"atime":1386861722000,"mtime":1386861722000,"ctime":1386861722000},"/test/data/Http.coffee":{"atime":1386835304000,"mtime":1384940373000,"ctime":1384940373000},"/package.json":{"atime":1386862710000,"mtime":1386862710000,"ctime":1386862710000},"easy-configuration/package.json":{"atime":1386835040000,"mtime":1385450929000,"ctime":1385450929000}});
require.version = '5.5.1';

/** run section **/
Expand Down
10 changes: 10 additions & 0 deletions test/browser/tests/DI.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ describe 'DI', ->
di.addService('fourth', (first) ->)
expect( -> di.get('first')).to.throw(Error, 'Circular reference detected for services: first, second, third, fourth.')

describe '#getByPath()', ->

it 'should return service by require path', ->
di.addService('app', "#{dir}/Application")
expect(di.getByPath("#{dir}/Application")).to.be.an.instanceof(Application)

it 'should return null for not auto required services', ->
di.addService('info', ['hello']).setInstantiate(false)
expect(di.getByPath('info')).to.not.exists

describe '#create()', ->

it 'should return always new instance of Application', ->
Expand Down
10 changes: 10 additions & 0 deletions test/node/DI.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ describe 'DI', ->
di.addService('fourth', (first) ->)
expect( -> di.get('first')).to.throw(Error, 'Circular reference detected for services: first, second, third, fourth.')

describe '#getByPath()', ->

it 'should return service by require path', ->
di.addService('app', "#{dir}/Application")
expect(di.getByPath("#{dir}/Application")).to.be.an.instanceof(Application)

it 'should return null for not auto required services', ->
di.addService('info', ['hello']).setInstantiate(false)
expect(di.getByPath('info')).to.not.exists

describe '#create()', ->

it 'should return always new instance of Application', ->
Expand Down
10 changes: 10 additions & 0 deletions test/node/DI.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d2496fa

Please sign in to comment.