Module: Hanami::Action::Callbacks::ClassMethods Private

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

Overview

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

Callbacks API class methods

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


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hanami/action/callbacks.rb', line 42

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

#append_after(*callbacks, &blk) ⇒ void Also known as: after

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.

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.3.2


138
139
140
# File 'lib/hanami/action/callbacks.rb', line 138

def append_after(*callbacks, &blk)
  after_callbacks.append(*callbacks, &blk)
end

#append_before(*callbacks, &blk) ⇒ void Also known as: before

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.

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 'hanami/controller'

class Show
  include Hanami::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 'hanami/controller'

class Show
  include Hanami::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

See Also:

Since:

  • 0.3.2


116
117
118
# File 'lib/hanami/action/callbacks.rb', line 116

def append_before(*callbacks, &blk)
  before_callbacks.append(*callbacks, &blk)
end

#prepend_after(*callbacks, &blk) ⇒ void

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.

This method returns an undefined value.

Define a callback for an Action. The callback will be executed *after* the action is called. It will add the callback at the beginning of the callbacks' chain.

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.3.2


179
180
181
# File 'lib/hanami/action/callbacks.rb', line 179

def prepend_after(*callbacks, &blk)
  after_callbacks.prepend(*callbacks, &blk)
end

#prepend_before(*callbacks, &blk) ⇒ void

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.

This method returns an undefined value.

Define a callback for an Action. The callback will be executed *before* the action is called. It will add the callback at the beginning of the callbacks' chain.

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.3.2


160
161
162
# File 'lib/hanami/action/callbacks.rb', line 160

def prepend_before(*callbacks, &blk)
  before_callbacks.prepend(*callbacks, &blk)
end