Module: Interaktor::Hooks::ClassMethods

Defined in:
lib/interaktor/hooks.rb

Overview

Internal: Interaktor::Hooks class methods.

Instance Method Summary collapse

Instance Method Details

#after(*hooks, &block) ⇒ Object

Public: Declare hooks to run after Interaktor 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 interaktor 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 MyInteraktor
  include Interaktor

  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.



124
125
126
127
# File 'lib/interaktor/hooks.rb', line 124

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 Interaktor invocation. The hooks appear in the order in which they will be run.

Examples

class MyInteraktor
  include Interaktor

  after :set_finish_time, :say_goodbye
end

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

Returns an Array of Symbols and Procs.



219
220
221
# File 'lib/interaktor/hooks.rb', line 219

def after_hooks
  @after_hooks ||= []
end

#around(*hooks, &block) ⇒ Object

Public: Declare hooks to run around Interaktor 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 interaktor 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 MyInteraktor
  include Interaktor

  around :time_execution

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

  def call
    puts "called"
  end

  private

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

Returns nothing.



50
51
52
53
# File 'lib/interaktor/hooks.rb', line 50

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 Interaktor invocation. The hooks appear in the order in which they will be run.

Examples

class MyInteraktor
  include Interaktor

  around :time_execution, :use_transaction
end

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

Returns an Array of Symbols and Procs.



181
182
183
# File 'lib/interaktor/hooks.rb', line 181

def around_hooks
  @around_hooks ||= []
end

#before(*hooks, &block) ⇒ Object

Public: Declare hooks to run before Interaktor 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 interaktor 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 MyInteraktor
  include Interaktor

  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.



87
88
89
90
# File 'lib/interaktor/hooks.rb', line 87

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 Interaktor invocation. The hooks appear in the order in which they will be run.

Examples

class MyInteraktor
  include Interaktor

  before :set_start_time, :say_hello
end

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

Returns an Array of Symbols and Procs.



200
201
202
# File 'lib/interaktor/hooks.rb', line 200

def before_hooks
  @before_hooks ||= []
end

#ensure_hook(*hooks, &block) ⇒ Object

Public: Declare hooks to run after Interaktor invocation in an ensure block. The after method may be called multiple times; subsequent calls append declared hooks to existing ensure_hook hooks.

hooks - Zero or more Symbol method names representing instance methods

to be called after interaktor 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 MyInteraktor
  include Interaktor

  ensure_hook :close_file

  ensure_hook do
    puts "finished"
  end

  def call
    puts "called"
  end

  private

  def close_file
    context.file.close
  end
end

Returns nothing.



161
162
163
164
# File 'lib/interaktor/hooks.rb', line 161

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

#ensure_hooksObject

Internal: An Array of declared hooks to run afer Interaktor invocation in an ensure block. The hooks appear in the order in which they will be run.

Examples

class MyInteraktor
  include Interaktor

  ensure_hook :set_finish_time, :say_goodbye
end

MyInteraktor.ensure_hooks
# => [:say_goodbye, :set_finish_time]

Returns an Array of Symbols and Procs.



239
240
241
# File 'lib/interaktor/hooks.rb', line 239

def ensure_hooks
  @ensure_hooks ||= []
end