Simple dependency injector for your precious node apps.
Installation
npm install simpleplan --save
Usage:
you write yourself a delicious module
// my-module.js; var { // Do some dirty things with Mongoose};
And then on the main app you go like
// index.jsvar simpleplan = ;simpleplan;simpleplan; var myModule = ; // Mongoose will magically be injected into the function. wow. I know.
That's it.
WAT
By calling the base require of simpleplan, it extends javascript's Function
and provides 2 more methods: inject()
and use(params)
.
register(key, value)
simply stores your dependencies in a key-value hash for the default dependencies. It was made for not passing a dependencies hash for every function you want to inject to.
simpleplan; // It will be injected to any 'a' parameter // unless it is being overriden in the function call.
use(params)
Tells simpleplan you want to inject some dependencies to this function, with the right order.
Parameter names are not strict.
var { console;}; ; // "a: First, b: Second, c: Gal Schlezinger"
inject()
Same as use(params)
only here you don't need to give in an array of dependency names - it takes them from the method signature.
var { console;}; ; // "a: Gal Schlezinger, b: First, c: Second"
Function
's prototype!
Don't touch my Well, you can also go like
var simpleplan = ;var inject = simpleplaninject register = simpleplanregister; ; var func = ; ; // => "Who's the man? Gal!"; // "Who's the man? Hugh Jackman!"
but as you can see, its not as awesome as extending Function
.
CoffeeScript Usage
require 'simpleplan' register "first""Hello"register "second""World" myFunc = inject " !" myFunc # => "Hello World!" myFunc first: "wat" #=> "wat World!"
Changelog
0.1.0
- Added registry for stop passing the dependencies object to every method.
0.0.2
- Added CoffeeScript support (
inject ->
..)
Contributing
- Fork this repo.
- Install dependencies. (
npm install
) - Make sure it runs all the tests. (
mocha
) - Branch.
- Code.
- Pull Request.
- $$$.