Module: RuleBox::ExecutionHook::ClassMethods

Defined in:
lib/rule_box/execution_hook.rb

Instance Method Summary collapse

Instance Method Details

#call_hook(type, arg = nil) ⇒ Object



22
23
24
25
26
27
# File 'lib/rule_box/execution_hook.rb', line 22

def call_hook(type, arg = nil)
  block = hooks[type.to_sym]
  return nil unless block.is_a? Proc

  block.call(arg)
end

#clear_hooks!Object



29
30
31
32
# File 'lib/rule_box/execution_hook.rb', line 29

def clear_hooks!
  @rescue_handlers = []
  @hooks = {}
end

#has_hook?(name) ⇒ Boolean



34
35
36
# File 'lib/rule_box/execution_hook.rb', line 34

def has_hook?(name)
  hooks[name.to_sym].is_a? Proc
end

#rescue_from(*klasses, &block) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rule_box/execution_hook.rb', line 38

def rescue_from(*klasses, &block)
  klasses.each do |klass|
    match = klass < Exception || klass == Exception
    raise ArgumentError, "#{klass.inspect} must be an Exception class" unless match

    rescue_handlers.concat [[klass, block]]
  end
end

#rescue_handlersObject



47
48
49
# File 'lib/rule_box/execution_hook.rb', line 47

def rescue_handlers
  @rescue_handlers ||= []
end

#resolve_exception!(facade) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rule_box/execution_hook.rb', line 51

def resolve_exception!(facade)
  rescue_handlers.each do |exception, block|
    next unless facade.exception.is_a? exception

    block.call facade
    return true
  end

  nil
end