Class: Warnings::Warning

Inherits:
Object
  • Object
show all
Defined in:
lib/warnings/warning.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, backtrace) ⇒ Warning

Creates a new warning.

Parameters:

  • message (String)

    The warning message.

  • backtrace (Array<String>)

    The backtrace of the warning.



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

def initialize(message,backtrace)
  @message = 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

#backtraceArray<String> (readonly)

The backtrace to the warning

Returns:

  • (Array<String>)

    The raw backtrace.



12
13
14
# File 'lib/warnings/warning.rb', line 12

def backtrace
  @backtrace
end

#messageObject (readonly)

The warning message



5
6
7
# File 'lib/warnings/warning.rb', line 5

def message
  @message
end

#source_locationArray<path, line> (readonly)

The source location of the warning.

Returns:

  • (Array<path, line>)

    The path and line number where the warning originated.



19
20
21
# File 'lib/warnings/warning.rb', line 19

def source_location
  @source_location
end

#source_methodSymbol (readonly)

The source method of the warning.

Returns:

  • (Symbol)

    The method name.



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.

Parameters:

  • other (Warning)

    The other warning to compare against.

Returns:

  • (Boolean)

    Specifies whether the two warnings represent the same message.



89
90
91
92
93
# File 'lib/warnings/warning.rb', line 89

def ==(other)
  (@message == other.message) &&
  (@source_location == other.source_location) &&
  (@source_method == other.source_method)
end

#===(other) ⇒ Boolean

Compares the warning to another warning.

Parameters:

  • other (Warning)

    The other warning to compare against.

Returns:

  • (Boolean)

    Specifies whether the two warnings represent the same message.



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.

Parameters:

  • pattern (Regexp)

    The pattern to match against.

Returns:

  • (Integer, nil)

    The index of the pattern match within the warning message.



76
77
78
# File 'lib/warnings/warning.rb', line 76

def =~(pattern)
  @message =~ message
end

#inspectString

Inspects the warning.

Returns:



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
Note:

Will include ANSI color codes, only if STDOUT is a TTY Terminal.

Formats a warning.

Returns:

  • (String)

    The printable 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_fileString

The source file of the warning.

Returns:

  • (String)

    The path of the file.



53
54
55
# File 'lib/warnings/warning.rb', line 53

def source_file
  @source_location[0]
end

#source_lineInteger

The source line of the warning.

Returns:

  • (Integer)

    The line-number of the warning.



63
64
65
# File 'lib/warnings/warning.rb', line 63

def source_line
  @source_location[1]
end

#to_sString

Converts the warning to a String.

Returns:

  • (String)

    The warning message.



121
122
123
# File 'lib/warnings/warning.rb', line 121

def to_s
  @message.to_s
end