Module: Roda::RodaPlugins::MultiRoute

Defined in:
lib/roda/plugins/multi_route.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods, RequestClassMethods, RequestMethods

Class Method Summary collapse

Class Method Details

.configure(app) ⇒ Object

Initialize storage for the named routes.



89
90
91
# File 'lib/roda/plugins/multi_route.rb', line 89

def self.configure(app)
  app::RodaRequest.instance_variable_set(:@namespaced_route_regexps, {})
end

.load_dependencies(app) ⇒ Object



84
85
86
# File 'lib/roda/plugins/multi_route.rb', line 84

def self.load_dependencies(app)
  app.plugin :named_routes
end