Skip to content

Commit

Permalink
Bind a meta value provider to a context
Browse files Browse the repository at this point in the history
* Create a MetaValueProvider interface

* Add Binding.toProvider() to bind a MetaValueProvider

* return provider.value() asynchronously thru Binding.getValue()
  • Loading branch information
deepakrkris committed May 31, 2017
1 parent 30e871f commit d00fb74
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/context/src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {Context} from './context';
import {Constructor, instantiateClass} from './resolver';
import {MetaValueProvider} from './provider';

// tslint:disable-next-line:no-any
export type BoundValue = any;
Expand Down Expand Up @@ -94,6 +95,23 @@ export class Binding {
return this;
}

/**
* Bind the key to a MetaValueProvider
* @param providerClass {Function} The MetaValueProvider class (constructor function)
* @example
* ```ts
* ctx.bind('now').toProvider(MyProvider);
*
* ```
*/
public toProvider<T>(providerClass: Constructor<MetaValueProvider>): this {
this.getValue = (ctx): Object | Function => {
const providerInstance: MetaValueProvider = instantiateClass<MetaValueProvider>(providerClass, ctx) as MetaValueProvider;
return providerInstance.value();
};
return this;
}

/**
* Bind the key to an instance of the given class.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/context/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export {Binding, BoundValue} from './binding';
export {Context} from './context';
export {Constructor} from './resolver';
export {inject} from './inject';
export {MetaValueProvider} from './provider';

export const isPromise = require('is-promise');
30 changes: 30 additions & 0 deletions packages/context/src/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT


/**
* Interface definition for a MetaValueProvider
*
* MetaValueProvider provisions injecting values via the constructor and
* returns a computed value (Object or Function) when the value() function is called
*
* @example:
* ```ts
* export class MyProvider implements MetaValueProvider {
* constructor(@inject('value') param){}
*
* static key = 'MY_KEY';
*
* value() {
* return computed_value;
* }
* }
*
* ```
*/
export interface MetaValueProvider {
key: string;
value(): Object | Function;
}
36 changes: 36 additions & 0 deletions packages/context/test/unit/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {MetaValueProvider} from '../../src';

describe('MetaValueProvider', () => {
let provider: MetaValueProvider;
beforeEach(givenProvider);

describe('key', () => {
it('identifies the provider', () => {
expect(provider.key).to.equal('MY_KEY');
});
});

describe('value()', () => {
it('returns the value synchronously', () => {
expect(provider.value()).to.equal('hello world');
});
});

function givenProvider() {
provider = new MyProvider('hello');
}
});

class MyProvider implements MetaValueProvider {
constructor(private _msg : string) {}
key = 'MY_KEY';
value() {
return this._msg + ' world';
}
}

0 comments on commit d00fb74

Please sign in to comment.