Module: Roda::RodaPlugins::Action

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

Overview

The action plugin loads the container plugin and adds the “action” class method to your Roda application.

You can then register your controllers with the app container and resolve a method on your controller to pass as the matcher block.

Example:

plugin :action

class UsersController
  attr_reader :repository

  def initialize(repository = {})
    @repository = repository
  end

  def index
    repository.values
  end

  def show(user_id)
    repository[user_id]
  end
end

MyApplication.register(:users_controller) do
  UsersController.new({
    '1' => { name: 'Jack' },
    '2' => { name: 'Gill' }
  })
end

route do |r|
  r.on 'users' do
    r.is do
      r.get(&MyApplication.action(:users_controller, :index))
    end

    r.is :id do |id|
      r.get(&MyApplication.action(:users_controller, :show).bind_arguments(id))
    end
  end
end

Defined Under Namespace

Modules: ClassMethods Classes: Action

Class Method Summary collapse

Class Method Details

.load_dependencies(app, _opts = nil) ⇒ Object

Load the container plugin, since the action plugin depends on it.



69
70
71
# File 'lib/roda/plugins/action.rb', line 69

def self.load_dependencies(app, _opts = nil)
  app.plugin :container
end