Module: Runbook::Hooks::Invoker

Included in:
Entity, Statement
Defined in:
lib/runbook/hooks.rb

Instance Method Summary collapse

Instance Method Details

#_execute_after_hooks(executor, object, *args) ⇒ Object



81
82
83
84
85
# File 'lib/runbook/hooks.rb', line 81

def _execute_after_hooks(executor, object, *args)
  executor.hooks_for(:after, object.class).each do |hook|
    executor.instance_exec(object, *args, &hook[:block])
  end
end

#_execute_around_hooks(executor, object, *args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/runbook/hooks.rb', line 66

def _execute_around_hooks(executor, object, *args)
  executor.hooks_for(:around, object.class).reverse.reduce(
    -> (object, *args) {
      yield
    }
  ) do |inner_method, hook|
    -> (object, *args) {
      inner_block = Proc.new do |object, *args|
        inner_method.call(object, *args)
      end
      executor.instance_exec(object, *args, inner_block, &hook[:block])
    }
  end.call(object, *args)
end

#_execute_before_hooks(executor, object, *args) ⇒ Object



60
61
62
63
64
# File 'lib/runbook/hooks.rb', line 60

def _execute_before_hooks(executor, object, *args)
  executor.hooks_for(:before, object.class).each do |hook|
    executor.instance_exec(object, *args, &hook[:block])
  end
end

#invoke_with_hooks(executor, object, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/runbook/hooks.rb', line 33

def invoke_with_hooks(executor, object, *args, &block)
  skip_before = skip_around = skip_after = false
  if executor <= Runbook::Run
    if executor.should_skip?(args[0])
      if executor.start_at_is_substep?(object, args[0])
        skip_before = skip_around = true
      else
        skip_before = skip_around = skip_after = true
      end
    end
  end

  unless skip_before
    _execute_before_hooks(executor, object, *args)
  end

  if skip_around
    block.call
  else
    _execute_around_hooks(executor, object, *args, &block)
  end

  unless skip_after
    _execute_after_hooks(executor, object, *args)
  end
end