[READ-ONLY] Mirror of https://github.com/Schniz/simpleplan. Simple dependency injector for your precious node apps.
0

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #3 from Schniz/register

Added registry for stop using the damn func.inject()(deps)

Gal Schlezinger (Jul 4, 2014, 6:03 PM +0300) aefaf51d 812ca012

+116 -17
+56 -7
README.md
··· 24 24 25 25 ```js 26 26 // index.js 27 - require('simpleplan')(); 28 - var dependencies = { 29 - Mongoose: 'store here any object you would like', 30 - app: 'hell yeah' 31 - }; 27 + var simpleplan = require('simpleplan')(); 28 + simpleplan.register("Mongoose", "store here any object you would like."); 29 + simpleplan.register("app", "and it will magically injected to your favorite methods"); 32 30 33 - var myModule = require('myModule')(dependencies); // Mongoose will magically be injected into the function. wow. I know. 31 + var myModule = require('myModule')(); // Mongoose will magically be injected into the function. wow. I know. 34 32 ``` 35 33 36 34 That's it. 37 35 38 36 WAT 39 37 --- 40 - simpleplan extends javascript's `Function` and provides 2 more methods: `inject()` and `use(params)`. 38 + By calling the base require of simpleplan, it extends javascript's `Function` and provides 2 more methods: `inject()` and `use(params)`. 39 + 40 + ### `register(key, value)` 41 + simply stores your dependencies in a key-value hash for the default dependencies. 42 + It was made for not passing a dependencies hash for every function you want to inject to. 43 + 44 + ```js 45 + simpleplan.register("a", { thisIs: "Its value" }); // It will be injected to any 'a' parameter 46 + // unless it is being overriden in the function call. 47 + ``` 41 48 42 49 ### `use(params)` 43 50 Tells simpleplan you want to inject some dependencies to this function, with the right order. ··· 49 56 }.use("c", "b", "whoIsAwesome"); 50 57 51 58 somefunc({ c: "First", b: "Second", whoIsAwesome: "Gal Schlezinger" }); // "a: First, b: Second, c: Gal Schlezinger" 59 + 52 60 ``` 53 61 54 62 ### `inject()` ··· 61 69 62 70 somefunc({ b: "First", c: "Second", a: "Gal Schlezinger" }); // "a: Gal Schlezinger, b: First, c: Second" 63 71 ``` 72 + 73 + ## Don't touch my `Function`'s prototype! 74 + Well, you can also go like 75 + ```js 76 + var simpleplan = require('simpleplan'); 77 + var inject = simpleplan.inject, 78 + register = simpleplan.register; 79 + 80 + register("param", "Hugh Jackman!"); 81 + 82 + var func = inject(function(param) { 83 + return "Who's the man? " + param; 84 + }); 85 + 86 + func("Gal!"); // => "Who's the man? Gal!" 87 + func(); // "Who's the man? Hugh Jackman!" 88 + ``` 89 + but as you can see, its not as awesome as extending `Function`. 90 + 91 + CoffeeScript Usage 92 + ------------------ 93 + ```coffeescript 94 + { register, inject } = require 'simpleplan' 95 + 96 + register "first", "Hello" 97 + register "second", "World" 98 + 99 + myFunc = inject (first, second) -> 100 + "#{first} #{second}!" 101 + 102 + myFunc() # => "Hello World!" 103 + myFunc first: "wat" #=> "wat World!" 104 + ``` 105 + 106 + Changelog 107 + --------- 108 + ### `0.1.0` 109 + - Added registry for stop passing the dependencies object to every method. 110 + 111 + ### `0.0.2` 112 + - Added CoffeeScript support (`inject ->`..) 64 113 65 114 Contributing 66 115 ------------
+1 -1
package.json
··· 1 1 { 2 2 "name": "simpleplan", 3 - "version": "0.0.2", 3 + "version": "0.1.0", 4 4 "description": "Simple dependency injector for your precious node apps.", 5 5 "main": "./lib/simpleplan.js", 6 6 "scripts": {
+16
lib/simpleplan.js
··· 1 + _registeredDependencies = {}; 2 + 1 3 // Awesome snippet by Jack Allan @ http://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically-from-javascript 2 4 var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; 3 5 var getParamNames = function(func) { ··· 34 36 35 37 var injectedDependencies = deps.map(function(dep) { 36 38 if (!(dep in dependencies)) { 39 + if (dep in _registeredDependencies) { 40 + return _registeredDependencies[dep]; 41 + } 42 + 37 43 throw Error("Dependency " + dep + " wasn't injected. available dependencies: " + Object.keys(dependencies).join(", ")); 38 44 } 39 45 return dependencies[dep]; ··· 43 49 } 44 50 } 45 51 52 + var register = function(key, value) { 53 + _registeredDependencies[key] = value; 54 + }; 55 + 46 56 var inject = function() { 47 57 var paramNames = getParamNames(this); 48 58 return use.apply(this, paramNames); ··· 51 61 var simpleplan = function() { 52 62 Function.prototype.use = use; 53 63 Function.prototype.inject = inject; 64 + 65 + return { 66 + register: register 67 + }; 54 68 } 69 + 70 + simpleplan.register = register; 55 71 56 72 simpleplan.inject = function(method) { 57 73 return inject.apply(method);
+23 -8
test/coffeescriptSpec.coffee
··· 12 12 describe "after calling", -> 13 13 myfunc = null 14 14 myfuncWithoutArguments = null 15 - { use, inject } = require('../lib/simpleplan') 15 + { use, inject, register } = require('../lib/simpleplan') 16 16 17 17 before -> 18 - myfunc = (firstMessage, secondMessage) -> "#{firstMessage}, #{secondMessage}" 18 + myfunc = (coffeeFirstMessage, coffeeSecondMessage) -> "#{coffeeFirstMessage}, #{coffeeSecondMessage}" 19 19 myfuncWithoutArguments = -> "this is a function." 20 20 21 21 describe "#use", -> ··· 26 26 ho: 'bet' 27 27 expect(alefbet).to.equal "alef, bet" 28 28 29 - used = use myfunc, "secondMessage", "firstMessage" 29 + used = use myfunc, "coffeeSecondMessage", "coffeeFirstMessage" 30 30 betalef = used 31 - firstMessage: 'alef' 32 - secondMessage: 'bet' 31 + coffeeFirstMessage: 'alef' 32 + coffeeSecondMessage: 'bet' 33 33 expect(betalef).to.equal "bet, alef" 34 34 35 35 it "should work with a function without arguments", -> ··· 41 41 it "should work with normal arguments", -> 42 42 injected = inject myfunc 43 43 alefbet = injected 44 - firstMessage: 'alef' 45 - secondMessage: 'bet' 44 + coffeeFirstMessage: 'alef' 45 + coffeeSecondMessage: 'bet' 46 46 expect(alefbet).to.equal("alef, bet") 47 47 48 48 it "should raise an error if a parameter is left with no injection", -> 49 49 expect(-> 50 50 injected = inject myfunc 51 51 52 - injected firstMessage: 'alef' 52 + injected coffeeFirstMessage: 'alef' 53 53 ).to.throw Error 54 54 55 55 it "should work with external modules", -> ··· 60 60 it "should work with a function without arguments", -> 61 61 value = myfuncWithoutArguments.inject()({ someArgument: 'arggggg' }) 62 62 expect(value).to.equal('this is a function.') 63 + 64 + describe "register use", -> 65 + injectedFunc = null 66 + 67 + before -> 68 + register "coffeeFirstMessage", "howdy" 69 + register "coffeeSecondMessage", "world!" 70 + 71 + injectedFunc = inject myfunc 72 + 73 + it "should complete the missing dependencies from the registry", -> 74 + expect(injectedFunc()).to.equal "howdy, world!" 75 + expect(injectedFunc(coffeeFirstMessage: "wat")).to.equal "wat, world!" 76 + expect(injectedFunc(coffeeSecondMessage: "wat")).to.equal "howdy, wat" 77 + expect(injectedFunc(coffeeSecondMessage: "b", coffeeFirstMessage: "a")).to.equal "a, b"
+20 -1
test/simpleplanSpec.js
··· 24 24 describe("after calling", function() { 25 25 var myfunc; 26 26 var myfuncWithoutArguments; 27 + var simpleplanInstance; 27 28 28 29 before(function() { 29 - simpleplan(); 30 + simpleplanInstance = simpleplan(); 30 31 31 32 myfunc = function(firstMessage, secondMessage) { 32 33 return firstMessage + ", " + secondMessage; ··· 82 83 it("should work with a function without arguments", function() { 83 84 var value = myfuncWithoutArguments.inject()({ someArgument: 'arggggg' }); 84 85 expect(value).to.equal('this is a function.'); 86 + }); 87 + 88 + describe("register use:", function() { 89 + before(function() { 90 + simpleplanInstance.register('firstMessage', 'howdy'); 91 + simpleplanInstance.register('secondMessage', 'world!'); 92 + }); 93 + 94 + it("should pass the registered dependencies when caled without arguments", function() { 95 + var value = myfunc.inject()(); 96 + 97 + expect(value).to.equal("howdy, world!"); 98 + }); 99 + 100 + it("should complete the missing dependencies from the register", function() { 101 + expect(myfunc.inject()({ firstMessage: "wat" })).to.equal("wat, world!"); 102 + expect(myfunc.inject()({ secondMessage: "wat" })).to.equal("howdy, wat"); 103 + }) 85 104 }); 86 105 }); 87 106 });