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

Commit

Permalink
Added possibility to pull nested properties
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-donat committed Feb 27, 2017
1 parent 7587f38 commit 0539bc7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ components:

```yml
components:
my.module.property: { property: './module::PropertyName' }
my.module.property: { property: './module::PropertyPath.PropertyName' }
my.node.property:
property: 'path::join'
```
Expand Down
12 changes: 8 additions & 4 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const Definition = require('./definition');

const _isArray = Array.isArray;

function getProperty(path, object) {
return _get(object, path);
}

function getReference(container, argDefinition) {
if (argDefinition.isSelf()) {
return container;
Expand Down Expand Up @@ -107,12 +111,12 @@ module.exports = class Builder extends EventEmitter {
let method;
let call = definition.calls[0];

if (call.method && !loadedModule[call.method]) {
if (call.method && !getProperty(call.method, loadedModule)) {
throw new Error(`Can't find factory method '${call.method}' in module '${definition.module}' for definition '${definition.id}'`);
}

if (call.method) {
method = loadedModule[call.method];
method = getProperty(call.method, loadedModule);
}

let getParams = () => [];
Expand Down Expand Up @@ -150,7 +154,7 @@ module.exports = class Builder extends EventEmitter {
let Klass = loadedModule;

if (definition.class) {
Klass = loadedModule[definition.class];
Klass = getProperty(definition.class, loadedModule);
}

if (!Klass) {
Expand Down Expand Up @@ -298,7 +302,7 @@ module.exports = class Builder extends EventEmitter {
throw error;
}

let property = loadedModule[definition.property];
let property = getProperty(definition.property, loadedModule);

if (property === undefined) {
throw new Error(`Can't locate property '${definition.property}' from module '${definition.module}' for definition '${definition.id}'`);
Expand Down
13 changes: 13 additions & 0 deletions tests/component/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ module.exports.TaggedClass = class {
module.exports.factoryFunction = function(...args) {
return {args, id: uuid.v1()};
}

module.exports.nested = {
nestedFactoryFunction(...args) {
return {args};
},
SomeClass: module.exports.SomeClass
}

module.exports.D = {
a: {
b: 1
}
}
10 changes: 9 additions & 1 deletion tests/component/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ describe('Canister.js', function() {

expect(container.get('my.parameter')).to.equal('parameterValue');
expect(container.get('my.module')).to.equal(fixtures);

expect(container.get('my.property')).to.equal(fixtures.A);

expect(container.get('my.deep.property')).to.equal(fixtures.D.a.b);

const singleton = container.get('my.singleton');

expect(singleton).to.be.an.instanceOf(fixtures.SomeClass);
Expand Down Expand Up @@ -78,6 +81,9 @@ describe('Canister.js', function() {
expect(container.get('my.factory.function').args).to.be.eql([1, 'parameterValue']);
expect(container.get('my.factory.function')).to.be.equal(container.get('my.factory.function'))

expect(container.get('my.double.nested.factory').args).to.be.eql([1, 'parameterValue']);
expect(container.get('my.double.nested.factory')).to.be.equal(container.get('my.double.nested.factory'));

expect(container.get('my.factory')).to.be.eql([2, 'prop.A.value']);

expect(container.get('my.transient.factory')).to.not.be.equal(
Expand All @@ -92,12 +98,14 @@ describe('Canister.js', function() {
prop: 'parameterValue', more: {param: 'parameterValue'}
}]);

expect(container.get('my.prop.reference').args).to.be.eql(['objectParamValue']);
expect(container.get('my.prop.reference').args).to.be.eql(['objectParamValue', 1]);

expect(container.get('override')).to.be.eql(1);
expect(container.get('nested_a_bit.override')).to.be.eql(2);

expect(container.get('injected.component')).to.be.equal(injectedComponent);
expect(container.get('injected.parameter')).to.be.equal(injectedParameter);

expect(container.get('my.nested.class')).to.be.an.instanceOf(fixtures.nested.SomeClass);
})
})
17 changes: 15 additions & 2 deletions tests/component/wiring.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
parameters:
my.parameter: parameterValue
my.object: {param: 'objectParamValue'}
my.object:
param: 'objectParamValue'
my.nesting:
a: { b: 1 }
override: override
nested_a_bit.override: override
components:
my.module: {module: ./fixture}
my.property: {property: ./fixture::A}
my.deep.property: {property: ./fixture::D.a.b}
my.property.class: {property: ./fixture::SomeClass }
my.singleton:
class: ./fixture::SomeClass
Expand Down Expand Up @@ -65,11 +69,20 @@ components:
with:
- {prop: '@my.parameter', more: { param: '@my.parameter' }}
my.nested.factory:
class: ./fixture::factoryFunction
factory: ./fixture::factoryFunction
with:
- {prop: '@my.parameter', more: { param: '@my.parameter' }}
my.double.nested.factory:
factory: ./fixture::nested.nestedFactoryFunction
with:
- 1
- '@my.parameter'
my.prop.reference:
class: ./fixture::SomeClass
with:
- '@my.object::param'
- '@my.nesting::a.b'

my.nested.class: {class: ./fixture::nested.SomeClass }


0 comments on commit 0539bc7

Please sign in to comment.