Method: Hanami::Utils::Callbacks::Chain#run

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

#run(context, *args) ⇒ Object

Runs all the callbacks in the chain. The only two ways to stop the execution are: raise or throw.

Examples:

require 'hanami/utils/callbacks'

class Action
  private
  def authenticate!
  end

  def set_article(params)
  end
end

action = Action.new
params = Hash[id: 23]

chain = Hanami::Utils::Callbacks::Chain.new
chain.append :authenticate!, :set_article

chain.run(action, params)

# `params` will only be passed as #set_article argument, because it has an arity greater than zero

chain = Hanami::Utils::Callbacks::Chain.new

chain.append do
  # some authentication logic
end

chain.append do |params|
  # some other logic that requires `params`
end

chain.run(action, params)

Those callbacks will be invoked within the context of `action`.

Since:

  • 0.1.0



152
153
154
155
156
# File 'lib/hanami/utils/callbacks.rb', line 152

def run(context, *args)
  @chain.each do |callback|
    callback.call(context, *args)
  end
end