Class: Hatchet::NestedDiagnosticContext::ContextStack
- Inherits:
-
Object
- Object
- Hatchet::NestedDiagnosticContext::ContextStack
- Defined in:
- lib/hatchet/nested_diagnostic_context.rb
Overview
Public: Class for holding the context stack of a NestedDiagnosticContext.
Deliberately intended to have a similar API to Array to make testing easier.
Instance Attribute Summary collapse
-
#stack ⇒ Object
readonly
Internal: Gets the internal stack.
Instance Method Summary collapse
-
#any? ⇒ Boolean
Public: Returns true if the stack contains any messages, otherwise returns false.
-
#clone ⇒ Object
Internal: Returns a clone of the stack.
-
#initialize(stack = []) ⇒ ContextStack
constructor
Internal: Creates a new instance.
-
#join(separator = $,) ⇒ Object
Public: Returns a String created by converting each message of the stack to a String, separated by separator.
-
#pop(n = nil) ⇒ Object
Internal: Removes one or more message from the stack.
-
#push(*values) ⇒ Object
Internal: Pushes the given messages onto the stack.
-
#to_a ⇒ Object
Internal: Returns a copy of the stack as an array.
Constructor Details
#initialize(stack = []) ⇒ ContextStack
Internal: Creates a new instance.
stack - An Array of values to initialize the stack with (default: []).
97 98 99 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 97 def initialize(stack = []) @stack = stack end |
Instance Attribute Details
#stack ⇒ Object (readonly)
Internal: Gets the internal stack.
91 92 93 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 91 def stack @stack end |
Instance Method Details
#any? ⇒ Boolean
Public: Returns true if the stack contains any messages, otherwise returns false.
104 105 106 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 104 def any? @stack.size != 0 end |
#clone ⇒ Object
Internal: Returns a clone of the stack.
110 111 112 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 110 def clone ContextStack.new(@stack.clone) end |
#join(separator = $,) ⇒ Object
Public: Returns a String created by converting each message of the stack to a String, separated by separator.
separator - The String to separate the messages of the stack with
(default: $,).
Returns a String created by converting each message of the stack to a String, separated by separator.
123 124 125 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 123 def join(separator = $,) @stack.join(separator) end |
#pop(n = nil) ⇒ Object
Internal: Removes one or more message from the stack.
n - The number of messages to remove from the cstack (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.
147 148 149 150 151 152 153 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 147 def pop(n = nil) if n @stack.pop(n) else @stack.pop end end |
#push(*values) ⇒ Object
Internal: Pushes the given messages onto the stack.
values - One or more messages to add to the context stack.
Returns nothing.
133 134 135 136 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 133 def push(*values) @stack.push(*values) nil end |
#to_a ⇒ Object
Internal: Returns a copy of the stack as an array.
157 158 159 |
# File 'lib/hatchet/nested_diagnostic_context.rb', line 157 def to_a @stack.clone.to_a end |