Module: Lotus::Action::Callbacks::ClassMethods

Defined in:
lib/lotus/action/callbacks.rb

Overview

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Since:

  • 0.1.0



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lotus/action/callbacks.rb', line 20

def self.extended(base)
  base.class_eval do
    include Utils::ClassAttribute

    class_attribute :before_callbacks
    self.before_callbacks = Utils::Callbacks::Chain.new

    class_attribute :after_callbacks
    self.after_callbacks = Utils::Callbacks::Chain.new
  end
end

Instance Method Details

#after(*callbacks, &blk) ⇒ void

This method returns an undefined value.

Define a callback for an Action. The callback will be executed after the action is called, in the order they are added.

Parameters:

  • callbacks (Symbol, Array<Symbol>)

    a single or multiple symbol(s) each of them is representing a name of a method available in the context of the Action.

  • blk (Proc)

    an anonymous function to be executed

See Also:

Since:

  • 0.1.0



111
112
113
# File 'lib/lotus/action/callbacks.rb', line 111

def after(*callbacks, &blk)
  after_callbacks.add *callbacks, &blk
end

#before(*callbacks, &blk) ⇒ void

This method returns an undefined value.

Define a callback for an Action. The callback will be executed before the action is called, in the order they are added.

Examples:

Method names (symbols)

require 'lotus/controller'

class Show
  include Lotus::Action

  before :authenticate, :set_article

  def call(params)
  end

  private
  def authenticate
    # ...
  end

  # `params` in the method signature is optional
  def set_article(params)
    @article = Article.find params[:id]
  end
end

# The order of execution will be:
#
# 1. #authenticate
# 2. #set_article
# 3. #call

Anonymous functions (Procs)

require 'lotus/controller'

class Show
  include Lotus::Action

  before { ... } # 1 do some authentication stuff
  before {|params| @article = Article.find params[:id] } # 2

  def call(params)
  end
end

# The order of execution will be:
#
# 1. authentication
# 2. set the article
# 3. #call

Parameters:

  • callbacks (Symbol, Array<Symbol>)

    a single or multiple symbol(s) each of them is representing a name of a method available in the context of the Action.

  • blk (Proc)

    an anonymous function to be executed

Since:

  • 0.1.0



92
93
94
# File 'lib/lotus/action/callbacks.rb', line 92

def before(*callbacks, &blk)
  before_callbacks.add *callbacks, &blk
end