Module: Hammertime

Included in:
Object
Defined in:
lib/hammertime19.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.intercept_nativeObject

Returns the value of attribute intercept_native.



17
18
19
# File 'lib/hammertime19.rb', line 17

def intercept_native
  @intercept_native
end

Class Method Details

.debug_supported?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/hammertime19.rb', line 37

def self.debug_supported?
  require 'ruby-debug'
  @debug_support = true
rescue LoadError
  warn "Unable to load ruby-debug"
  warn "Gem not installed or debugging not supported on your platform"
  @debug_support = false
end

.ignored_errorsObject



21
22
23
# File 'lib/hammertime19.rb', line 21

def self.ignored_errors
  @ignored_errors ||= [LoadError]
end

.ignored_linesObject



25
26
27
# File 'lib/hammertime19.rb', line 25

def self.ignored_lines
  @ignored_lines ||= []
end

.stoppedObject



29
30
31
# File 'lib/hammertime19.rb', line 29

def self.stopped
  @stopped ||= false
end

.stopped=(value) ⇒ Object



33
34
35
# File 'lib/hammertime19.rb', line 33

def self.stopped=(value)
  @stopped = value
end

Instance Method Details

#fail(*args) ⇒ Object



130
131
132
133
# File 'lib/hammertime19.rb', line 130

def fail(*args)
  caller_binding = binding.of_caller(1)
  hammertime_raise(*args) { caller_binding } 
end

#hammertime_original_raise(*args) ⇒ Object



126
127
128
# File 'lib/hammertime19.rb', line 126

def hammertime_original_raise(*args)
  Kernel.instance_method(:raise).bind(self).call(*args)
end

#hammertime_raise(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hammertime19.rb', line 46

def hammertime_raise(*args)
  backtrace = caller(2)
  fallback = lambda do
    hammertime_original_raise(*args)
  end
  exclusive_and_non_reentrant(fallback) do
    error, backtrace =
      case args.size
      when 0 then [($!.nil? ? RuntimeError.new : $!), backtrace]
      when 1 then
        if args[0].is_a?(String)
          [RuntimeError.exception(args[0]), backtrace]
        else
          [args[0].exception, backtrace]
        end
      when 2 then
        [args[0].exception(args[1]), backtrace]
      when 3 then
        [args[0].exception(args[1]), args[2]]
      else
        super(ArgumentError, "wrong number of arguments", backtrace)
      end
    error.set_backtrace(backtrace)

    if hammertime_ignore_error?(error, backtrace)
      hammertime_original_raise(error)
    else
      ::Hammertime.stopped = true
    end

    c = ::Hammertime.hammertime_console
    c.say "\n"
    c.say "=== Stop! Hammertime. ==="
    c.say "An error has occurred at #{backtrace.first}"
    c.say "The error is: #{error.inspect}"
    menu_config = lambda do |menu|
      menu.prompt    = "What now?"
      menu.default   = "Continue"
      menu.select_by = :index_or_name

      menu.choice "Continue (process the exception normally)" do
        hammertime_original_raise(error)
        true
      end
      menu.choice "Ignore (proceed without raising an exception)" do
        true
      end
      menu.choice "Permit by type (don't ask about future errors of this type)" do
        ::Hammertime.ignored_errors << error.class
        c.say "Added #{error.class} to permitted error types"
        hammertime_original_raise(error)
        true
      end
      menu.choice "Permit by line (don't ask about future errors raised from this point)" do
        ::Hammertime.ignored_lines << backtrace.first
        c.say "Added #{backtrace.first} to permitted error lines"
        hammertime_original_raise(error)
        true
      end
      menu.choice "Backtrace (show the call stack leading up to the error)" do
        backtrace.each do |frame| c.say frame end
        false
      end
      if Hammertime.debug_supported?
        menu.choice "Debug (start a debugger)" do
          debugger
          false
        end
      end
      menu.choice "Console (start a pry session)" do
        yield.pry
        false
      end
    end
    continue = c.choose(&menu_config) until continue
  end
ensure
  ::Hammertime.stopped = false
end

#raise(*args) ⇒ Object



135
136
137
138
# File 'lib/hammertime19.rb', line 135

def raise(*args)
  caller_binding = binding.of_caller(1)    
  hammertime_raise(*args) { caller_binding } 
end