= roda-route_list
The Roda route_list plugin reads route information from a json
file, and then makes the route metadata available for
introspection. This provides a workaround to the general
issue of routing trees being unable to introspect the routes.
== Installation
gem install roda-route_list
== Source Code
Source code is available on GitHub at https://github.com/jeremyevans/roda-route_list
== Basic Usage
This plugin assumes that a json file containing the routes
metadata has already been created. The recommended way to
create one is to add comments above each route in the Roda
app, in one of the following formats:
route: /path/to/foo
route: GET /path/to/foo
route: GET|POST /path/to/foo/:foo_id
route[route_name]: /path/to/foo
route[route_name]: GET /path/to/foo
route[foo]: GET|POST /path/to/foo/:foo_id
As you can see, the general style is a comment followed by
the word route. If you want to name the route, you can
put the name in brackets. Then you have a colon. Optionally
after that you can have the method for the route, or multiple
methods separated by pipes if the path works with multiple
methods. The end is the path for the route.
Assuming you have added the appropriate comments as explained
above, you can create the json file using the roda-parse_routes
executable that came with the roda-route_list gem:
roda-parse_routes -f routes.json app.rb
Assuming you have the necessary json file created, you can then
get route information:
plugin :route_list
Array of route metadata hashes
route_list # => [{:path=>'/path/to/foo', :methods=>['GET', 'POST']}]
path for the route with the given name
listed_route(:route_name) # => '/path/to/foo'
path for the route with the given name, supplying hash for placeholders
listed_route(:foo, :foo_id=>3) # => '/path/to/foo/3'
path for the route with the given name, supplying array for placeholders
listed_route(:foo, [3]) # => '/path/to/foo/3'
The +listed_route+ method is also available at the instance level to make it
easier to use inside the route block.
=== Automatically Updating the Routes Metadata
==== On Heroku
You can get this to work on Heroku by hooking into the facility for
precompiling assets. If this consider your route list an asset, this
makes sense. You just need to add an assets:precompile task, similar to
this (or add the code an existing assets:precompile task):
namespace :assets do
desc "Update the routes metadata"
task :precompile do
sh 'roda-parse_routes -f routes.json app.rb'
end
end
==== Otherwise
At the top of your Roda app file, or at least before you create the Roda app
subclass, just call the roda-parse_routes program:
app.rb
system 'roda-parse_routes', '-f', 'routes.json', FILE
require 'roda'
class App < Roda
# ...
end
== License
MIT
== Maintainer
Jeremy Evans code@jeremyevans.net