Skip to content

Commit

Permalink
Merge pull request #3 from codyjdalton/0.3.0
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
codyjdalton committed May 24, 2018
2 parents e926fbb + 3c6ca59 commit 65b1c05
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 661 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ deploy:
secure: R7NYRyRB1oIw6G+Dpzw5NgdaIQirt1KE0Wh9cH14cvIOwj37LUcuXZqQADWnrItABqg8xVRichCnFPB6UQp/pVaBO04Bz2cKH9MBYx+C0oAI/7Yk7i02yJcJnkamjdI8kBEhV98ewxJiFqEEr1f3iJQ2duiPZESMP5xUgS42B7oHS/9nB7m3+/u9GELyF2EyHK7K8169EkDCXDtyq7d77eQdZAjdSqkJ8GK4BreHS2O1j+ykdOyvz0mLLQoaak/HUrXovmjLqXEObzAoqnwoT32fFrLLiyLfZvRsN0K3RoQY6JMAZtUkuiDiT7ReeG4KDLljNFZdAbZfekEoI5z+sq6KObh80Erb3baFh/dTpUCbOzmqlyh060QO8wrD48kc9cHeKIhmLZtb3L6xknFpBzxflpO6jIsXLmqVOyHHHWy85t+oLC2oQDY9FbdbQvTdbvskwkNjbWBpArCprrudFmWBy9jOjZZvb6IjcLBIeufKjO6B+A4tEzQJj6HU6sCGOoVEbJk2sxZtHrD0aQtJn/ixMuBiWwDG0jtlyA9I6E9Bt2sPlI8naY/niFZAv9tOhMJaApiOozWTsgamAYn9jpAFbCaS27PR1E6ZtfrJG1UB5+gqcIntoanXYVyRGQZbnsYTGcTOxL8hCemua9q5RxtmcAfKCc12quLxEiC/+is=
on:
branch: master
tags: true
repo: codyjdalton/super-injector
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/codyjdalton/super-injector.svg?branch=master)](https://travis-ci.org/codyjdalton/ts-module-seed) [![Coverage Status](https://coveralls.io/repos/github/codyjdalton/super-injector/badge.svg?branch=master)](https://coveralls.io/github/codyjdalton/ts-module-seed?branch=master)
[![Build Status](https://travis-ci.org/codyjdalton/super-injector.svg?branch=master)](https://travis-ci.org/codyjdalton/super-injector) [![Coverage Status](https://coveralls.io/repos/github/codyjdalton/super-injector/badge.svg?branch=master)](https://coveralls.io/github/codyjdalton/super-injector?branch=master)

# Super Injector

Expand Down
20 changes: 20 additions & 0 deletions lib/decorators/metadata.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* metadata.decorator
*/

import { Type } from '../common/models/type.model';
import { GenericClassDecorator } from '../common/types/class-decorator.type';
import { Injector } from '../modules/injector.module';

/**
* Create generic method decorator
*/
export const MetadataDecorator = () => {
return (config: object = {}): any => {
return (target: Type<any>, propertyKey: string, descriptor: PropertyDescriptor): void => {
Injector.set(target, config, propertyKey);
};
};
};

export const Metadata = MetadataDecorator();
45 changes: 44 additions & 1 deletion lib/modules/injector.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import { expect } from 'chai';

import { Injectable } from '../decorators/injectable.decorator';
import { Injector } from './injector.module';

import { Injectable } from '../decorators/injectable.decorator';
import { Metadata } from '../decorators/metadata.decorator';

describe('Injector', () => {

it('should include class dependencies', () => {
Expand All @@ -31,4 +33,45 @@ describe('Injector', () => {

expect(aTestClass.testMessage).to.equal('test-message');
});

it('should allow setting properties on a class target', () => {

@Injectable()
class TestClass {}

// set a property at the test class level
Injector.set(TestClass, { someProp: 'test' });

const propVal: string = Injector.get(TestClass, 'someProp');
const someVal: string = Injector.get(TestClass, 'noProp', 'defaultValue');

expect(propVal).to.equal('test');
expect(someVal).to.equal('defaultValue');
});

it('should allow setting properties on a class method target', () => {

@Injectable()
class TestClass {

@Metadata({
someProp: 'another-test'
})
public someMethod() {
// ..
}

@Metadata()
public anotherMethod() {
// ..
}
}

const aTestClass: any = Injector.resolve<TestClass>(TestClass);
const propVal: string = Injector.get(aTestClass, 'someProp', null, 'someMethod');
const anotherVal: string = Injector.get(aTestClass, 'someProp', null, 'anotherMethod');

expect(propVal).to.equal('another-test');
expect(anotherVal).to.equal(null);
});
});
6 changes: 3 additions & 3 deletions lib/modules/injector.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export const Injector = new class {
* @param {object} config - an object containing metadata
* @param {string | symbol} propKey
*/
/*public set(target: Type<any>, config: object = {}, propKey: string | symbol): void {
public set(target: Type<any>, config: object, propKey: any = null): void {
Object.keys(config).forEach(
(key: string) => Reflect.defineMetadata(key, config[key], target, propKey),
(key: string) => Reflect.defineMetadata(key, config[key], target, propKey ? propKey : undefined),
);
}

Expand All @@ -41,7 +41,7 @@ export const Injector = new class {
* @param {string} key
* @param {any} defaultValue
*/
/*public get(target: Type<any>, key: string, defaultValue: any = null, propKey: any = null): any {
public get(target: Type<any>, key: string, defaultValue: any = null, propKey: any = null): any {
return Reflect.hasMetadata(key, target, propKey ? propKey : undefined) ?
Reflect.getMetadata(key, target, propKey ? propKey : undefined) :
defaultValue;
Expand Down
Loading

0 comments on commit 65b1c05

Please sign in to comment.