Module: DebugLogging::Hooks::ClassMethods

Defined in:
lib/debug_logging/hooks.rb

Instance Method Summary collapse

Instance Method Details

#debug_after(*names, &blk) ⇒ Object



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

def debug_after(*names, &blk)
  unless blk
    raise NoBlockGiven,
          '.after must be called with a block',
          caller
  end
  names.each do |name|
    meth = instance_method(name)
    define_method name do |*args, &block|
      result = meth.bind(self).call(*args, &block)
      instance_exec(result, &blk)
    end
  end
end

#debug_before(*names, &blk) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/debug_logging/hooks.rb', line 51

def debug_before(*names, &blk)
  unless blk
    raise NoBlockGiven,
          '.before must be called with a block',
          caller
  end
  names.each do |name|
    meth = instance_method(name)
    define_method name do |*args, &block|
      instance_exec(name, *args, block, &blk)
      meth.bind(self).call(*args, &block)
    end
  end
end

#debug_rescue_on_fail(*names, &blk) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/debug_logging/hooks.rb', line 33

def debug_rescue_on_fail(*names, &blk)
  unless blk
    raise NoBlockGiven,
          '.rescue_on_fail must be called with a block',
          caller
  end
  names.each do |name|
    meth = instance_method(name)
    define_method(name) do |*args, &block|
      begin
        meth.bind(self).call(*args, &block)
      rescue StandardError => e
        instance_exec(e, &blk)
      end
    end
  end
end

#debug_time_box(time, *names, &blk) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/debug_logging/hooks.rb', line 15

def debug_time_box(time, *names, &blk)
  names.each do |name|
    meth = instance_method(name)
    define_method(name) do |*args, &block|
      begin
        Timeout.timeout(time) do
          meth.bind(self).call(*args, &block)
        end
      rescue Timeout::Error
        error_args = [TimeoutError, 'execution expired', caller]
        raise(*error_args) unless blk

        instance_exec(*error_args, &blk)
      end
    end
  end
end