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.



23
24
25
26
27
28
# File 'lib/rails_error_dashboard/services/breadcrumb_collector.rb', line 23

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



30
31
32
33
34
# File 'lib/rails_error_dashboard/services/breadcrumb_collector.rb', line 30

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

#clearObject



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

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

#to_aObject



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

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