Skip to content

Commit

Permalink
Merge pull request #45 from flyvictor/expose-full-fortune-configurati…
Browse files Browse the repository at this point in the history
…on-to-hooks

Hooks are able to read full fortune configuration.
  • Loading branch information
mjtodd committed Jul 10, 2014
2 parents 83c7380 + b9308b1 commit 6a59a4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ exports.addHook = function(name, hooks, stage, type, inlineConfig){
resource.hooks[stage][type] = resource.hooks[stage][type] || [];
_.each(hooks, function(hook){
var hookOptions = getHookConfig(hook, resource, inlineConfig);
resource.hooks[stage][type].push(hook.init(hookOptions, _this.options));
resource.hooks[stage][type].push(hook.init(hookOptions, _this));
});
});
};
Expand Down
17 changes: 15 additions & 2 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var synchronousHook = [{
},
init: function(hookConfig, fortuneConfig){
var a = hookConfig.a;
var b = fortuneConfig.b || 100;
var b = (fortuneConfig.options && fortuneConfig.options.b) || 100;
return function(req, res){
this.hooked = a + b;
return this;
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('hooks', function(){
}
};
var resource = {};
hooks.initGlobalHooks(resourceConfig, {b: 2});
hooks.initGlobalHooks(resourceConfig, {options: {b: 2}});
resourceConfig.hooks._before.read[0].call(resource);
(resource.hooked).should.equal(3);
done();
Expand Down Expand Up @@ -83,6 +83,7 @@ describe('hooks', function(){
var fortune;
beforeEach(function(){
fortune = {
_resource: "person",
_resources: {
person: {
hooksOptions: {
Expand Down Expand Up @@ -129,5 +130,17 @@ describe('hooks', function(){
(pet.hooked).should.equal(12);
done();
});
it('hooks should be provided with full fortune instance', function(done){
var mock = [{
name: "mock",
init: function(config, fortune){
should.exist(fortune);
(fortune._resource).should.equal('person');
(fortune._resources).should.be.an.Object;
done();
}
}];
hooks.addHook.call(fortune, 'person', mock, '_after', 'write');
});
});
});

0 comments on commit 6a59a4f

Please sign in to comment.