Class: StructuredWarnings::Base

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/structured_warnings/base.rb

Overview

Descendents of class StructuredWarnings::Base are used to raise structured warnings. They enable programmers to explicitly supress certain kinds of warnings and provide additional information in contrast to the plain warn method. They implement an Exception-like interface and carry information about the warning -- its type (the warning's class name), an optional descriptive string, and optional traceback information. Programs may subclass StructuredWarnings::Base to add additional information.

Large portions of this class's documentation are taken from the Exception RDoc.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

active?, disable, enable

Constructor Details

#initialize(message = nil) ⇒ Base

Construct a new StructuredWarning::Base object, optionally passing in a message.



14
15
16
17
# File 'lib/structured_warnings/base.rb', line 14

def initialize(message = nil)
  @message = message
  @backtrace = caller(1)
end

Instance Method Details

#backtraceObject

call-seq:

warning.backtrace    => array

Returns any backtrace associated with the warning. The backtrace is an array of strings, each containing either filename:lineNo: in `method''' or filename:lineNo.''



26
27
28
# File 'lib/structured_warnings/base.rb', line 26

def backtrace
  @backtrace
end

#inspectObject

Return this warning's class name and message



45
46
47
# File 'lib/structured_warnings/base.rb', line 45

def inspect
  "#<#{self.class}: #{self}>"
end

#set_backtrace(backtrace) ⇒ Object

Sets the backtrace information associated with warning. The argument must be an array of String objects in the format described in Exception#backtrace.



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

def set_backtrace(backtrace)
  @backtrace = backtrace
end

#to_sObject Also known as: to_str, message

Returns warning's message (or the name of the warning if no message is set).



38
39
40
# File 'lib/structured_warnings/base.rb', line 38

def to_s
  @message || self.class.name
end