module Roda::RodaPlugins::MultiRoute

The multi_route plugin allows for multiple named routes, which the main route block can dispatch to by name at any point by calling route. If the named route doesn’t handle the request, execution will continue, and if the named route does handle the request, the response returned by the named route will be returned.

In addition, this also adds the r.multi_route method, which will assume check if the first segment in the path matches a named route, and dispatch to that named route.

Example:

plugin :multi_route

route('foo') do |r|
  r.is 'bar' do
    '/foo/bar'
  end
end

route('bar') do |r|
  r.is 'foo' do
    '/bar/foo'
  end
end

route do |r|
  r.multi_route

  # or

  r.on "foo" do
    r.route 'foo'
  end

  r.on "bar" do
    r.route 'bar'
  end
end

Note that in multi-threaded code, you should not attempt to add a named route after accepting requests.

If you want to use the r.multi_route method, use string names for the named routes. Also, you can provide a block to r.multi_route that is called if the route matches but the named route did not handle the request:

r.multi_route do
  "default body"
end

Public Class Methods

configure(app) click to toggle source

Initialize storage for the named routes.

# File lib/roda/plugins/multi_route.rb, line 56
def self.configure(app)
  app.instance_exec{@named_routes ||= {}}
end