Skip to content
Browse files

Handler's handlerSetup() renamed to containerSetup, Handler's isSetup…

…Only introduced, toHandler() introduced, refactored
  • Loading branch information...
1 parent 67daf76 commit faf0aded712adda7413c66227caf538eb0cc1de6 @dimsmol committed Apr 5, 2013
View
42 lib/handlers/chain.js
@@ -1,7 +1,6 @@
"use strict";
var inherits = require('util').inherits;
var Handler = require('./core/handler');
-var Custom = require('./custom');
var Chain = function () {
@@ -22,35 +21,32 @@ Chain.prototype.handleRequest = function (ctx) {
};
Chain.prototype.add = function (handler) {
- handler = Chain.resolveHandler(handler, this);
+ handler = this.toHandler(handler);
if (handler != null) {
- this.handlers.push(handler);
+ if (handler.containerSetup != null) {
+ handler.containerSetup(this);
+ }
+ if (!handler.isSetupOnly) {
+ this.handlers.push(handler);
+ }
}
};
-Chain.chain = function () {
- return new Chain();
+Chain.prototype.addHandlers = function (handlers) {
+ for (var i = 0; i < handlers.length; i++) {
+ this.add(handlers[i]);
+ }
};
-Chain.resolveHandler = function (handler, opt_container) {
- var result = null;
- if (handler != null) {
- if (Array.isArray(handler)) {
- result = new Chain();
- for (var i = 0; i < handler.length; i++) {
- result.add(handler[i]);
- }
- }
- else if (handler.constructor === Function) {
- result = new Custom(handler);
- }
- else {
- result = handler;
- }
+Chain.prototype.toHandler = function (handler) {
+ var toHandler = require('./core/to_handler');
+ return toHandler(handler);
+};
- if (result.handlerSetup != null) {
- result = result.handlerSetup(opt_container);
- }
+Chain.chain = function (opt_handlers) {
+ var result = new Chain();
+ if (opt_handlers != null) {
+ result.addHandlers(opt_handlers);
}
return result;
};
View
5 lib/handlers/core/handler.js
@@ -3,6 +3,7 @@ var Handler = function () {
};
Handler.prototype.name = 'Handler';
+Handler.prototype.isSetupOnly = false;
Handler.prototype.toString = function () {
return ['[', this.name, ']'].join('');
@@ -25,9 +26,7 @@ Handler.prototype.handleError = function (ctx) {
ctx.next();
};
-Handler.prototype.handlerSetup = function (handlerContainer) {
- // returning null will exclude handler
- return this;
+Handler.prototype.containerSetup = function (container) {
};
View
4 lib/handlers/core/index.js
@@ -1,9 +1,11 @@
"use strict";
var Handler = require('./handler');
var ValidatingHandler = require('./validating_handler');
+var toHandler = require('./to_handler');
module.exports = {
Handler: Handler,
- ValidatingHandler: ValidatingHandler
+ ValidatingHandler: ValidatingHandler,
+ toHandler: toHandler
};
View
27 lib/handlers/core/to_handler.js
@@ -0,0 +1,27 @@
+"use strict";
+var custom = require('../custom').custom;
+var chain = require('../chain').chain;
+
+
+var toHandler = function (handler) {
+ var result = null;
+ if (handler != null) {
+ if (Array.isArray(handler)) {
+ result = chain(handler);
+ }
+ else if (handler.constructor === Function) {
+ result = custom(handler);
+ }
+ else {
+ result = handler;
+ }
+
+ if (result.createHandler != null) {
+ result = result.createHandler();
+ }
+ }
+ return result;
+};
+
+
+module.exports = toHandler;
View
33 lib/handlers/custom.js
@@ -3,23 +3,20 @@ var inherits = require('util').inherits;
var Handler = require('./core/handler');
-var Custom = function (opt_handleRequestFunc, opt_handleErrorFunc, opt_setupFunc) {
+var Custom = function (opt_handleRequestFunc, opt_handleErrorFunc, opt_containerSetupFunc) {
Handler.call(this);
this.handleRequestFunc = opt_handleRequestFunc;
this.handleErrorFunc = opt_handleErrorFunc;
- this.setupFunc = opt_setupFunc;
+ this.containerSetupFunc = opt_containerSetupFunc;
};
inherits(Custom, Handler);
Custom.prototype.name = 'Custom';
-Custom.prototype.handlerSetup = function (handlerContainer) {
- if (this.setupFunc) {
- return this.setupFunc(handlerContainer);
- }
- else {
- return this;
+Custom.prototype.containerSetup = function (container) {
+ if (this.containerSetupFunc) {
+ return this.containerSetupFunc(container);
}
};
@@ -41,24 +38,24 @@ Custom.prototype.handleError = function (ctx) {
}
};
-Custom.custom = function (opt_handleRequestFunc, opt_handleErrorFunc, opt_setupFunc) {
- return new Custom(opt_handleRequestFunc, opt_handleErrorFunc, opt_setupFunc);
+Custom.custom = function (opt_handleRequestFunc, opt_handleErrorFunc, opt_containerSetupFunc) {
+ return new Custom(opt_handleRequestFunc, opt_handleErrorFunc, opt_containerSetupFunc);
};
-Custom.custom.both = function (handleFunc, opt_setupFunc) {
- return new Custom(handleFunc, handleFunc, opt_setupFunc);
+Custom.custom.both = function (handleFunc, opt_containerSetupFunc) {
+ return new Custom(handleFunc, handleFunc, opt_containerSetupFunc);
};
-Custom.custom.req = function (handleRequestFunc, opt_setupFunc) {
- return new Custom(handleRequestFunc, null, opt_setupFunc);
+Custom.custom.req = function (handleRequestFunc, opt_containerSetupFunc) {
+ return new Custom(handleRequestFunc, null, opt_containerSetupFunc);
};
-Custom.custom.err = function (handleErrorFunc, opt_setupFunc) {
- return new Custom(null, handleErrorFunc, opt_setupFunc);
+Custom.custom.err = function (handleErrorFunc, opt_containerSetupFunc) {
+ return new Custom(null, handleErrorFunc, opt_containerSetupFunc);
};
-Custom.custom.setup = function (setupFunc) {
- return new Custom(null, null, setupFunc);
+Custom.custom.containerSetup = function (containerSetupFunc) {
+ return new Custom(null, null, containerSetupFunc);
};
View
8 lib/handlers/impl.js
@@ -12,10 +12,11 @@ var Impl = function (implFunc, isSync) {
inherits(Impl, Handler);
Impl.prototype.name = 'Impl';
+Impl.prototype.isSetupOnly = true;
-Impl.prototype.handlerSetup = function (handlerContainer) {
+Impl.prototype.containerSetup = function (container) {
var found = false;
- var info = handlerContainer.info;
+ var info = container.info;
if (info != null && info.core != null) {
var ret = info.core.ret;
if (ret != null) {
@@ -27,9 +28,6 @@ Impl.prototype.handlerSetup = function (handlerContainer) {
if (!found) {
throw new Error('Impl requires Ret');
}
-
- // exclude handler itself
- return null;
};
View
7 lib/handlers/ret.js
@@ -35,9 +35,9 @@ Ret.prototype.setImpl = function (impl, isSync) {
}
};
-Ret.prototype.handlerSetup = function (handlerContainer) {
- if (handlerContainer != null && handlerContainer.info != null) {
- var info = handlerContainer.info;
+Ret.prototype.containerSetup = function (container) {
+ if (container != null && container.info != null) {
+ var info = container.info;
if (info.core != null && info.core.ret != null) {
throw new Error('Multiple Ret within handler container');
}
@@ -46,7 +46,6 @@ Ret.prototype.handlerSetup = function (handlerContainer) {
info.core.ret = this;
}
}
- return this;
};
Ret.prototype.handleRequest = function (ctx) {
View
9 lib/mechanics/batch/batch.js
@@ -7,7 +7,7 @@ var handlers = require('../../handlers');
var Mechanics = require('./mechanics');
var Applier = require('./applier');
-var Chain = handlers.Chain;
+var chain = handlers.chain;
var data = handlers.data;
var impl = handlers.impl;
var ret = handlers.ret;
@@ -58,12 +58,7 @@ Batch.prototype.getHandlers = function () {
};
Batch.prototype.createHandlers = function () {
- var handlers = this.getHandlers();
- var chain = new Chain(this);
- for (var i = 0; i < handlers.length; i++) {
- chain.add(handlers[i]);
- }
- return chain;
+ return chain(this.getHandlers());
};
Batch.prototype.handle = function (ctx) {
View
11 lib/resources/resource.js
@@ -1,7 +1,8 @@
"use strict";
var valid = require('valid');
var MethodNotAllowed = require('../errors').MethodNotAllowed;
-var Chain = require('../handlers/chain');
+var toHandler = require('../handlers/core/to_handler');
+var chain = require('../handlers/chain').chain;
var impls = require('../handlers/impl').impls;
var ret = require('../handlers/ret').ret;
var subpaths = require('../tools/path').subpaths;
@@ -18,7 +19,7 @@ var Resource = function (path, handlers) {
this.handlers = {};
for (var method in handlers) {
- var handler = this.prepareHandler(handlers[method]);
+ var handler = this.toHandler(handlers[method]);
if (handler != null) {
this.handlers[method] = handler;
}
@@ -33,7 +34,7 @@ var Resource = function (path, handlers) {
Resource.prototype.createDefaultOptionsHandler = function () {
var self = this;
- return this.prepareHandler([
+ return chain([
ret(valid.validators.any),
impls(function (ctx) {
@@ -52,8 +53,8 @@ Resource.prototype.createMethodMapper = function () {
return new MethodMapper(this.handlers);
};
-Resource.prototype.prepareHandler = function (handler) {
- return Chain.resolveHandler(handler);
+Resource.prototype.toHandler = function (handler) {
+ return toHandler(handler);
};
Resource.prototype.resolve = function (ctx) {

0 comments on commit faf0ade

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