Class: Clacky::Utils::LimitStack
- Inherits:
-
Object
- Object
- Clacky::Utils::LimitStack
- Defined in:
- lib/clacky/utils/limit_stack.rb
Overview
Auto-rolling fixed-size array. Automatically discards oldest elements when the line-count limit is exceeded.
Optional limits (all default to nil = no limit):
max_line_chars – truncate each individual line to this many characters on push
max_chars – once the total accepted chars reach this threshold, further
pushes are silently dropped (sets #truncated? = true)
These extra limits are fully opt-in; existing callers that only pass max_size are completely unaffected.
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all elements.
-
#each(&block) ⇒ Object
Iterate over elements.
-
#empty? ⇒ Boolean
Check if empty.
-
#initialize(max_size: 5000, max_line_chars: nil, max_chars: nil) ⇒ LimitStack
constructor
A new instance of LimitStack.
-
#last(n = nil) ⇒ Object
Get last N elements.
-
#pop ⇒ Object
Remove and return the last element.
-
#push(*elements) ⇒ Object
(also: #<<)
Add elements (supports single or multiple).
-
#push_lines(text) ⇒ Object
Add multi-line text (split by lines and add).
-
#size ⇒ Object
Current size.
-
#to_a ⇒ Object
Get all elements.
-
#to_s ⇒ Object
Convert to string (for text content).
-
#trim_if_needed ⇒ Object
kept for compatibility (called internally; public so subclasses can override).
-
#truncated? ⇒ Boolean
True if any content was dropped (lines rolled off the front OR chars budget was exceeded OR a line was truncated).
Constructor Details
#initialize(max_size: 5000, max_line_chars: nil, max_chars: nil) ⇒ LimitStack
Returns a new instance of LimitStack.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/clacky/utils/limit_stack.rb', line 18 def initialize(max_size: 5000, max_line_chars: nil, max_chars: nil) @max_size = max_size @max_line_chars = max_line_chars @max_chars = max_chars @items = [] @total_chars = 0 # chars currently stored in @items @truncated = false @chars_full = false # latched true once max_chars is reached end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
16 17 18 |
# File 'lib/clacky/utils/limit_stack.rb', line 16 def items @items end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
16 17 18 |
# File 'lib/clacky/utils/limit_stack.rb', line 16 def max_size @max_size end |
Instance Method Details
#clear ⇒ Object
Clear all elements
86 87 88 89 90 91 92 |
# File 'lib/clacky/utils/limit_stack.rb', line 86 def clear @items.clear @total_chars = 0 @truncated = false @chars_full = false self end |
#each(&block) ⇒ Object
Iterate over elements
95 96 97 |
# File 'lib/clacky/utils/limit_stack.rb', line 95 def each(&block) @items.each(&block) end |
#empty? ⇒ Boolean
Check if empty
81 82 83 |
# File 'lib/clacky/utils/limit_stack.rb', line 81 def empty? @items.empty? end |
#last(n = nil) ⇒ Object
Get last N elements
61 62 63 |
# File 'lib/clacky/utils/limit_stack.rb', line 61 def last(n = nil) n ? @items.last(n) : @items.last end |
#pop ⇒ Object
Remove and return the last element
54 55 56 57 58 |
# File 'lib/clacky/utils/limit_stack.rb', line 54 def pop item = @items.pop @total_chars -= item.length if item.is_a?(String) item end |
#push(*elements) ⇒ Object Also known as: <<
Add elements (supports single or multiple)
36 37 38 39 40 41 |
# File 'lib/clacky/utils/limit_stack.rb', line 36 def push(*elements) elements.each do |element| _push_one(element) end self end |
#push_lines(text) ⇒ Object
Add multi-line text (split by lines and add)
45 46 47 48 49 50 51 |
# File 'lib/clacky/utils/limit_stack.rb', line 45 def push_lines(text) return self if text.nil? || text.empty? lines = text.is_a?(Array) ? text : text.lines lines.each { |line| _push_one(line) } self end |
#size ⇒ Object
Current size
76 77 78 |
# File 'lib/clacky/utils/limit_stack.rb', line 76 def size @items.size end |
#to_a ⇒ Object
Get all elements
66 67 68 |
# File 'lib/clacky/utils/limit_stack.rb', line 66 def to_a @items.dup end |
#to_s ⇒ Object
Convert to string (for text content)
71 72 73 |
# File 'lib/clacky/utils/limit_stack.rb', line 71 def to_s @items.join end |
#trim_if_needed ⇒ Object
kept for compatibility (called internally; public so subclasses can override)
100 101 102 103 104 105 106 |
# File 'lib/clacky/utils/limit_stack.rb', line 100 def trim_if_needed while @items.size > @max_size removed = @items.shift @total_chars -= removed.length if removed.is_a?(String) @truncated = true end end |
#truncated? ⇒ Boolean
True if any content was dropped (lines rolled off the front OR chars budget was exceeded OR a line was truncated).
31 32 33 |
# File 'lib/clacky/utils/limit_stack.rb', line 31 def truncated? @truncated end |