framework-one/fw1 — pull request #191 by jcberquist (2013-09-12 13:53:48+00:00)
Title: first take on generating routes

I have been interested in route shortcuts being added to fw1 as per this discussion: https://groups.google.com/forum/#!searchin/framework-one/rest/framework-one/lF9ovBxRwrk/7cIqdjLuedIJ. I thought I would take a stab at it and seek some feedback. My goal was to implement shortcuts to writing out all of the standard routes for a given resource. Given that routes are not cached but are processed on every request, it seems like new routes need to be generated on the fly, as the appropriate keys were encountered in the routes array of structs. So I was trying to find a simple way to do that, that would still allow for some customization.

My approach was to add a new key to `variables.framework` called `routeDefinitions` whose value is expected to be a struct. One then defines a route pack (or an array of route packs) as follows:
````
variables.framework.routeDefinitions = {
'$RESOURCES' = {
'$GET/{1}/$' = '/{1}/index',
'$GET/{1}/new/$' = '/{1}/new',
'$POST/{1}/$' = '/{1}/create',
'$GET/{1}/:id/$' = '/{1}/show/id/:id',
'$PUT/{1}/:id/$' = '/{1}/update/id/:id',
'$DELETE/{1}/:id/$' = '/{1}/destroy/id/:id'
},
'$REST' = [ { }, { }, ... ]
};
````
Then when route processing encounters a route such as `'$RESOURCES/posts' = 'posts'` it will use the above definition to generate new routes on the fly, and process them. In effect, `'$RESOURCES/posts' = 'posts'` will be treated by the route processing as though one had written:
````
{
'$GET/posts/$' = '/posts/index',
'$GET/posts/new/$' = '/posts/new',
'$POST/posts/$' = '/posts/create',
'$GET/posts/:id/$' = '/posts/show/id/:id',
'$PUT/posts/:id/$' = '/posts/update/id/:id',
'$DELETE/posts/:id/$' = '/posts/destroy/id/:id'
}
````
I generate the routes by simple substitution of the value given (`'posts'` in the above example) treated as a list, by list position. Thus `'posts'` is substituted into the routeDefinition wherever `'{1}'` is encountered. This approach, while it does not seem to be the most elegant to me, does allow one to add additional substitutions if needed or desired. For instance, instead of `'$GET/{1}/:id/$' = '/{1}/show/id/:id'` one could have `'$GET/{1}/:{2}/$' = '/{1}/show/{2}/:{2}'` and then set `'$RESOURCES/posts'` equal to `'posts,post_id'`, resulting in `$GET/{1}/:{2}/$' = '/{1}/show/{2}/:{2}'` becoming '`$GET/posts/:post_id/$' = '/posts/show/post_id/:post_id'`.

One last thing I should point out is that one could specify the routes either by `'$RESOURCES/posts' = 'posts'` or more simply as `'$RESOURCES' = 'posts'`. Either way the same routes will be generated. What I did do however is make the assumption that if the key is `'$RESOURCES/posts'`, then the `'/posts'` part is specifying the root of the path. In such a case, new routes will only be generated if `/posts` is found at the root of the path (thus saving a little processing time, as the routes do not have to be created, converted to regular expressions, and matched).