lib/application.js
function createControllerFunction(func) {

  var Handlebars = protos.require('handlebars');
  var context, newFunc, compile, source,
      funcSrc = func.toString();
  var code = funcSrc
      .trim()
      .replace(/^function\s+(.*?)(\s+)?\{(\s+)?/, '')
      .replace(/(\s+)?\}$/, '');

  // Get source file path
  var alias = protos.lib.controller.prototype.getAlias(func.name),
      srcFile = this.mvcpath + 'controllers/' + alias + '.js';

  try {
    source = fs.readFileSync(srcFile, 'utf-8');
  } catch(e){
    source = fs.readFileSync(srcFile.replace(/\.js$/, '_controller.js'), 'utf-8');
  }

  // Detect pre & post function code
  var si = source.indexOf(funcSrc),
      preFuncSrc = source.slice(0, si).trim(),
      postFuncSrc = source.slice(si + funcSrc.length).trim();

  // Controller code

  var template = Handlebars.compile('\n\
with (locals) {\n\n\
function {{{name}}}(app) {\n\
  this.filters = this.filters["{{{name}}}"] || [];\n\
}\n\n\
require("util").inherits({{{name}}}, protos.lib.controller);\n\n\
protos.extend({{{name}}}, protos.lib.controller);\n\n\
{{{name}}}.filter = {{{name}}}.prototype.filter;\n\
{{{name}}}.handler = {{{name}}}.prototype.handler;\n\n\
var __funKeys__ = Object.keys({{{name}}});\n\
\n\
(function() { \n\
{{{preFuncSrc}}}\n\n\
with(this) {\n\n\
  {{{code}}}\n\
}\n\n\
{{{postFuncSrc}}}\n\n\
}).call({{{name}}});\n\n\
for (var key in {{{name}}}) {\n\
  if (__funKeys__.indexOf(key) === -1) { \n\
    {{{name}}}.prototype[key] = {{{name}}}[key];\n\
    delete {{{name}}}[key];\n\
  }\n\
}\n\
\n\
if (!{{{name}}}.prototype.authRequired) {\n\
  {{{name}}}.prototype.authRequired = protos.lib.controller.prototype.authRequired;\n\
} else {\n\
  {{{name}}}.prototype.authRequired = true; \n\
}\n\n\
return {{{name}}};\n\n\
}');

  var fnCode = template({
    name: func.name,
    code: code,
    preFuncSrc: preFuncSrc,
    postFuncSrc: postFuncSrc,
  });

  // console.exit(fnCode);

  /*jshint evil:true */
  compile = new Function('locals', fnCode);

  newFunc = compile({
    app: this,
    protos: protos,
    module: {},
    require: require,
    console: console,
    __dirname: this.mvcpath + 'controllers',
    __filename: srcFile,
    process: process
  });

  return newFunc;

}
function createControllerFunction(func) {

  var Handlebars = protos.require('handlebars');
  var context, newFunc, compile, source,
      funcSrc = func.toString();
  var code = funcSrc
      .trim()
      .replace(/^function\s+(.*?)(\s+)?\{(\s+)?/, '')
      .replace(/(\s+)?\}$/, '');

  // Get source file path
  var alias = protos.lib.controller.prototype.getAlias(func.name),
      srcFile = this.mvcpath + 'controllers/' + alias + '.js';

  try {
    source = fs.readFileSync(srcFile, 'utf-8');
  } catch(e){
    source = fs.readFileSync(srcFile.replace(/\.js$/, '_controller.js'), 'utf-8');
  }

  // Detect pre & post function code
  var si = source.indexOf(funcSrc),
      preFuncSrc = source.slice(0, si).trim(),
      postFuncSrc = source.slice(si + funcSrc.length).trim();

  // Controller code

  var template = Handlebars.compile('\n\
with (locals) {\n\n\
function {{{name}}}(app) {\n\
  this.filters = this.filters["{{{name}}}"] || [];\n\
}\n\n\
require("util").inherits({{{name}}}, protos.lib.controller);\n\n\
protos.extend({{{name}}}, protos.lib.controller);\n\n\
{{{name}}}.filter = {{{name}}}.prototype.filter;\n\
{{{name}}}.handler = {{{name}}}.prototype.handler;\n\n\
var __funKeys__ = Object.keys({{{name}}});\n\
\n\
(function() { \n\
{{{preFuncSrc}}}\n\n\
with(this) {\n\n\
  {{{code}}}\n\
}\n\n\
{{{postFuncSrc}}}\n\n\
}).call({{{name}}});\n\n\
for (var key in {{{name}}}) {\n\
  if (__funKeys__.indexOf(key) === -1) { \n\
    {{{name}}}.prototype[key] = {{{name}}}[key];\n\
    delete {{{name}}}[key];\n\
  }\n\
}\n\
\n\
if (!{{{name}}}.prototype.authRequired) {\n\
  {{{name}}}.prototype.authRequired = protos.lib.controller.prototype.authRequired;\n\
} else {\n\
  {{{name}}}.prototype.authRequired = true; \n\
}\n\n\
return {{{name}}};\n\n\
}');

  var fnCode = template({
    name: func.name,
    code: code,
    preFuncSrc: preFuncSrc,
    postFuncSrc: postFuncSrc,
  });

  // console.exit(fnCode);

  /*jshint evil:true */
  compile = new Function('locals', fnCode);

  newFunc = compile({
    app: this,
    protos: protos,
    module: {},
    require: require,
    console: console,
    __dirname: this.mvcpath + 'controllers',
    __filename: srcFile,
    process: process
  });

  return newFunc;

}
function createControllerFunction ( func ) { var Handlebars = protos . require ( 'handlebars' ) ; var context , newFunc , compile , source , funcSrc = func . toString ( ) ; var code = funcSrc . trim ( ) . replace ( / ^function\s+(.*?)(\s+)?\{(\s+)? / , '' ) . replace ( / (\s+)?\}$ / , '' ) ; // Get source file path var alias = protos . lib . controller . prototype . getAlias ( func . name ) , srcFile = this . mvcpath + 'controllers/' + alias + '.js' ; try { source = fs . readFileSync ( srcFile , 'utf-8' ) ; } catch ( e ) { source = fs . readFileSync ( srcFile . replace ( / \.js$ / , '_controller.js' ) , 'utf-8' ) ; } // Detect pre & post function code var si = source . indexOf ( funcSrc ) , preFuncSrc = source . slice ( 0 , si ) . trim ( ) , postFuncSrc = source . slice ( si + funcSrc . length ) . trim ( ) ; // Controller code var template = Handlebars . compile ( '\n\
with (locals) {\n\n\
function {{{name}}}(app) {\n\
  this.filters = this.filters["{{{name}}}"] || [];\n\
}\n\n\
require("util").inherits({{{name}}}, protos.lib.controller);\n\n\
protos.extend({{{name}}}, protos.lib.controller);\n\n\
{{{name}}}.filter = {{{name}}}.prototype.filter;\n\
{{{name}}}.handler = {{{name}}}.prototype.handler;\n\n\
var __funKeys__ = Object.keys({{{name}}});\n\
\n\
(function() { \n\
{{{preFuncSrc}}}\n\n\
with(this) {\n\n\
  {{{code}}}\n\
}\n\n\
{{{postFuncSrc}}}\n\n\
}).call({{{name}}});\n\n\
for (var key in {{{name}}}) {\n\
  if (__funKeys__.indexOf(key) === -1) { \n\
    {{{name}}}.prototype[key] = {{{name}}}[key];\n\
    delete {{{name}}}[key];\n\
  }\n\
}\n\
\n\
if (!{{{name}}}.prototype.authRequired) {\n\
  {{{name}}}.prototype.authRequired = protos.lib.controller.prototype.authRequired;\n\
} else {\n\
  {{{name}}}.prototype.authRequired = true; \n\
}\n\n\
return {{{name}}};\n\n\
}' ) ; var fnCode = template ( { name : func . name , code : code , preFuncSrc : preFuncSrc , postFuncSrc : postFuncSrc , } ) ; // console.exit(fnCode); /*jshint evil:true */ compile = new Function ( 'locals' , fnCode ) ; newFunc = compile ( { app : this , protos : protos , module : { } , require : require , console : console , __dirname : this . mvcpath + 'controllers' , __filename : srcFile , process : process } ) ; return newFunc ; }
Converts a regular function into a controller constructor
Converts a regular function into a controller constructor