Skip to content
Browse files

Code style improvements

  • Loading branch information...
1 parent cd99389 commit 3a77ab086b44a25242e8c83e5b96784df308bf3d @bnoguchi committed Feb 28, 2012
Showing with 93 additions and 105 deletions.
  1. +3 −4 index.js
  2. +85 −96 lib/modules/everymodule.js
  3. +1 −1 lib/promise.js
  4. +4 −4 lib/stepSequence.js
View
7 index.js
@@ -29,15 +29,14 @@ everyauth.middleware = function () {
}
, function fetchUserFromSession (req, res, next) {
var sess = req.session
- , auth = sess && sess.auth
- , everymodule, findUser;
+ , auth = sess && sess.auth;
if (!auth) return next();
if (!auth.userId) return next();
- everymodule = everyauth.everymodule;
+ var everymodule = everyauth.everymodule;
if (!everymodule._findUserById) return next();
var pause = __pause(req);
everymodule._findUserById(auth.userId, function (err, user) {
- if (err) throw err; // TODO Leverage everyauth's error handling
+ if (err) return next(err);
if (user) req.user = user;
else delete sess.auth;
next();
View
181 lib/modules/everymodule.js
@@ -9,13 +9,14 @@ var routeDescPrefix = {
get: 'ROUTE (GET)'
, post: 'ROUTE (POST)'
};
-routeDescPrefix.GET = routeDescPrefix.get;
-routeDescPrefix.POST = routeDescPrefix.post;
+// Returns a function used for declaring new route triggered sequences
+// associated with the uri route and the http `method`
+// @param {String} method is the http method (e.g, 'get', 'post')
function route (method) {
- return function (alias, description) {
+ return function (alias, description) { /* `this` is `everyModule` */
if (description)
- description = routeDescPrefix[method] + ' - ' + description;
+ description = routeDescPrefix[method.toLowerCase()] + ' - ' + description;
this.configurable(alias, description);
var name = method + ':' + alias;
this._currSeq =
@@ -26,17 +27,15 @@ function route (method) {
var everyModule = module.exports = {
name: 'everymodule'
- , definit: function (fn) {
- // Remove any prior `init` that was assigned
- // directly to the object via definit and not
- // assigned via prototypal inheritance
- if (this.hasOwnProperty('init'))
- delete this.init;
+ , definit: function definit (fn) {
+ // Remove any prior `init` that was assigned directly to the object via
+ // definit and not assigned via prototypal inheritance
+ if (this.hasOwnProperty('init')) delete this.init;
var _super = this.init;
// since this.hasOwnProperty('init') is false
- this.init = function () {
+ this.init = function init () {
this._super = _super;
fn.apply(this, arguments);
delete this._super;
@@ -47,62 +46,74 @@ var everyModule = module.exports = {
}
, get: route('get')
, post: route('post')
- , stepseq: function (name, description) {
+ , stepseq: function stepseq (name, description) {
this.configurable(name, description);
this._currSeq =
this._stepSequences[name] || (this._stepSequences[name] = new StepSequence(name, this));
return this;
}
- , configurable: function (arg, description, wrapper) {
+ , configurable: function configurable (arg, description, wrapper) {
if (!arguments.length)
+ // Calling module.configurable() returns a listing of the module's
+ // configuration
return this._configurable;
+
var property;
if (arg.constructor === Object) {
for (property in arg) {
description = arg[property];
this.configurable(property, description);
}
- } else if (typeof arg === 'string') {
- property = arg;
- if (property.indexOf(' ') !== -1) {
- // e.g., property === 'apiHost appId appSecret'
- var self = this;
- property.split(/\s+/).forEach( function (_property) {
- self.configurable(_property);
- });
- return this;
+ return this;
+ }
+
+ // Otherwise, assume typeof arg === 'string'
+
+ // Calling module.configurable('apiHost appId appSecret')
+ // is a shortcut for:
+ // module.configurable('apiHost');
+ // module.configurable('appId');
+ // module.configurable('appSecret');
+ // e.g., property === 'apiHost appId appSecret'
+ property = arg;
+ if (property.indexOf(' ') !== -1) {
+ var props = property.split(/\s+/);
+ for (var i = props.length; i--;) {
+ this.configurable(props[i]);
}
+ return this;
+ }
- // Else we have a single property name
- // (Base Case)
- if (!this[property])
- this[property] = function (setTo) {
- var k = '_' + property;
- if (!arguments.length) {
- // TODO this.everyauth is not yet available here in some contexts
- // For example, when we set and try to access a scope in an auth module definition
- // but if you look in index, everyauth is not assigned to the module until after it is
- // required
- if (this.everyauth && this.everyauth.debug && 'undefined' === typeof this[k]) {
- var debugMsg = 'WARNING: You are trying to access the attribute/method configured by `' +
- property + '`, which you did not configure. Time to configure it.';
- console.log(debugMsg);
- console.trace();
- }
- return this[k];
+ // Else we have a single property name
+ // (Base Case)
+ if (!this[property]) {
+ this[property] = function (setTo) {
+ var k = '_' + property;
+ if (!arguments.length) {
+ // TODO this.everyauth is not yet available here in some contexts
+ // For example, when we set and try to access a scope in an auth module definition
+ // but if you look in index, everyauth is not assigned to the module until after it is
+ // required
+ if (this.everyauth && this.everyauth.debug && 'undefined' === typeof this[k]) {
+ var debugMsg = 'WARNING: You are trying to access the attribute/method configured by `' +
+ property + '`, which you did not configure. Time to configure it.';
+ console.log(debugMsg);
+ console.trace();
}
- this[k] = setTo;
- return this;
- };
- this._configurable[property] = description ||
- this.configurable[property] ||
- 'No Description';
-
- // Add configurable to submodules that inherit from this
- // supermodule
- for (var name in this.submodules) {
- this.submodules[name].configurable(arg, description);
- }
+ return this[k];
+ }
+ this[k] = setTo;
+ return this;
+ };
+ }
+ this._configurable[property] = description ||
+ this.configurable[property] ||
+ 'No Description';
+
+ // Add configurable to submodules that inherit from this
+ // supermodule
+ for (var name in this.submodules) {
+ this.submodules[name].configurable(arg, description);
}
return this;
}
@@ -132,40 +143,30 @@ var everyModule = module.exports = {
}
, accepts: function (input) {
- var step = this._currentStep;
- step.accepts = input
- ? input.split(/\s+/)
- : null;
+ this._currentStep.accepts = input && input.split(/\s+/) || null;
return this;
}
, promises: function (output) {
- var step = this._currentStep;
- step.promises = output
- ? output.split(/\s+/)
- : null;
+ this._currentStep.promises = output && output.split(/\s+/) || null;
return this;
}
, description: function (desc) {
var step = this._currentStep;
step.description = desc;
-
- if (desc)
- desc = 'STEP FN [' + step.name + '] - ' + desc;
+ if (desc) desc = 'STEP FN [' + step.name + '] - ' + desc;
this.configurable(step.name, desc);
return this;
}
, stepTimeout: function (millis) {
- var step = this._currentStep;
- step.timeout = millis;
+ this._currentStep.timeout = millis;
return this;
}
, stepErrback: function (fn) {
- var step = this._currentStep;
- step.errback = fn;
+ this._currentStep.errback = fn;
return this;
}
@@ -178,15 +179,15 @@ var everyModule = module.exports = {
, cloneOnSubmodule: ['cloneOnSubmodule', '_configurable']
-
+
, submodules: {}
/**
* Creates a new submodule using prototypal
* inheritance
*/
, submodule: function (name) {
- var submodule = Object.create(this)
+ var submodule = Object.create(this, { name: { value: name } })
, self = this;
// So that when we add configurables after
@@ -196,11 +197,9 @@ var everyModule = module.exports = {
this.submodules[name] = submodule;
submodule.submodules = {};
- this.cloneOnSubmodule.forEach(
- function (toClone) {
- submodule[toClone] = clone(self[toClone]);
- }
- );
+ this.cloneOnSubmodule.forEach( function (toClone) {
+ submodule[toClone] = clone(self[toClone]);
+ });
var seqs = this._stepSequences
, newSeqs = submodule._stepSequences = {};
@@ -214,28 +213,24 @@ var everyModule = module.exports = {
newSteps[stepName] = steps[stepName].clone(stepName, submodule);
}
- submodule.name = name;
return submodule;
}
, validateSteps: function () {
- for (var seqName in this._stepSequences) {
- this._stepSequences[seqName].checkSteps();
+ var seqs = this._stepSequences;
+ for (var seqName in seqs) {
+ seqs[seqName].validateSteps();
}
}
- /**
- * Decorates the app with the routes required of the
- * module
- */
+ // Decorates the app with the routes required of the module
, routeApp: function (app) {
if (this.init) this.init();
- var self = this
- , routes = this._routes
+ var routes = this._routes
, methods = ['get', 'post'];
for (var method in routes) {
for (var routeAlias in routes[method]) {
- var path = self[routeAlias]();
+ var path = this[routeAlias]();
if (!path)
throw new Error('You have not defined a path for the route alias ' + routeAlias + '.');
var seq = routes[method][routeAlias];
@@ -303,35 +298,29 @@ Object.defineProperty(everyModule, '_routes', { get: function () {
}});
Object.defineProperty(everyModule, 'route', {
- get: function () {
- return this._routes;
- }
+ get: function () { return this._routes; }
});
Object.defineProperty(everyModule, 'routes', {get: function () {
var arr = []
, _routes = this._routes
- , _descriptions = this._configurable
- , aliases
- , self = this;
+ , _descriptions = this._configurable;
for (var method in _routes) {
for (var alias in _routes[method]) {
method = method.toUpperCase();
arr.push(method + ' (' + alias + ') [' +
- self[alias]() + ']' +
- _descriptions[alias].replace(routeDescPrefix[method], ''));
+ this[alias]() + ']' +
+ _descriptions[alias].replace(routeDescPrefix[method.toLowerCase()], ''));
}
}
return arr;
}});
everyModule
.configurable({
- moduleTimeout: 'how long to wait per step ' +
- 'before timing out and invoking any ' +
- 'timeout callbacks'
- , moduleErrback: 'THE error callback that is invoked' +
- 'any time an error occurs in the module; defaults to `throw` wrapper'
+ moduleTimeout: 'how long to wait per step before timing out and invoking any timeout callbacks'
+ , moduleErrback: 'THE error callback that is invoked any time an error occurs in the module; ' +
+ 'defaults to `throw` wrapper'
, logoutRedirectPath: 'where to redirect the app upon logging out'
, findUserById: 'function for fetching a user by his/her id -- used to assign to `req.user` - function (userId, callback) where function callback (err, user)'
})
View
2 lib/promise.js
@@ -12,7 +12,7 @@ Promise.prototype = {
if (this.values) {
fn.apply(scope, this.values);
return this;
- }
+ }
this._callbacks.push([fn, scope]);
return this;
}
View
8 lib/stepSequence.js
@@ -73,9 +73,9 @@ var StepSequence = module.exports = function StepSequence (name, _module) {
StepSequence.prototype = {
constructor: StepSequence
, clone: function (submodule) {
- var ret = new (this.constructor)(this.name, submodule);
- ret.orderedStepNames = clone(this.orderedStepNames);
- return ret;
+ var sequence = new (this.constructor)(this.name, submodule);
+ sequence.orderedStepNames = clone(this.orderedStepNames);
+ return sequence;
}
, materialize: function () {
@@ -88,7 +88,7 @@ StepSequence.prototype = {
}
// TODO Replace logic here with newer introspection code
- , checkSteps: function () {
+ , validateSteps: function () {
var steps = this.steps
, step
, paramsToDate = []

0 comments on commit 3a77ab0

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