ground
Version:
A framework work with sails and backbone.
71 lines (55 loc) • 1.97 kB
text/coffeescript
# Author: Andy Zhao(andy@nodeswork.com)
_ = require 'underscore'
class GroundRoutes
: ''
: {}
: {}
: {}
: (req) ->
req.paths = Object.create {},
req.paths.req = req
: () ->
?= do =>
res = {}
for key, val of
do (val) ->
res[key] = get: ->
val.replace /:([\w\d_-]+)/g, (match, p1) =>
unless .params[p1]? then throw new Error "param '#{p1}' not found."
.params[p1]
res
: ->
result = {}
emit = (prefix, route, config) =>
routePaths = route.split ' '
bindPath = prefix + _.last routePaths
if bindPath.length > 1 and _.last(bindPath) is '/'
bindPath = bindPath.substring(0, bindPath.length - 1)
routePaths[routePaths.length - 1] = bindPath
if config.as?
[config.as] = bindPath
as = config.as
do (as) =>
[as] = (params) =>
[as].replace /:([\w\d_-]+)/g, (match, p1) =>
unless params[p1]? then throw new Error "param '#{p1}' not found."
params[p1]
delete config.as
if config.action?
actions = config.action.split '.'
if actions.length?
config = _.compact((config.controller || '').split('.').concat(actions)).join('.')
if config.controller?
config = config.controller
result[routePaths.join ' '] = config
generateRoute = (routes, prefix='') ->
return unless _.isObject(routes)
for key, val of routes
switch
when _.isString(val) then emit prefix, key, val
when _.isObject(val) and (val.controller? or val.view? or val.action?)
emit prefix, key, val
when _.isObject val then generateRoute val, prefix + key
generateRoute ,
result
module.exports = GroundRoutes