Skip to content

Commit

Permalink
Merge pull request #1 from chalasr/multi-tag-def
Browse files Browse the repository at this point in the history
Allow defining many tags per service
  • Loading branch information
Tom32i authored Jun 13, 2017
2 parents 792d6cb + 0bbbbe2 commit 13319ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Container {
* @param {String} name
* @param {Scalar|Function} value
* @param {Array} dependencies
* @param {String} tag
* @param {String|String[]} tags
*/
register(name, value, dependencies = [], tag = null) {
register(name, value, dependencies = [], tags = []) {
if (Container.isConstructor(value)) {
this.registerDefinition(name, value, dependencies, tag);
this.registerDefinition(name, value, dependencies, tags);
} else {
this.registerParameter(name, value);
}
Expand All @@ -39,11 +39,12 @@ class Container {
* @param {String} name
* @param {Function} classname
* @param {Array} dependencies
* @param {String} tag
* @param {String|String[]} tags
*/
registerDefinition(name, classname, dependencies = [], tag = null) {
registerDefinition(name, classname, dependencies = [], tags = []) {
tags = typeof (tags) === 'string' ? [tags] : tags;
this.ensureUniqueness(name);
this.services.set(name, { classname, name, dependencies, tag });
this.services.set(name, { classname, name, dependencies, tags });
}

/**
Expand Down Expand Up @@ -98,16 +99,16 @@ class Container {
}

/**
* Get tagged service
* Get services for a given tag.
*
* @param {String} tag
*
* @return {Array}
*/
getTaggedService(tag) {
getTaggedServices(tag) {
return Array
.from(this.services.values())
.filter(definition => definition.tag === tag)
.filter(definition => definition.tags.includes(tag))
;
}

Expand Down
20 changes: 19 additions & 1 deletion test/Container.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Container from '../src/Container';
describe('Container', () => {
const container = new Container();
class MyClass { }
container.registerDefinition('my:class', MyClass);
class BarClass { }
container.registerDefinition('my:class', MyClass, [], ['foo_tag']);
container.registerDefinition('bar', BarClass, [], ['foo_tag', 'bar_tag']);

describe('Container.get()', () => {
test('should return the expected service when asked for', () => {
Expand All @@ -16,4 +18,20 @@ describe('Container', () => {
}).toThrowError('Service or parameter "unexisting:service" not found.');
});
});

describe('Container.getTaggedServices()', () => {
test('should return all services with a given tag', () => {
expect(container.getTaggedServices('foo_tag')).toEqual([
{ classname: MyClass, dependencies: [], name: 'my:class', tags: ['foo_tag'] },
{ classname: BarClass, dependencies: [], name: 'bar', tags: ['foo_tag', 'bar_tag'] }
]);
expect(container.getTaggedServices('bar_tag')).toEqual([
{ classname: BarClass, dependencies: [], name: 'bar', tags: ['foo_tag', 'bar_tag'] }
]);
});

test('should return an empty array on undefined tag', () => {
expect(container.getTaggedServices('foobar')).toEqual([]);
});
});
});

0 comments on commit 13319ae

Please sign in to comment.