0.0
No commit activity in last 3 years
No release in over 3 years
A plugin for Roda to resolve actions from roda-container
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 10.3
>= 0
~> 3.2

Runtime

 Project Readme

roda-action Join the chat at https://gitter.im/AMHOL/roda-action

A plugin for Roda to resolve actions from roda-container

Installation

gem 'roda-action', '0.1.0'

Usage

class MyApplication < Roda
  plugin :json
  plugin :action
end

class UsersController
  attr_reader :repository

  def initialize(repository = [])
    @repository = repository
  end

  def index
    repository
  end

  def show(user_id)
    repository[user_id.to_i - 1]
  end
end

MyApplication.register(:users_controller) do
  UsersController.new([
    { name: 'Jack' },
    { name: 'Gill' }
  ])
end

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

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

If you wish to have access to the usual Roda application instance methods in your registered controllers, it is recommended that you register the application and resolve it for use in your controllers:

NOTE: Be sure to use .instance as below, see: https://github.com/AMHOL/roda-container#thread-safety

class MyApplication < Roda
  plugin :json
  plugin :action
end

class UsersController
  attr_reader :app, :repository

  def initialize(app, repository = [])
    @app, @repository = app, repository
  end

  def index
    repository
  end

  def show(user_id)
    repository[user_id.to_i - 1]
  end

  def create
    id = repository.length.next

    if (name = app.request.params['name'].to_s).length > 0
      repository << { name: name }
      app.response.redirect "/users/#{id}"
    end
  end
end

MyApplication.register(:users_repository, [
  { name: 'Jack' },
  { name: 'Gill' }
])

MyApplication.register(:users_controller) do
  UsersController.new(
    MyApplication.instance.resolve(:app),
    MyApplication.resolve(:users_repository)
  )
end

MyApplication.route do |r|
  register(:app, self, call: false)

  r.on 'users' do
    r.is do
      r.get(&action(:users_controller, :index))
      r.post(&action(:users_controller, :create))
    end

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

Contributing

  1. Fork it ( https://github.com/AMHOL/roda-action )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

MIT