Permalink
Browse files

Added validations

  • Loading branch information...
1 parent 447d172 commit 73f64304cfd1edbd1129433b6ef4e03dd4706fdf @fk1blow committed May 28, 2011
Showing with 39 additions and 26 deletions.
  1. +39 −26 lib/Controller/ActionController.js
@@ -1,34 +1,37 @@
var Config = require('../Config'),
-File = require('../File/File'),
-Standardizr = require('./Standardizr'),
-ActionRouter = require('./ActionRouter');
-BaseController = require('./BaseController');
+ File = require('../File/File'),
+ Standardizr = require('./Standardizr'),
+ ActionRouter = require('./ActionRouter'),
+ BaseController = require('./BaseController'),
+ Validator = require('./Validator');
var ActionDispatcher = Klass({
statics: {
dispatch: function(res) {
- ActionController.init(res);
- res.end('default');
+ Controller.init();
+ if(Config.env.default_response)
+ res.end(Config.env.default_response);
}
}
});
-var ActionController = Klass({
+var Controller = Klass({
statics: {
controller: {
default_name: null,
object_name: null,
instance: null,
- action: null
+ action: null,
},
- stack: {},
-
- init: function(res) {
+ init: function() {
this._setDefaults();
+ // Validate
+ this._validateNames();
+
// Here be dragons
this._loadAppController();
@@ -39,6 +42,22 @@ var ActionController = Klass({
this._callAction();
},
+ _validateNames: function() {
+ var cn_valid = Validator.validate('c_n', this.controller.default_name);
+ var co_valid = Validator.validate('c_o', this.controller.object_name);
+ var an_valid = Validator.validate('a_n', this.controller.action);
+
+ if(!cn_valid) {
+ throw new Error('Invalid controller route name:: ' + this.controller.default_name);
+ }
+ if(!co_valid) {
+ throw new Error('Invalid controller object name:: ' + this.controller.object_name);
+ }
+ if(!an_valid) {
+ throw new Error('Invalid action name:: ' + this.controller.action);
+ }
+ },
+
_setDefaults: function() {
this.controller.default_name = ActionRouter.getControllerName();
this.controller.object_name = Standardizr.standardizeName(this.controller.default_name);
@@ -49,16 +68,6 @@ var ActionController = Klass({
_buildInstance: function() {
var c = this._loadControllerFile(this.controller.object_name);
- //if(!Seek.ActionModule.Validators.hasValidName(this.controller.object_name)) {
- // throw new Error('Unable to find #' + this.controller.object_name);
- //}
- //if(!Seek.ActionModule.Validators.hasValidMeta(this.controller.object_name)) {
- // throw new Error('Invalid controller meta #' + this.controller.object_name);
- //}
- //if(!Seek.ActionModule.Validators.isAJooseObject(this.controller.object_name)) {
- // throw new Error('Not a valid object #' + this.controller.object_name);
- //}
-
// Constructing instance
this.controller.instance = new c();
},
@@ -67,18 +76,22 @@ var ActionController = Klass({
_callAction: function() {
var action = Standardizr.getControllerAction();
- //if(!Seek.ActionModule.Validators.hasMethod(this.controller.object_name, action)) {
- // throw new ReferenceError('Unable to find action #' + action + ' in #' + this.controller.object_name);
- //}
+ if(typeof(this.controller.instance[action]) != 'function') {
+ throw new Error('Unable to find action:: ' + action);
+ }
// Calling the controller's instance method (action)
this.controller.instance[action]();
},
// Loads the ApplicationController
_loadAppController: function() {
- if(typeof(ApplicationController) != 'function')
- this._loadControllerFile('ApplicationController');
+ if(typeof(ApplicationController) != 'function') {
+ var f = this._loadControllerFile('ApplicationController');
+ }
+ if(typeof(f) != 'function') {
+ throw new Error('Unable to find ApplicationController');
+ }
},
// Loads a controller givend the object name EX: 'UsersController'

0 comments on commit 73f6430

Please sign in to comment.