Simple dependency injector for your precious node apps.
npm install simpleplan --save
you write yourself a delicious module
// my-module.js
require('simpleplan')();
var iNeedMongooseHere = function(Mongoose) {
// Do some dirty things with Mongoose
}.inject();And then on the main app you go like
// index.js
var simpleplan = require('simpleplan')();
simpleplan.register("Mongoose", "store here any object you would like.");
simpleplan.register("app", "and it will magically injected to your favorite methods");
var myModule = require('myModule')(); // Mongoose will magically be injected into the function. wow. I know.That's it.
By calling the base require of simpleplan, it extends javascript's Function and provides 2 more methods: inject() and use(params).
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.register("a", { thisIs: "Its value" }); // It will be injected to any 'a' parameter
// unless it is being overriden in the function call.Tells simpleplan you want to inject some dependencies to this function, with the right order.
Parameter names are not strict.
var somefunc = function(a,b,c) {
console.log("a: " + a + ", b: " + b + ", c: " + c);
}.use("c", "b", "whoIsAwesome");
somefunc({ c: "First", b: "Second", whoIsAwesome: "Gal Schlezinger" }); // "a: First, b: Second, c: Gal Schlezinger"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 somefunc = function(a,b,c) {
console.log("a: " + a + ", b: " + b + ", c: " + c);
}.inject();
somefunc({ b: "First", c: "Second", a: "Gal Schlezinger" }); // "a: Gal Schlezinger, b: First, c: Second"Well, you can also go like
var simpleplan = require('simpleplan');
var inject = simpleplan.inject,
register = simpleplan.register;
register("param", "Hugh Jackman!");
var func = inject(function(param) {
return "Who's the man? " + param;
});
func("Gal!"); // => "Who's the man? Gal!"
func(); // "Who's the man? Hugh Jackman!"but as you can see, its not as awesome as extending Function.
{ register, inject } = require 'simpleplan'
register "first", "Hello"
register "second", "World"
myFunc = inject (first, second) ->
"#{first} #{second}!"
myFunc() # => "Hello World!"
myFunc first: "wat" #=> "wat World!"- Added registry for stop passing the dependencies object to every method.
- Added CoffeeScript support (
inject ->..)
- Fork this repo.
- Install dependencies. (
npm install) - Make sure it runs all the tests. (
mocha) - Branch.
- Code.
- Pull Request.
- $$$.