Skip to content

Commit

Permalink
fixes alias
Browse files Browse the repository at this point in the history
  • Loading branch information
matuszeman committed May 24, 2017
1 parent f647b71 commit 6b8967e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 36 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ dic.getAsync('myApp').then(app => {
<dt><a href="#Dic">Dic</a></dt>
<dd><p>Dependency injection container</p>
<p>For more usage examples see: <a href="#Dic+instance">instance</a>, <a href="#Dic+class">class</a>, <a href="#Dic+factory">factory</a>,
<a href="#Dic+asyncFactory">asyncFactory</a>, <a href="#Dic+bindChild">bindChild</a>.</p>
<a href="#Dic+asyncFactory">asyncFactory</a>, <a href="#Dic+bind">bind</a>.</p>
</dd>
</dl>

Expand Down Expand Up @@ -224,7 +224,7 @@ Set up Dic according the config
Dependency injection container

For more usage examples see: [instance](#Dic+instance), [class](#Dic+class), [factory](#Dic+factory),
[asyncFactory](#Dic+asyncFactory), [bindChild](#Dic+bindChild).
[asyncFactory](#Dic+asyncFactory), [bind](#Dic+bind).

**Kind**: global class

Expand All @@ -239,7 +239,7 @@ For more usage examples see: [instance](#Dic+instance), [class](#Dic+class), [fa
* [.get(name)](#Dic+get) ⇒ <code>\*</code>
* [.getAsync(name)](#Dic+getAsync) ⇒ <code>\*</code>
* [.alias(name, alias)](#Dic+alias)
* [.bindChild(dic, opts)](#Dic+bindChild)
* [.bind(dic, opts)](#Dic+bind)
* [.createInstance(def, opts)](#Dic+createInstance) ⇒ <code>\*</code>
* [.createInstanceAsync(def, opts)](#Dic+createInstanceAsync) ⇒ <code>\*</code>

Expand All @@ -250,7 +250,7 @@ For more usage examples see: [instance](#Dic+instance), [class](#Dic+class), [fa
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | |
| options.containerSeparator | <code>String</code> | Container / service name separator. Default `_`. See [bindChild](#Dic+bindChild) |
| options.containerSeparator | <code>String</code> | Container / service name separator. Default `_`. See [bind](#Dic+bind) |
| options.debug | <code>boolean</code> | Debug on/off |

**Example**
Expand Down Expand Up @@ -478,9 +478,9 @@ dic.alias('one', 'oneAgain');

dic.get('one') === dic.get('oneAgain')
```
<a name="Dic+bindChild"></a>
<a name="Dic+bind"></a>

### dic.bindChild(dic, opts)
### dic.bind(dic, opts)
Bind other Dic instance with this one.

**Kind**: instance method of <code>[Dic](#Dic)</code>
Expand Down Expand Up @@ -529,7 +529,7 @@ class MyService() {
const dic = new Dic();
dic.class('myService', MyService);

dic.bindChild(packageDic, {
dic.bind(packageDic, {
name: 'myPackage'
})

Expand Down
2 changes: 1 addition & 1 deletion es5/dic-config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var DicConfigLoader = function () {
});

_.each(config.bindings, function (binding, containerName) {
var child = dic.getChild(containerName);
var child = dic.getBoundContainer(containerName);

_this.loadConfig(child, binding);

Expand Down
40 changes: 24 additions & 16 deletions es5/dic.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var Parser = require('./parser');
* Dependency injection container
*
* For more usage examples see: {@link Dic#instance}, {@link Dic#class}, {@link Dic#factory},
* {@link Dic#asyncFactory}, {@link Dic#bindChild}.
* {@link Dic#asyncFactory}, {@link Dic#bind}.
*
* @example // Dependency injection example
* class MyService {
Expand All @@ -60,7 +60,7 @@ var Dic = function () {

/**
* @param {Object} options
* @param {String} options.containerSeparator Container / service name separator. Default `_`. See {@link Dic#bindChild}
* @param {String} options.containerSeparator Container / service name separator. Default `_`. See {@link Dic#bind}
* @param {boolean} options.debug Debug on/off
*/
function Dic(options) {
Expand Down Expand Up @@ -266,7 +266,7 @@ var Dic = function () {
if (ret.def) {
this.throwError('Service "' + name + '" already registered');
}

def.container = this;
ret.container.instances[ret.name] = this.validateDef(def);
}

Expand Down Expand Up @@ -659,21 +659,23 @@ var Dic = function () {
* const dic = new Dic();
* dic.class('myService', MyService);
*
* dic.bindChild(packageDic, {
* dic.bind(packageDic, {
* name: 'myPackage'
* })
*
* // get a child service instance directly
* const logger = dic.get('myPackage_logger');
*
* @alias Dic#bind
*
* @param {Dic} dic
* @param {Object} opts
* @param {String} opts.name Container services prefix name
*/

}, {
key: 'bindChild',
value: function bindChild(dic) {
key: 'bindContainer',
value: function bindContainer(dic) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

opts = Joi.attempt(opts, {
Expand All @@ -686,8 +688,15 @@ var Dic = function () {
this.children[opts.name] = dic;
}
}, {
key: 'getChild',
value: function getChild(name) {
key: 'bind',
value: function bind(dic) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

this.bindContainer(dic, opts);
}
}, {
key: 'getBoundContainer',
value: function getBoundContainer(name) {
if (!this.children[name]) {
this.throwError(name + ' child container does not exist');
}
Expand Down Expand Up @@ -830,7 +839,7 @@ var Dic = function () {
}, _callee3, this);
}));

function createInstanceAsync(_x9, _x10) {
function createInstanceAsync(_x10, _x11) {
return _ref3.apply(this, arguments);
}

Expand All @@ -856,7 +865,8 @@ var Dic = function () {
params: Joi.array(),
paramsAlias: Joi.object().default({}),
asyncFactory: Joi.func(),
inject: Joi.object().default({})
inject: Joi.object().default({}),
container: Joi.object().type(Dic).optional().default(this)
}));

if (!def.type) {
Expand Down Expand Up @@ -905,31 +915,29 @@ var Dic = function () {
}, {
key: 'getServices',
value: function getServices(def) {
var _this = this;

var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var container = def.container;

var params = this._getDefParams(def);
return params.map(function (param) {
if (def.inject[param]) {
return def.inject[param];
}
return _this.get(param, opts);
return container.get(param, opts);
});
}
}, {
key: 'getServicesAsync',
value: function getServicesAsync(def) {
var _this2 = this;

var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var container = def.container;

var params = this._getDefParams(def);
var servicesPromises = params.map(function (param) {
if (def.inject[param]) {
return def.inject[param];
}
return _this2.getAsync(param, opts);
return container.getAsync(param, opts);
});

return _promise2.default.all(servicesPromises);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "bb-dic",
"version": "0.8.0",
"version": "0.9.0",
"description": "A dependency injection container",
"main": "src/index.js",
"scripts": {
"test": "mocha --harmony 'src/**/*.spec.js'",
"test-watch": "mocha --harmony --watch 'src/**/*.spec.js'",
"build": "npm run es5 && npm run docs",
"es5": "babel src -d es5",
"es5": "babel src -d es5 --ignore '**/*.spec.js'",
"docs": "jsdoc2md --configure jsdoc.json --template README.hbs.md \"src/**/*.js\" > README.md"
},
"author": "Matus Zeman <matus.zeman@gmail.com>",
Expand Down
2 changes: 1 addition & 1 deletion src/dic-config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DicConfigLoader {
});

_.each(config.bindings, (binding, containerName) => {
const child = dic.getChild(containerName);
const child = dic.getBoundContainer(containerName);

this.loadConfig(child, binding);

Expand Down
79 changes: 79 additions & 0 deletions src/dic-config-loader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const expect = require('chai').expect;
const {Dic, DicConfigLoader} = require('./index');

class Service1 {
constructor(service1Opts) {
this.options = service1Opts;
}
}

describe('DicConfigLoader', () => {
before(function() {
this.expect = {
instanceof: async (dic, ins, classDef) => {
expect(dic.get(ins)).instanceof(classDef);
expect(await dic.getAsync(ins)).instanceof(classDef);
}
}
});

beforeEach(function() {
this.dic = new Dic();
this.dicConfigLoader = new DicConfigLoader();
});

describe('.loadConfig()', () => {
describe('opts: aliases, options', () => {
it('works', async function() {
const {dic, dicConfigLoader} = this;

dic.class('service1', Service1);

dicConfigLoader.loadConfig(dic, {
options: {
service1: {}
},
aliases: {
service2: 'service1'
}
});

await this.expect.instanceof(dic, 'service1', Service1);
await this.expect.instanceof(dic, 'service2', Service1);
});
});

describe('opts: binding, imports', () => {
it('works', async function() {
const {dic, dicConfigLoader} = this;

const package1Dic = new Dic();
dic.bind(package1Dic, {
name: 'package1'
});

dic.class('service1', Service1);

dicConfigLoader.loadConfig(dic, {
options: {
service1: {}
},
aliases: {
service2: 'service1'
},
bindings: {
package1: {
imports: {
service1: 'service2'
}
}
}
});

await this.expect.instanceof(dic, 'service1', Service1);
await this.expect.instanceof(dic, 'service2', Service1);
await this.expect.instanceof(package1Dic, 'service1', Service1);
});
});
});
});
27 changes: 18 additions & 9 deletions src/dic.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Parser = require('./parser');
* Dependency injection container
*
* For more usage examples see: {@link Dic#instance}, {@link Dic#class}, {@link Dic#factory},
* {@link Dic#asyncFactory}, {@link Dic#bindChild}.
* {@link Dic#asyncFactory}, {@link Dic#bind}.
*
* @example // Dependency injection example
* class MyService {
Expand All @@ -29,7 +29,7 @@ class Dic {

/**
* @param {Object} options
* @param {String} options.containerSeparator Container / service name separator. Default `_`. See {@link Dic#bindChild}
* @param {String} options.containerSeparator Container / service name separator. Default `_`. See {@link Dic#bind}
* @param {boolean} options.debug Debug on/off
*/
constructor(options) {
Expand Down Expand Up @@ -207,7 +207,7 @@ class Dic {
if (ret.def) {
this.throwError(`Service "${name}" already registered`);
}

def.container = this;
ret.container.instances[ret.name] = this.validateDef(def);
}

Expand Down Expand Up @@ -487,18 +487,20 @@ class Dic {
* const dic = new Dic();
* dic.class('myService', MyService);
*
* dic.bindChild(packageDic, {
* dic.bind(packageDic, {
* name: 'myPackage'
* })
*
* // get a child service instance directly
* const logger = dic.get('myPackage_logger');
*
* @alias Dic#bind
*
* @param {Dic} dic
* @param {Object} opts
* @param {String} opts.name Container services prefix name
*/
bindChild(dic, opts = {}) {
bindContainer(dic, opts = {}) {
opts = Joi.attempt(opts, {
name: Joi.string().required()
});
Expand All @@ -509,7 +511,11 @@ class Dic {
this.children[opts.name] = dic;
}

getChild(name) {
bind(dic, opts = {}) {
this.bindContainer(dic, opts);
}

getBoundContainer(name) {
if (!this.children[name]) {
this.throwError(`${name} child container does not exist`);
}
Expand Down Expand Up @@ -610,7 +616,8 @@ class Dic {
params: Joi.array(),
paramsAlias: Joi.object().default({}),
asyncFactory: Joi.func(),
inject: Joi.object().default({})
inject: Joi.object().default({}),
container: Joi.object().type(Dic).optional().default(this)
}));

if (!def.type) {
Expand Down Expand Up @@ -655,22 +662,24 @@ class Dic {
}

getServices(def, opts = {}) {
const {container} = def;
const params = this._getDefParams(def);
return params.map((param) => {
if (def.inject[param]) {
return def.inject[param];
}
return this.get(param, opts);
return container.get(param, opts);
});
}

getServicesAsync(def, opts = {}) {
const {container} = def;
const params = this._getDefParams(def);
const servicesPromises = params.map((param) => {
if (def.inject[param]) {
return def.inject[param];
}
return this.getAsync(param, opts);
return container.getAsync(param, opts);
});

return Promise.all(servicesPromises);
Expand Down

0 comments on commit 6b8967e

Please sign in to comment.