Comparing version 0.1.0 to 0.1.1
@@ -6,3 +6,3 @@ { | ||
"repository": "git://github.com/edinella/beat.git", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"license": "MIT", | ||
@@ -19,3 +19,9 @@ "main": "lib/beat.js", | ||
"test": "mocha" | ||
} | ||
} | ||
}, | ||
"keywords": [ | ||
"di", | ||
"dependency", | ||
"injection", | ||
"injector" | ||
] | ||
} |
101
README.md
@@ -1,3 +0,2 @@ | ||
beat | ||
==== | ||
# beat | ||
Simple dependency injection for node | ||
@@ -11,6 +10,6 @@ | ||
app.value('port', process.env.PORT || 3000); | ||
app.value('port', process.env.PORT || 3000); | ||
app.value('express', require('express')); | ||
app.factory('app', function(express){ | ||
app.factory('app', function(express){ | ||
return express(); | ||
@@ -26,2 +25,96 @@ }); | ||
}); | ||
``` | ||
## How to use | ||
Install it with NPM: | ||
```sh | ||
npm install --save beat | ||
``` | ||
Then require it: | ||
```js | ||
var Beat = require('beat'); | ||
``` | ||
## API | ||
**constructor(alias)**: starts a new Beat with an alias (defaults to "unnamed") | ||
To produce the instance, `Beat` should be called with `new` operator. | ||
The `alias` argument identifies it, useful for debugging in case of errors. | ||
```js | ||
var myServer = new Beat('server'); | ||
``` | ||
**value(token, value)**: defines a value for injection | ||
Register the final value. | ||
```js | ||
myServer.value('port', 80); | ||
``` | ||
**factory(token, factoryFn)**: defines a factory for injection | ||
To produce the instance, `factoryFn` will be called once (with instance context) and its result will be used. | ||
The `factoryFn` function can use injection annotation. | ||
```js | ||
myServer.factory('port', function(){ | ||
var port = 80; | ||
// some logic here | ||
return port; | ||
}); | ||
``` | ||
**run(fn)**: runs a code block, with injection | ||
`fn` will be called (with instance context). | ||
The `fn` function can use injection annotation. | ||
```js | ||
myServer.factory('port', function(){ | ||
var port = 80; | ||
// some logic here | ||
return port; | ||
}); | ||
``` | ||
**get(token)**: obtains a property | ||
```js | ||
myServer.value('port', 80); | ||
var port = myServer.get('port'); | ||
``` | ||
### Annotation | ||
Methods `run` and `factory` can recieve annotated functions as arguments, that will be used for injection. | ||
The injector looks up tokens based on argument names: | ||
```js | ||
myServer.run(function(server, port) { | ||
// will inject objects bound to 'server' and 'port' tokens | ||
}); | ||
``` | ||
You can also use comments: | ||
```js | ||
myServer.run(function(/* httpServer */ server, /* serverPort */ port) { | ||
// will inject objects bound to 'http' and 'serverPort' tokens | ||
}); | ||
``` | ||
You can also use a array: | ||
```js | ||
myServer.run(['http', 'serverPort', function(server, port) { | ||
// will inject objects bound to 'http' and 'serverPort' tokens | ||
}]); | ||
``` |
@@ -11,2 +11,4 @@ var Beat = require('../../'); | ||
}); | ||
// basics | ||
it('.run method should recieve and execute a function', function(){ | ||
@@ -19,5 +21,2 @@ var spy = sinon.spy(); | ||
}); | ||
it('.run method should be chainable', function(){ | ||
expect(beat.run(function(){})).to.be.equal(beat); | ||
}); | ||
it('.value method should set a value to be obtained by .get', function(){ | ||
@@ -28,5 +27,2 @@ var mv = {}; | ||
}); | ||
it('.value method should be chainable', function(){ | ||
expect(beat.value('myValue')).to.be.equal(beat); | ||
}); | ||
it('.factory method should set a factory function of a value', function(){ | ||
@@ -40,5 +36,2 @@ var mv = {}; | ||
}); | ||
it('.factory method should be chainable', function(){ | ||
expect(beat.factory('myValue', function(){})).to.be.equal(beat); | ||
}); | ||
it('factories should be called once', function(){ | ||
@@ -78,3 +71,64 @@ var stub = sinon.stub().returns({}); | ||
}); | ||
// chainability | ||
it('.run method should be chainable', function(){ | ||
expect(beat.run(function(){})).to.be.equal(beat); | ||
}); | ||
it('.value method should be chainable', function(){ | ||
expect(beat.value('myValue')).to.be.equal(beat); | ||
}); | ||
it('.factory method should be chainable', function(){ | ||
expect(beat.factory('myValue', function(){})).to.be.equal(beat); | ||
}); | ||
// array declaration | ||
it('.run method should be able to deal with an array for declaring dependencies', function(done){ | ||
var x = {}; | ||
var y = {}; | ||
beat.value('myValue', x); | ||
beat.factory('myFabricatedValue', sinon.stub().returns(y)); | ||
beat.run(['myFabricatedValue', 'myValue', function(a, b){ | ||
expect(a).to.be.equal(y); | ||
expect(b).to.be.equal(x); | ||
done(); | ||
}]); | ||
}); | ||
it('.factory method should be able to deal with an array for declaring dependencies', function(done){ | ||
var x = {}; | ||
var y = {}; | ||
beat.value('myValue', x); | ||
beat.factory('myFabricatedValue', sinon.stub().returns(y)); | ||
beat.factory('z', ['myFabricatedValue', 'myValue', function(a, b){ | ||
expect(a).to.be.equal(y); | ||
expect(b).to.be.equal(x); | ||
done(); | ||
}]); | ||
beat.get('z'); | ||
}); | ||
// comment aliases | ||
it('.run method should be able to deal with comments for declaring dependencies', function(done){ | ||
var x = {}; | ||
var y = {}; | ||
beat.value('myValue', x); | ||
beat.factory('myFabricatedValue', sinon.stub().returns(y)); | ||
beat.run(function(/* myFabricatedValue */ a, /* myValue */ b){ | ||
expect(a).to.be.equal(y); | ||
expect(b).to.be.equal(x); | ||
done(); | ||
}); | ||
}); | ||
it('.factory method should be able to deal with comments for declaring dependencies', function(done){ | ||
var x = {}; | ||
var y = {}; | ||
beat.value('myValue', x); | ||
beat.factory('myFabricatedValue', sinon.stub().returns(y)); | ||
beat.factory('z', function(/* myFabricatedValue */ a, /* myValue */ b){ | ||
expect(a).to.be.equal(y); | ||
expect(b).to.be.equal(x); | ||
done(); | ||
}); | ||
beat.get('z'); | ||
}); | ||
}); | ||
}); |
10725
188
118