Module: DebugLogging::Hooks::ClassMethods

Defined in:
lib/debug_logging/hooks.rb

Instance Method Summary collapse

Instance Method Details

#debug_after(*names, &blk) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/debug_logging/hooks.rb', line 62

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_call(self, *args, &block)
      instance_exec(result, &blk)
    end
  end
end

#debug_before(*names, &blk) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/debug_logging/hooks.rb', line 47

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_call(self, *args, &block)
    end
  end
end

#debug_rescue_on_fail(*names, &blk) ⇒ Object



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

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|
      meth.bind_call(self, *args, &block)
    rescue StandardError => e
      instance_exec(e, &blk)
    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
# 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|
      Timeout.timeout(time) do
        meth.bind_call(self, *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