Class: Hatchet::Message
- Inherits:
-
Object
- Object
- Hatchet::Message
- Defined in:
- lib/hatchet/message.rb
Overview
Public: Class for wrapping message strings and blocks in a way that means they can be treated identically.
If an error is associated with the message this will be available via the #error attribute.
The nested diagnostic context of the message will be availble via the #ndc attribute.
Blocks will be lazily evaluated once for all appenders when required.
Defined Under Namespace
Classes: ErrorDecorator
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Public: Gets the error associated with this message, if given.
-
#ndc ⇒ Object
readonly
Public: Gets the nested diagnostic context values.
Instance Method Summary collapse
-
#evaluated_message ⇒ Object
Public: Returns the evaluated message.
-
#initialize(args = {}, error = nil, &block) ⇒ Message
constructor
Public: Creates a new message.
-
#to_s ⇒ Object
Public: Returns the String representation of the message.
Constructor Details
#initialize(args = {}, error = nil, &block) ⇒ Message
Public: Creates a new message.
args - The Hash used to provide context for the message (default: {}):
:ndc - An Array of nested diagnostic context values
(default: []).
:message - An already evaluated message, usually a String
(default: nil).
:error - An error that is associated with the message
(default: nil).
block - An optional block which will provide a message when invoked.
Examples
Message.new(ndc: [], message: "Evaluated message", error: e)
Message.new(ndc: %w{Foo Bar}) { "Lazily evaluated message" }
The signature of the constructor was originally:
message - An already evaluated message, usually a String (default: nil). error - An error that is associated with the message (default: nil). block - An optional block which will provide a message when invoked.
This format is also supported for compatibility to version 0.1.0 and below and will be deprecated in the future.
Examples
Message.new("Evaluated message", e)
Message.new { "Lazily evaluated message" }
One of message or block must be provided. If both are provided then the block is preferred as it is assumed to provide more detail.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/hatchet/message.rb', line 87 def initialize(args = {}, error = nil, &block) @message = nil if args.kind_of? Hash # If args is a Hash then using new constructor format or no parameters # specified. Either way, use the new format. @ndc = args[:ndc] || [] @error = args[:error] @message = args[:message] unless block else # Otherwise assume the old format and coerce args accordingly. @ndc = [] @error = error @message = args unless block end @error = ErrorDecorator.new(@error, args[:backtrace_filters]) if @error && args[:backtrace_filters] @block = block end |
Instance Attribute Details
#error ⇒ Object (readonly)
Public: Gets the error associated with this message, if given.
48 49 50 |
# File 'lib/hatchet/message.rb', line 48 def error @error end |
#ndc ⇒ Object (readonly)
Public: Gets the nested diagnostic context values.
52 53 54 |
# File 'lib/hatchet/message.rb', line 52 def ndc @ndc end |
Instance Method Details
#evaluated_message ⇒ Object
Public: Returns the evaluated message.
115 116 117 |
# File 'lib/hatchet/message.rb', line 115 def @evaluated_message ||= (@message || @block.call) end |
#to_s ⇒ Object
Public: Returns the String representation of the message.
109 110 111 |
# File 'lib/hatchet/message.rb', line 109 def to_s .to_s end |