module Interaktor::Hooks
Internal: Methods relating to supporting hooks around Interaktor
invocation.
Public Class Methods
Internal: Install Interaktor's behavior in the given class.
# File lib/interaktor/hooks.rb, line 4 def self.included(base) base.class_eval do extend ClassMethods end end
Private Instance Methods
Internal: Run after hooks.
Returns nothing.
# File lib/interaktor/hooks.rb, line 236 def run_after_hooks run_hooks(self.class.after_hooks) end
Internal: Run around hooks.
Returns nothing.
# File lib/interaktor/hooks.rb, line 220 def run_around_hooks(&block) self.class.around_hooks.reverse.inject(block) { |chain, hook| proc { run_hook(hook, chain) } }.call end
Internal: Run before hooks.
Returns nothing.
# File lib/interaktor/hooks.rb, line 229 def run_before_hooks run_hooks(self.class.before_hooks) end
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/interaktor/hooks.rb, line 261 def run_hook(hook, *args) hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook) end
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/interaktor/hooks.rb, line 246 def run_hooks(hooks) hooks.each { |hook| run_hook(hook) } end
Internal: Run around, before and after hooks around yielded execution. The required block is surrounded with hooks and executed.
Examples
class MyProcessor include Interaktor::Hooks def process_with_hooks with_hooks do process end end def process puts "processed!" end end
Returns nothing.
# File lib/interaktor/hooks.rb, line 209 def with_hooks run_around_hooks do run_before_hooks yield run_after_hooks end end