module Roda::RodaPlugins::ClassLevelRouting

  1. lib/roda/plugins/class_level_routing.rb

The class_level_routing plugin adds routing methods at the class level, which can be used instead of or in addition to using the normal route method to start the routing tree. If a request is not matched by the normal routing tree, the class level routes will be tried. This offers a more Sinatra-like API, while still allowing you to use a routing tree inside individual actions.

Here's the first example from the README, modified to use the class_level_routing plugin:

class App < Roda
  plugin :class_level_routing

  # GET / request
  root do
    request.redirect "/hello"

  # GET /hello/world request
  get "hello/world" do
    "Hello world!"

  # /hello request
  is "hello" do
    # Set variable for both GET and POST requests
    @greeting = 'Hello'

    # GET /hello request
    request.get do

    # POST /hello request do
      puts "Someone said #{@greeting}!"

When using the the class_level_routing plugin with nested routes, you may also want to use the delegate plugin to delegate certain instance methods to the request object, so you don't have to continually use request. in your routing blocks.

Note that class level routing is implemented via a simple array of routes, so routing performance will degrade linearly as the number of routes increases. For best performance, you should use the normal route class method to define your routing tree. This plugin does make it simpler to add additional routes after the routing tree has already been defined, though.


Public Class

  1. configure

Public Class methods

configure (app)

Initialize the class_routes array when the plugin is loaded. Also, if the application doesn't currently have a routing block, setup an empty routing block so that things will still work if a routing block isn't added.

[show source]
# File lib/roda/plugins/class_level_routing.rb, line 57
def self.configure(app)
  app.route{} unless app.route_block
  app.opts[:class_level_routes] ||= []