Skip to content

Commit

Permalink
2nd initial commit :D
Browse files Browse the repository at this point in the history
  • Loading branch information
fk1blow committed Jun 25, 2011
1 parent 55716d8 commit 883cc91
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 131 deletions.
4 changes: 2 additions & 2 deletions lib/Controller/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ var Standardizr = {
* @todo remove res param - get the response by Response.Factory.get('response')
*/
var ControllerDispatcher = {
dispatch: function(res) {
ActiveRouter.init();
dispatch: function(req, res) {
ActiveRouter.init(req);
Controller.init();

if(Config.env.defaults.response) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Http/HttpDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var HttpDispatcher = {
// Builds the request object
Request.Build(req, function() {
if(!StaticRequest.hasAssets(req)) {
ControllerDispatcher.dispatch(res);
ControllerDispatcher.dispatch(req, res);
}
});
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Http/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ var Request = Klass({
return this.data;
},

// this will fail...doooh
getRequest: function(request_object) {
return request_object;
getRequest: function() {
return this.request;
},

getMethod: function() {
Expand Down
4 changes: 2 additions & 2 deletions lib/Router/ActiveRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ var ActiveRouter = Klass({
statics: {
_route: {},

init: function() {
init: function(req) {
// current request url
this.current_url = Request.Factory().getUrl();

// Reset the route object
this._route = {};

// ...and pals
this._route = Parser.parseRouteFor(this.current_url);
this._route = Parser.parseRouteFor(this.current_url, req);

if(this._route) {
this._route['params'] = this.getParams();
Expand Down
4 changes: 2 additions & 2 deletions lib/Router/Mapper/Mapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var ResourceRoute = require('./ResourceRoute'),
SimpleRoute = require('./SimpleRoute'),
var SimpleRoute = require('./SimpleRoute'),
ResourceRoute = require('./ResourceRoute'),
Stacker = require('../Stacker/Stacker');


Expand Down
72 changes: 26 additions & 46 deletions lib/Router/Mapper/ResourceRoute.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Route = require('./Route');
var Route = require('./Route').Route,
Utils = require('./Route').Utils;


/**
Expand All @@ -9,12 +10,6 @@ var ResourceRoute = Klass({
Extends: Route,

attributes: {
_name: null,
_path: null,
_controller: null,
_action: null,
_method: null,
_regex: null,
_special_segments: ['controller', 'action', 'method', 'only'],
_methods_actions: {
create: 'POST',
Expand Down Expand Up @@ -74,8 +69,8 @@ var ResourceRoute = Klass({
},

_generateRegex: function(options) {
var constrained = this.constraintize(this._path, this._getConstraints(options));
var standard = this.standardize(constrained);
var constrained = Utils.constraintize(this._path, this._getConstraints(options));
var standard = Utils.standardize(constrained);
var r = new RegExp('^' + standard + '$');
this._regex = r;
},
Expand All @@ -97,44 +92,29 @@ var ResourceRoute = Klass({
},

_generatePath: function(resource_path) {
var action = this._action, controller = this._controller;
if(this._lazy_actions.indexOf(this._action) >= 0) {
action += '/:id';
var action, controller = this._controller;

switch(this._action) {
case 'index':
case 'create':
action = '';
break;

case 'edit':
action = '/:id/' + this._action;
break;

case 'show':
case 'destroy':
case 'update':
action = '/:id';
break;

case 'add':
action = '/add';
break;
}
this._path = '/' + Array(controller, action).join('/');
},


/**
* @public
* Getters
*/
getName: function() {
return this._name;
},

getPath: function() {
return this._path;
},

getController: function() {
return this._controller;
},

getAction: function() {
return this._action;
},

getName: function() {
return this._name;
},

getMethod: function() {
return this._method;
},

getRegex: function() {
return this._regex;
this._path = '/' + controller + action;
}
}
});
Expand Down
96 changes: 65 additions & 31 deletions lib/Router/Mapper/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,89 @@ var Inflection = require('../../Inflection/Inflection');

var Route = Klass({
attributes: {
_crud_actions: [ 'index', 'show', 'add', 'create', 'edit', 'update', 'destroy' ],
_lazy_actions: [ 'show', 'edit', 'update', 'destroy' ],
_resources: [] // this array should be used to retrieve the new routes builded by a route class
_name: null,
_path: null,
_controller: null,
_action: null,
_method: null,
_regex: null,
_crud_actions: [ 'index', 'show', 'add', 'create', 'edit', 'update', 'destroy' ],
_lazy_actions: [ 'show', 'edit', 'update', 'destroy' ]
},

methods: {
getResources: function() {

getName: function() {
return this._name;
},

constraintize: function(path, constraints) {
var rc = constraints,
ref = null;

for(var i in rc) {
if(rc.hasOwnProperty(i))
ref = ((ref) ? ref : path).replace(':'+i, rc[i]);
}
// replace all the remaining segments
if(ref) {
ref = this.replaceWith((ref) ? ref : path, /:[\w]+/g, '[\\w\\_]+');
} else {
ref = this.replaceWith(path, /:[\w]+/g, '[\\w\\_]+');
}
return ref;
getPath: function() {
return this._path;
},

standardize: function(route_path) {
var res = route_path.replace(/\)/g, ')?').replace(/[\/]+/g, '\\/');
return res
getController: function() {
return this._controller;
},

splitString: function(string, needle) {
var arr = string.split(needle)
return arr.splice(1, arr.length);
getAction: function() {
return this._action;
},

replaceWith: function(target, needle, haystack) {
return target.replace(needle, haystack);
getName: function() {
return this._name;
},

removeQS: function(url) {
return url.split(/\?|;/)[0];
getMethod: function() {
return this._method;
},

getRegex: function() {
return this._regex;
}
}
});


var Utils = {
constraintize: function(path, constraints) {
var rc = constraints,
ref = null;

for(var i in rc) {
if(rc.hasOwnProperty(i))
ref = ((ref) ? ref : path).replace(':'+i, rc[i]);
}
// replace all the remaining segments
if(ref) {
ref = this.replaceWith((ref) ? ref : path, /:[\w]+/g, '[\\w\\_]+');
} else {
ref = this.replaceWith(path, /:[\w]+/g, '[\\w\\_]+');
}
return ref;
},

standardize: function(route_path) {
var res = route_path.replace(/\)/g, ')?').replace(/[\/]+/g, '\\/');
return res
},

splitString: function(string, needle) {
var arr = string.split(needle)
return arr.splice(1, arr.length);
},

replaceWith: function(target, needle, haystack) {
return target.replace(needle, haystack);
},

removeQS: function(url) {
return url.split(/\?|;/)[0];
}
}





exports.Route = Route;

module.exports = Route;
exports.Utils = Utils;
46 changes: 4 additions & 42 deletions lib/Router/Mapper/SimpleRoute.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var Route = require('./Route');
var Route = require('./Route').Route,
Utils = require('./Route').Utils;


/**
Expand All @@ -9,12 +10,6 @@ var SimpleRoute = Klass({
Extends: Route,

attributes: {
_name: null,
_path: null,
_controller: null,
_action: null,
_method: null,
_regex: null,
_special_segments: ['controller', 'action', 'method', 'to']
},

Expand Down Expand Up @@ -63,8 +58,8 @@ var SimpleRoute = Klass({
},

_generateRegex: function(path, options) {
var constrained = this.constraintize(path, this._getConstraints(options));
var standard = this.standardize(constrained);
var constrained = Utils.constraintize(path, this._getConstraints(options));
var standard = Utils.standardize(constrained);
var r = new RegExp('^' + standard + '$');
this._regex = r;
},
Expand All @@ -88,39 +83,6 @@ var SimpleRoute = Klass({
_generatePath: function(path) {
if(path && typeof(path) == 'string')
this._path = path
},


/**
* @public
* Getters
*/
getName: function() {
return this._name;
},

getPath: function() {
return this._path;
},

getController: function() {
return this._controller;
},

getAction: function() {
return this._action;
},

getName: function() {
return this._name;
},

getMethod: function() {
return this._method;
},

getRegex: function() {
return this._regex;
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions lib/Router/Parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var Parser = (function() {
return {
parsed_routes: {},

parseRouteFor: function(url) {
parseRouteFor: function(url, request) {
named_items = {};
current_url = removeQS(url);
var route_found = false;
Expand All @@ -72,7 +72,7 @@ var Parser = (function() {

var l = RouteStack.length;
for(var i = 0; i < l; i++) {
if(RouteStack[i].regex.test(current_url)) {
if(RouteStack[i].regex.test(current_url) && (RouteStack[i].method == request.method)) {
setNamedItems(RouteStack[i]);
route_found = true;
break;
Expand Down

0 comments on commit 883cc91

Please sign in to comment.