Skip to content

Commit

Permalink
Merge branch 'release/v0.1.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Sep 17, 2015
2 parents 65a65c1 + dced05f commit c3f45ab
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 32 deletions.
38 changes: 22 additions & 16 deletions es5/lib/forbin.js
Expand Up @@ -213,33 +213,39 @@ var Controller = (function () {
var self = this;

this[actionName] = function (request, response) {
var originalEnd = response.end;
var originalEnd = undefined;
if (response && response.end) {
originalEnd = response.end;
}

_flowsync2["default"].series([function beforeFilters(next) {
self[processBeforeFilters](actionName, request, response, next);
self[processBeforeFilters].call(self, actionName, request, response, next);
}, function action(next) {
var _this3 = this;

response.end = function () {
for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
args[_key6] = arguments[_key6];
}
if (originalEnd) {
response.end = function () {
for (var _len6 = arguments.length, args = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
args[_key6] = arguments[_key6];
}

originalEnd.apply.apply(originalEnd, [self].concat(args));
next();
};
}

originalEnd.apply.apply(originalEnd, [_this3].concat(args));
next();
};
originalAction(request, response);
originalAction.call(self, request, response);
}, function afterFilters(next) {
self[processAfterFilters](actionName, request, response, next);
self[processAfterFilters].call(self, actionName, request, response, next);
}]);
};
}
}, {
key: processFilters,
value: function (filters, request, response, callback) {
var self = this;
_flowsync2["default"].eachSeries(filters, function processFilter(filterDetails, next) {
//call filter if not skipped
if (filterDetails.skip !== true) {
filterDetails.filter(request, response, next);
filterDetails.filter.call(self, request, response, next);
} else {
next();
}
Expand All @@ -250,10 +256,10 @@ var Controller = (function () {
}, {
key: getFilters,
value: function (action, filterObject) {
var _this4 = this;
var _this3 = this;

return filterObject.filter(function (filter) {
return filter.action === _this4[action];
return filter.action === _this3[action];
});
}
}, {
Expand Down
87 changes: 84 additions & 3 deletions es5/spec/forbin.spec.js
Expand Up @@ -41,13 +41,13 @@ describe("Controller(...options)", function () {

_createClass(ClientController, [{
key: "create",
value: function create(request, response) {}
value: function create() {}
}, {
key: "update",
value: function update(request, response) {}
value: function update() {}
}, {
key: "delete",
value: function _delete(request, response) {}
value: function _delete() {}
}]);

return ClientController;
Expand Down Expand Up @@ -708,5 +708,86 @@ describe("Controller(...options)", function () {
});
});
});

describe("(binding)", function () {
var clean = Symbol("clean"),
throwIt = Symbol("throwIt"),
actionSpy = undefined,
actionObject = undefined,
beforeFilterSpy = undefined,
afterFilterSpy = undefined,
beforeFilterObject = undefined,
afterFilterObject = undefined,
appleController = undefined;

var AppleController = (function (_Controller4) {
function AppleController() {
_classCallCheck(this, AppleController);

if (_Controller4 != null) {
_Controller4.apply(this, arguments);
}
}

_inherits(AppleController, _Controller4);

_createClass(AppleController, [{
key: "filters",
value: function filters() {
this.before(this[clean]);
this.after(this[throwIt]);
}
}, {
key: clean,
value: function (request, response, next) {
beforeFilterSpy(this);
next();
}
}, {
key: throwIt,
value: function (request, response, next) {
afterFilterSpy(this);
next();
}
}, {
key: "eat",
value: function eat(request, response) {
actionSpy(this);
response.end();
}
}]);

return AppleController;
})(_index2["default"]);

before(function (done) {
actionSpy = _sinon2["default"].spy(function (object) {
actionObject = object;
});

beforeFilterSpy = _sinon2["default"].spy(function (object) {
beforeFilterObject = object;
});

afterFilterSpy = _sinon2["default"].spy(function (object) {
afterFilterObject = object;
});

appleController = new AppleController();
appleController.eat({}, { end: done });
});

it("should allow to access this on the action", function () {
actionObject.should.eql(appleController);
});

it("should allow to access this on the before filter", function () {
beforeFilterObject.should.eql(appleController);
});

it("should allow to access this on the after filter", function () {
afterFilterObject.should.eql(appleController);
});
});
});
});
26 changes: 17 additions & 9 deletions es6/lib/forbin.js
Expand Up @@ -159,25 +159,32 @@ export default class Controller {
const self = this;

this[actionName] = (request, response) => {
const originalEnd = response.end;
let originalEnd;
if(response && response.end) {
originalEnd = response.end;
}

flowsync.series([
function beforeFilters(next) {
self[processBeforeFilters](
self[processBeforeFilters].call(self,
actionName,
request,
response,
next
);
},
function action(next) {
response.end = (...args) => {
originalEnd.apply(this, ...args);
next();
};
originalAction(request, response);
if(originalEnd) {
response.end = (...args) => {
originalEnd.apply(self, ...args);
next();
};
}

originalAction.call(self, request, response);
},
function afterFilters(next) {
self[processAfterFilters](
self[processAfterFilters].call(self,
actionName,
request,
response,
Expand All @@ -190,12 +197,13 @@ export default class Controller {
}

[processFilters](filters, request, response, callback) {
const self = this;
flowsync.eachSeries(
filters,
function processFilter(filterDetails, next) {
//call filter if not skipped
if(filterDetails.skip !== true) {
filterDetails.filter(request, response, next);
filterDetails.filter.call(self, request, response, next);
} else {
next();
}
Expand Down
69 changes: 66 additions & 3 deletions es6/spec/forbin.spec.js
Expand Up @@ -7,9 +7,9 @@ describe("Controller(...options)", () => {
let clientController;

class ClientController extends Controller {
create(request, response) {}
update(request, response) {}
delete(request, response) {}
create() {}
update() {}
delete() {}
}

beforeEach(() => {
Expand Down Expand Up @@ -623,5 +623,68 @@ describe("Controller(...options)", () => {
});
});
});

describe("(binding)", () => {
let clean = Symbol("clean"),
throwIt = Symbol("throwIt"),
actionSpy,
actionObject,
beforeFilterSpy,
afterFilterSpy,
beforeFilterObject,
afterFilterObject,
appleController;

class AppleController extends Controller {
filters() {
this.before(this[clean]);
this.after(this[throwIt]);
}

[clean](request, response, next) {
beforeFilterSpy(this);
next();
}

[throwIt](request, response, next) {
afterFilterSpy(this);
next();
}

eat(request, response) {
actionSpy(this);
response.end();
}
}

before((done) => {
actionSpy = sinon.spy((object) => {
actionObject = object;
});

beforeFilterSpy = sinon.spy((object) => {
beforeFilterObject = object;
});

afterFilterSpy = sinon.spy((object) => {
afterFilterObject = object;
});

appleController = new AppleController();
appleController.eat({}, {end: done});
});

it("should allow to access this on the action", () => {
actionObject.should.eql(appleController);
});

it("should allow to access this on the before filter", () => {
beforeFilterObject.should.eql(appleController);
});

it("should allow to access this on the after filter", () => {
afterFilterObject.should.eql(appleController);
});
});
});
});
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "forbin",
"version": "0.1.5",
"version": "0.1.6",
"description": "ES6 Component for controllers with filter support.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit c3f45ab

Please sign in to comment.