Class: RailsErrorDashboard::Services::BreadcrumbCollector::RingBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/breadcrumb_collector.rb

Overview

Fixed-size ring buffer — O(1) append, wraps around when full

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ RingBuffer

Returns a new instance of RingBuffer.



40
41
42
43
44
45
# File 'lib/rails_error_dashboard/services/breadcrumb_collector.rb', line 40

def initialize(max_size)
  @max_size = max_size
  @buffer = Array.new(max_size)
  @write_pos = 0
  @count = 0
end

Instance Method Details

#add(entry) ⇒ Object



47
48
49
50
51
# File 'lib/rails_error_dashboard/services/breadcrumb_collector.rb', line 47

def add(entry)
  @buffer[@write_pos] = entry
  @write_pos = (@write_pos + 1) % @max_size
  @count += 1 if @count < @max_size
end

#clearObject



64
65
66
67
68
# File 'lib/rails_error_dashboard/services/breadcrumb_collector.rb', line 64

def clear
  @buffer = Array.new(@max_size)
  @write_pos = 0
  @count = 0
end

#to_aObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/rails_error_dashboard/services/breadcrumb_collector.rb', line 53

def to_a
  return [] if @count == 0

  if @count < @max_size
    @buffer[0...@count]
  else
    # Buffer has wrapped — read from write_pos to end, then start to write_pos
    @buffer[@write_pos...@max_size] + @buffer[0...@write_pos]
  end
end