Class: Hatchet::NestedDiagnosticContext

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/nested_diagnostic_context.rb

Overview

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

All access to this class is performed through internal classes.

Defined Under Namespace

Classes: ContextStack

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNestedDiagnosticContext

Internal: Creates a new instance of the class.



22
23
24
# File 'lib/hatchet/nested_diagnostic_context.rb', line 22

def initialize
  clear!
end

Instance Attribute Details

#contextObject (readonly)

Internal: Gets the current context stack.



11
12
13
# File 'lib/hatchet/nested_diagnostic_context.rb', line 11

def context
  @context
end

Class Method Details

.currentObject

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



16
17
18
# File 'lib/hatchet/nested_diagnostic_context.rb', line 16

def self.current
  Thread.current[:hatchet_ndc] ||= NestedDiagnosticContext.new
end

Instance Method Details

#clear!Object

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.



77
78
79
80
# File 'lib/hatchet/nested_diagnostic_context.rb', line 77

def clear!
  @context = ContextStack.new
  nil
end

#pop(n = nil) ⇒ Object

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.



45
46
47
48
49
50
51
# 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) ⇒ Object

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

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

Returns nothing.



32
33
34
# File 'lib/hatchet/nested_diagnostic_context.rb', line 32

def push(*values)
  @context.push(*values)
end

#scope(*values, &block) ⇒ Object

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.



62
63
64
65
66
67
68
# File 'lib/hatchet/nested_diagnostic_context.rb', line 62

def scope(*values, &block)
  before = @context.clone
  push(*values)
  block.call
ensure
  @context = before
end