Module: Interactor::Hooks::ClassMethods

Defined in:
lib/interactor/hooks.rb

Overview

Internal: Interactor::Hooks class methods.

Instance Method Summary collapse

Instance Method Details

#after(*hooks, &block) ⇒ Object

Public: Declare hooks to run after Interactor invocation. The after method may be called multiple times; subsequent calls prepend declared hooks to existing after hooks.

hooks - Zero or more Symbol method names representing instance methods to be called after interactor invocation. block - An optional block to be executed as a hook. If given, the block is executed before methods corresponding to any given Symbols.

Examples

class MyInteractor
include Interactor

after :set_finish_time

after do
  puts "finished"
end

def call
  puts "called"
end

private

def set_finish_time
  context.finish_time = Time.now
end
end

Returns nothing.



125
126
127
128
# File 'lib/interactor/hooks.rb', line 125

def after(*hooks, &block)
  hooks << block if block
  hooks.each { |hook| after_hooks.unshift(hook) }
end

#after_hooksObject

Internal: An Array of declared hooks to run before Interactor invocation. The hooks appear in the order in which they will be run.

Examples

class MyInteractor
include Interactor

after :set_finish_time, :say_goodbye
end

MyInteractor.after_hooks
# => [:say_goodbye, :set_finish_time]

Returns an Array of Symbols and Procs.



183
184
185
# File 'lib/interactor/hooks.rb', line 183

def after_hooks
  @after_hooks ||= []
end

#around(*hooks, &block) ⇒ Object

Public: Declare hooks to run around Interactor invocation. The around method may be called multiple times; subsequent calls append declared hooks to existing around hooks.

hooks - Zero or more Symbol method names representing instance methods to be called around interactor invocation. Each instance method invocation receives an argument representing the next link in the around hook chain. block - An optional block to be executed as a hook. If given, the block is executed after methods corresponding to any given Symbols.

Examples

class MyInteractor
include Interactor

around :time_execution

around do |interactor|
  puts "started"
  interactor.call
  puts "finished"
end

def call
  puts "called"
end

private

def time_execution(interactor)
  context.start_time = Time.now
  interactor.call
  context.finish_time = Time.now
end
end

Returns nothing.



51
52
53
54
# File 'lib/interactor/hooks.rb', line 51

def around(*hooks, &block)
  hooks << block if block
  hooks.each { |hook| around_hooks.push(hook) }
end

#around_hooksObject

Internal: An Array of declared hooks to run around Interactor invocation. The hooks appear in the order in which they will be run.

Examples

class MyInteractor
include Interactor

around :time_execution, :use_transaction
end

MyInteractor.around_hooks
# => [:time_execution, :use_transaction]

Returns an Array of Symbols and Procs.



145
146
147
# File 'lib/interactor/hooks.rb', line 145

def around_hooks
  @around_hooks ||= []
end

#before(*hooks, &block) ⇒ Object

Public: Declare hooks to run before Interactor invocation. The before method may be called multiple times; subsequent calls append declared hooks to existing before hooks.

hooks - Zero or more Symbol method names representing instance methods to be called before interactor invocation. block - An optional block to be executed as a hook. If given, the block is executed after methods corresponding to any given Symbols.

Examples

class MyInteractor
include Interactor

before :set_start_time

before do
  puts "started"
end

def call
  puts "called"
end

private

def set_start_time
  context.start_time = Time.now
end
end

Returns nothing.



88
89
90
91
# File 'lib/interactor/hooks.rb', line 88

def before(*hooks, &block)
  hooks << block if block
  hooks.each { |hook| before_hooks.push(hook) }
end

#before_hooksObject

Internal: An Array of declared hooks to run before Interactor invocation. The hooks appear in the order in which they will be run.

Examples

class MyInteractor
include Interactor

before :set_start_time, :say_hello
end

MyInteractor.before_hooks
# => [:set_start_time, :say_hello]

Returns an Array of Symbols and Procs.



164
165
166
# File 'lib/interactor/hooks.rb', line 164

def before_hooks
  @before_hooks ||= []
end