The static_routing plugin adds static_* routing class methods for handling static routes (i.e. routes with static paths, no nesting or placeholders). These routes are processed before the normal routing tree and designed for maximum performance. This can be substantially faster than Roda’s normal tree based routing if you have large numbers of static routes, about 3-4x for 100-10000 static routes. Example:
plugin :static_routing static_route '/foo' do |r| @var = :foo r.get do 'Not actually reached' end r.post{'static POST /#{@var}'} end static_get '/foo' do |r| 'static GET /foo' end route do |r| 'Not a static route' end
A few things to note in the above example. First, unlike most other routing methods in Roda
, these take the full path of the request, and only match if r.path_info matches exactly. This is why you need to include the leading slash in the path argument.
Second, the static_* routing methods only take a single string argument for the path, they do not accept other options, and do not handle placeholders in strings. For any routes needing placeholders, you should use Roda’s routing tree.
There are separate static_* methods for each type of request method, and these request method specific routes are tried first. There is also a static_route method that will match regardless of the request method, if there is no matching request methods specific route. This is why the static_get method call takes precedence over the static_route method call for /foo. As shown above, you can use Roda’s routing tree methods inside the static_route block to have shared behavior for different request methods, while still handling the request methods differently.
Classes and Modules
Public Class methods
# File lib/roda/plugins/static_routing.rb 52 def self.load_dependencies(app) 53 app.plugin :hash_paths 54 end