Module: Lotus::Controller::Dsl::ClassMethods

Defined in:
lib/lotus/controller/dsl.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#action(name, &blk) ⇒ Object

Define an action for the given name. It generates a concrete class for the action, for this reason the name MUST be a valid name for Ruby.

Examples:

require 'lotus/controller'

class ArticlesController
  include Lotus::Controller

  action 'Index' do
    def call(params)
      # ...
    end
  end

  action 'Show' do
    def call(params)
      # ...
    end
  end
end

Parameters:

  • name (String)

    the name of the action

  • blk (Proc)

    the code of the action

Raises:

  • TypeError when the name isn’t a valid Ruby name

See Also:

Since:

  • 0.1.0



43
44
45
46
47
48
49
50
51
52
# File 'lib/lotus/controller/dsl.rb', line 43

def action(name, &blk)
  config = configuration.duplicate
  const_set(name, Class.new)

  const_get(name).tap do |klass|
    klass.class_eval { include config.action_module }
    klass.configuration = config
    klass.class_eval(&blk)
  end
end