module Hanami::Action::Callbacks::ClassMethods

Callbacks API class methods

@since 0.1.0 @api private

Public Class Methods

extended(base) click to toggle source

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

@param base [Class] the target action

@since 0.1.0 @api private

@see www.ruby-doc.org/core/Module.html#method-i-extended

# 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

Public Instance Methods

after(*callbacks, &blk)

@since 0.1.0

Alias for: append_after
append_after(*callbacks, &blk) click to toggle source

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

@param 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.

@param blk [Proc] an anonymous function to be executed

@return [void]

@since 0.3.2

@see Hanami::Action::Callbacks::ClassMethods#append_before

# File lib/hanami/action/callbacks.rb, line 138
def append_after(*callbacks, &blk)
  after_callbacks.append(*callbacks, &blk)
end
Also aliased as: after
append_before(*callbacks, &blk) click to toggle source

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

@param 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.

@param blk [Proc] an anonymous function to be executed

@return [void]

@since 0.3.2

@see Hanami::Action::Callbacks::ClassMethods#append_after

@example 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

@example 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
# File lib/hanami/action/callbacks.rb, line 116
def append_before(*callbacks, &blk)
  before_callbacks.append(*callbacks, &blk)
end
Also aliased as: before
before(*callbacks, &blk)

@since 0.1.0

Alias for: append_before
prepend_after(*callbacks, &blk) click to toggle source

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.

@param 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.

@param blk [Proc] an anonymous function to be executed

@return [void]

@since 0.3.2

@see Hanami::Action::Callbacks::ClassMethods#prepend_before

# File lib/hanami/action/callbacks.rb, line 179
def prepend_after(*callbacks, &blk)
  after_callbacks.prepend(*callbacks, &blk)
end
prepend_before(*callbacks, &blk) click to toggle source

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.

@param 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.

@param blk [Proc] an anonymous function to be executed

@return [void]

@since 0.3.2

@see Hanami::Action::Callbacks::ClassMethods#prepend_after

# File lib/hanami/action/callbacks.rb, line 160
def prepend_before(*callbacks, &blk)
  before_callbacks.prepend(*callbacks, &blk)
end