class Hatchet::NestedDiagnosticContext

Public: Class that manages the nested diagnostic context for a thread.

All access to this class is performed through internal classes.

Attributes

context[R]

Internal: Gets the current context stack.

Public Class Methods

current() click to toggle source

Internal: Gets the NestedDiagnosticContext for the current thread, lazily initializing it as necessary.

# File lib/hatchet/nested_diagnostic_context.rb, line 16
def self.current
  Thread.current[:hatchet_ndc] ||= NestedDiagnosticContext.new
end
new() click to toggle source

Internal: Creates a new instance of the class.

# File lib/hatchet/nested_diagnostic_context.rb, line 22
def initialize
  clear!
end

Public Instance Methods

clear!() click to toggle source

Public: Clears all messages from the context stack.

Intend for use when the current thread is, or may, be reused in the future and the accumlated context is no longer wanted.

Returns nothing.

# File lib/hatchet/nested_diagnostic_context.rb, line 77
def clear!
  @context = ContextStack.new
  nil
end
pop(n = nil) click to toggle source

Public: Removes one or more message from the context stack.

n - The number of messages to remove from the context stack (default:

nil). If no number is provided then one message will be removed.

Returns the message or messages removed from the context stack. If n was not specified it will return a single message, otherwise it will return an Array of up to n messages.

# File lib/hatchet/nested_diagnostic_context.rb, line 45
def pop(n = nil)
  if n
    @context.pop(n)
  else
    @context.pop
  end
end
push(*values) click to toggle source

Public: Adds one or more messages onto the context stack.

values - One or more messages to add to the context stack.

Returns nothing.

# File lib/hatchet/nested_diagnostic_context.rb, line 32
def push(*values)
  @context.push(*values)
end
scope(*values, &block) click to toggle source

Public: Adds one more or message onto the context stack for the scope of the given block.

values - One or more messages to add to the context stack for the scope of

the given block.

block - The block to execute with the additional messages.

Returns the result of calling the block.

# File lib/hatchet/nested_diagnostic_context.rb, line 62
def scope(*values, &block)
  before = @context.clone
  push(*values)
  block.call
ensure
  @context = before
end