Skip to content

Commit

Permalink
Merge pull request #4 from codyjdalton/0.3.1
Browse files Browse the repository at this point in the history
0.3.1: Update documentation and export metadata
  • Loading branch information
codyjdalton committed May 24, 2018
2 parents 3868913 + 1c78afe commit 4b69ac0
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 662 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
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![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)
[![npm version](https://badge.fury.io/js/super-injector.svg)](https://badge.fury.io/js/super-injector) [![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) ![npm](https://img.shields.io/npm/l/super-injector.svg)


# Super Injector

Expand All @@ -16,6 +17,10 @@ npm i super-injector --save

Add the Injectable decorator and resolve a class with dependencies:

### Get an instance of a class with dependencies

Super Injector will new up your class and all of its dependencies

```typescript
import { Injectable, Injector } from 'super-injector';

Expand All @@ -42,6 +47,48 @@ console.log(anotherClassInstance.message);
// logs "Hello World" to the console
```

### Set metadata on classes

Set metadata on classes before instantiation:

```typescript
import { Injectable, Injector } from 'super-injector';

@Injectable()
class TestClass {}

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

console.log(Injector.get(TestClass, 'someProp'));

// logs "Hello World" to the console
```

### Set metadata on class methods

You can set metadata at design time and retrieve it at run time:

```typescript
import { Metadata, Injector } from 'super-injector';

class SomeClass {

@Metadata({
someProp: 'Hello World'
})
someMethod(): void {
// ..
}
}

const someClassInstance: AnotherClass = Injector.resolve(someClassInstance);

console.log(Injector.get(someClassInstance, 'someProp', null, 'someMethod'));

// logs "Hello World" to the console
```

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/codyjdalton/super-injector/tags).
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();
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// export your main module:
export { Injectable } from './decorators/injectable.decorator';
export { Injector } from './modules/injector.module';
export { Metadata } from './decorators/metadata.decorator';
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 4b69ac0

Please sign in to comment.