module Roda::RodaPlugins::MultiRoute

  1. lib/roda/plugins/multi_route.rb

The multi_route plugin builds on the named_routes plugin and allows for dispatching to multiple named routes # by calling the r.multi_route method, which will check # if the first segment in the path matches a named route, and dispatch to that named route.

The hash_branches plugin offers a r.hash_branches method that is similar to and performs better than the r.multi_route method, and it is recommended to consider using that instead of this plugin.

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
end

Note that only named routes with string names will be dispatched to by the r.multi_route method. Named routes with other names can be dispatched to using the named_routes plugin API, but will not be automatically dispatched to by r.multi_route.

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

If a block is not provided to multi_route, the return value of the named route block will be used.

Namespace Support

The multi_route plugin also has support for namespaces, allowing you to use r.multi_route at multiple levels in your routing tree. Example:

route('foo') do |r|
  r.multi_route('foo')
end

route('bar') do |r|
  r.multi_route('bar')
end

route('baz', 'foo') do |r|
  # handles /foo/baz prefix
end

route('quux', 'foo') do |r|
  # handles /foo/quux prefix
end

route('baz', 'bar') do |r|
  # handles /bar/baz prefix
end

route('quux', 'bar') do |r|
  # handles /bar/quux prefix
end

route do |r|
  r.multi_route
end

Methods

Public Class

  1. configure
  2. load_dependencies

Public Class methods

configure(app)

Initialize storage for the named routes.

[show source]
   # File lib/roda/plugins/multi_route.rb
89 def self.configure(app)
90   app::RodaRequest.instance_variable_set(:@namespaced_route_regexps, {})
91 end
load_dependencies(app)
[show source]
   # File lib/roda/plugins/multi_route.rb
84 def self.load_dependencies(app)
85   app.plugin :named_routes
86 end