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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override Ruby’s hook for modules. It includes callbacks logic

Parameters:

  • base (Class)

    the target action

See Also:

Since:

  • 0.1.0



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lotus/action/callbacks.rb', line 38

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



129
130
131
# File 'lib/lotus/action/callbacks.rb', line 129

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



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

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