Class: Warnings::Warning
- Inherits:
-
Object
- Object
- Warnings::Warning
- Defined in:
- lib/warnings/warning.rb
Instance Attribute Summary collapse
-
#backtrace ⇒ Array<String>
readonly
The backtrace to the warning.
-
#message ⇒ Object
readonly
The warning message.
-
#source_location ⇒ Array<path, line>
readonly
The source location of the warning.
-
#source_method ⇒ Symbol
readonly
The source method of the warning.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the warning to another warning.
-
#===(other) ⇒ Boolean
Compares the warning to another warning.
-
#=~(pattern) ⇒ Integer?
Compares the warning message to a pattern.
-
#initialize(message, backtrace) ⇒ Warning
constructor
Creates a new warning.
-
#inspect ⇒ String
Inspects the warning.
-
#print ⇒ String
Formats a warning.
-
#source_file ⇒ String
The source file of the warning.
-
#source_line ⇒ Integer
The source line of the warning.
-
#to_s ⇒ String
Converts the warning to a String.
Constructor Details
#initialize(message, backtrace) ⇒ Warning
Creates a new warning.
37 38 39 40 41 42 43 44 45 |
# File 'lib/warnings/warning.rb', line 37 def initialize(,backtrace) @message = @backtrace = backtrace file, line, context = @backtrace.first.split(':',3) @source_location = [file, line.to_i] @source_method = context[5..-2] if context end |
Instance Attribute Details
#backtrace ⇒ Array<String> (readonly)
The backtrace to the warning
12 13 14 |
# File 'lib/warnings/warning.rb', line 12 def backtrace @backtrace end |
#message ⇒ Object (readonly)
The warning message
5 6 7 |
# File 'lib/warnings/warning.rb', line 5 def @message end |
#source_location ⇒ Array<path, line> (readonly)
The source location of the warning.
19 20 21 |
# File 'lib/warnings/warning.rb', line 19 def source_location @source_location end |
#source_method ⇒ Symbol (readonly)
The source method of the warning.
26 27 28 |
# File 'lib/warnings/warning.rb', line 26 def source_method @source_method end |
Instance Method Details
#==(other) ⇒ Boolean
Compares the warning to another warning.
89 90 91 92 93 |
# File 'lib/warnings/warning.rb', line 89 def ==(other) (@message == other.) && (@source_location == other.source_location) && (@source_method == other.source_method) end |
#===(other) ⇒ Boolean
Compares the warning to another warning.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/warnings/warning.rb', line 104 def ===(other) case other when Warning self == other when Regexp, String !(@message.match(other).nil?) else false end end |
#=~(pattern) ⇒ Integer?
Compares the warning message to a pattern.
76 77 78 |
# File 'lib/warnings/warning.rb', line 76 def =~(pattern) @message =~ end |
#inspect ⇒ String
Inspects the warning.
156 157 158 159 160 161 162 |
# File 'lib/warnings/warning.rb', line 156 def inspect if @source_method "#{source_file}:#{source_line} (#{@source_method}): #{@message}" else "#{source_file}:#{source_line}: #{@message}" end end |
#print ⇒ String
Will include ANSI color codes, only if STDOUT
is a TTY Terminal.
Formats a warning.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/warnings/warning.rb', line 134 def print trace = unless $DEBUG @backtrace[0,5] else @backtrace end if $stderr.tty? $stderr.puts " \e[33m#{@message}:\e[0m" trace.each { |line| $stderr.puts "\t\e[2m#{line}\e[0m" } else $stderr.puts " #{@message}:" trace.each { |line| $stderr.puts "\t#{line}" } end end |
#source_file ⇒ String
The source file of the warning.
53 54 55 |
# File 'lib/warnings/warning.rb', line 53 def source_file @source_location[0] end |
#source_line ⇒ Integer
The source line of the warning.
63 64 65 |
# File 'lib/warnings/warning.rb', line 63 def source_line @source_location[1] end |
#to_s ⇒ String
Converts the warning to a String.
121 122 123 |
# File 'lib/warnings/warning.rb', line 121 def to_s @message.to_s end |