Skip to content
Browse files

(api) route.constraints is now an array

  • Loading branch information...
1 parent a4a8212 commit 5348fb0bee294090906218789a7e6a874f3c5155 @cloudhead committed Jan 31, 2011
Showing with 23 additions and 32 deletions.
  1. +21 −30 lib/journey.js
  2. +2 −2 test/journey-test.js
View
51 lib/journey.js
@@ -96,18 +96,16 @@ journey.Router.methods = ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'];
journey.map = function (context) {
return {
paths: [],
- required: {},
+ required: [],
filter: function (/* variable arguments */) {
var args = Array.prototype.slice.call(arguments),
map = (typeof(args[args.length - 1]) === 'function') && args.pop(),
- filter = args.pop() || context.options.filter,
- prev = this.required['filter'];
+ filter = args.pop() || context.options.filter;
- this.required['filter'] = filter;
+ this.required.push(filter);
map.call(this, this);
-
- this.required['filter'] = prev;
+ this.required.pop();
},
get: function (pattern, opts) { return this.route('GET', pattern, opts) },
@@ -122,17 +120,14 @@ journey.map = function (context) {
pattern = this.paths.length ? '' : /.*/,
ignoreCase = false,
method = journey.Router.methods,
- constraints = {},
+ constraints = [],
extension = context.options.extension ? '(?:' + context.options.extension + ')?' : '';
- // Mixin the shared required constraints
- mixin(constraints, this.required);
-
+ Array.prototype.push.apply(constraints, this.required);
+
args.forEach(function (arg) {
if (journey.Router.methods.indexOf(arg) !== -1 || Array.isArray(arg)) {
method = arg;
- } else if (typeof(arg) === "object") {
- mixin(constraints, arg);
} else if (typeof(arg) === "string" || arg.exec) {
pattern = arg;
} else {
@@ -169,12 +164,12 @@ journey.map = function (context) {
route.handler = handler;
return route;
},
- ensure: function (name, handler) {
- route.constraints[name] = handler;
+ ensure: function (handler) {
+ route.constraints.push(handler);
return this;
},
filter: function (handler) {
- return this.ensure('filter', handler || context.options.filter);
+ return this.ensure(handler || context.options.filter);
}
};
},
@@ -232,7 +227,7 @@ journey.Router.prototype = {
return promise;
},
- constraints: {},
+ constraints: [],
resolve: function (request, body, dispatcher) {
var that = this, allowedMethods = [];
@@ -260,19 +255,15 @@ journey.Router.prototype = {
// asynchronously making sure everything passes.
//
(function checkConstraints(constraints) {
- var key = constraints.shift();
-
- if (key) {
- if (typeof route.constraints[key] === 'function') {
- // If the constraint is a function then expect it to have a method signature:
- // asyncConstraint(request, body, callback);
- route.constraints[key](request, body, function (err) {
- if (err) return cb(err);
- checkConstraints(constraints);
- });
- } else {
- cb(new(Error)("constraint '" + key + "' not found."));
- }
+ var constraint = constraints.shift();
+
+ if (constraint) {
+ // If the constraint is a function then expect it to have a method signature:
+ // asyncConstraint(request, body, callback);
+ constraint(request, body, function (err) {
+ if (err) return cb(err);
+ checkConstraints(constraints);
+ });
} else {
// If there is no handler for this route, return a new NotImplemented exception
if (! ('handler' in route)) { return cb(new(NotImplemented)("unbound route")) }
@@ -296,7 +287,7 @@ journey.Router.prototype = {
return cb(null, false);
}
}
- })(Object.keys(route.constraints));
+ })(route.constraints.slice(0));
};
//
View
4 test/journey-test.js
@@ -72,7 +72,7 @@ var router = new(journey.Router)(function (map) {
map.route('DELETE', /^(\w+)\/([0-9]+)$/).
bind(function (res, r, k) { return resources[r].destroy(res, k) });
- map.put('home/assert', { assert: function (res, body) { return body.length === 9; } }).
+ map.put('home/assert').filter(function (res, req, body) { return body.length === 9; }).
bind(function (res) { res.send(200, {"Content-Type":"text/html"}, "OK"); });
//
@@ -165,7 +165,7 @@ vows.describe('Journey').addBatch({
pattern: /^noparams$/,
method: 'GET', handler: function (res, params) {
promise.emit('success', params);
- }, success: undefined, constraints: {}
+ }, success: undefined, constraints: []
});
router.route(mock.mockRequest('GET', '/noparams', {}));
return promise;

0 comments on commit 5348fb0

Please sign in to comment.
Something went wrong with that request. Please try again.