module Interactor::Hooks

Internal: Methods relating to supporting hooks around Interactor invocation.

Public Class Methods

included(base) click to toggle source

Internal: Install Interactor's behavior in the given class.

# File lib/interactor/hooks.rb, line 5
def self.included(base)
  base.class_eval do
    extend ClassMethods
  end
end

Private Instance Methods

run_after_hooks() click to toggle source

Internal: Run after hooks.

Returns nothing.

# File lib/interactor/hooks.rb, line 237
def run_after_hooks
  run_hooks(self.class.after_hooks)
end
run_around_hooks(&block) click to toggle source

Internal: Run around hooks.

Returns nothing.

# File lib/interactor/hooks.rb, line 221
def run_around_hooks(&block)
  self.class.around_hooks.reverse.inject(block) { |chain, hook|
    proc { run_hook(hook, chain) }
  }.call
end
run_before_hooks() click to toggle source

Internal: Run before hooks.

Returns nothing.

# File lib/interactor/hooks.rb, line 230
def run_before_hooks
  run_hooks(self.class.before_hooks)
end
run_hook(hook, *args) click to toggle source

Internal: Run an individual hook. The “run_hook” method is the common interface by which an individual hook is run. If the given hook is a symbol, the method is invoked whether public or private. If the hook is a proc, the proc is evaluated in the context of the current instance.

hook - A Symbol or Proc hook. args - Zero or more arguments to be passed as block arguments into the

given block or as arguments into the method described by the given
Symbol method name.

Returns nothing.

# File lib/interactor/hooks.rb, line 262
def run_hook(hook, *args)
  hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
end
run_hooks(hooks) click to toggle source

Internal: Run a colection of hooks. The “run_hooks” method is the common interface by which collections of either before or after hooks are run.

hooks - An Array of Symbol and Proc hooks.

Returns nothing.

# File lib/interactor/hooks.rb, line 247
def run_hooks(hooks)
  hooks.each { |hook| run_hook(hook) }
end
with_hooks() { || ... } click to toggle source

Internal: Run around, before and after hooks around yielded execution. The required block is surrounded with hooks and executed.

Examples

class MyProcessor
  include Interactor::Hooks

  def process_with_hooks
    with_hooks do
      process
    end
  end

  def process
    puts "processed!"
  end
end

Returns nothing.

# File lib/interactor/hooks.rb, line 210
def with_hooks
  run_around_hooks do
    run_before_hooks
    yield
    run_after_hooks
  end
end