Sha256: 20e940f14bc1695fa8db5a2d770300bdcf2a2a69ac3329cb359298f01227eab9

Contents?: true

Size: 821 Bytes

Versions: 65

Compression:

Stored size: 821 Bytes

Contents

module Honeybadger
  module Breadcrumbs
    class RingBuffer
      # Simple ring buffer implementation that keeps item count constrained using
      # a rolling window. Items from the front of the buffer are dropped as more
      # are pushed on the end of the stack.
      include Enumerable

      attr_reader :buffer

      def initialize(buffer_size = 40)
        @buffer_size = buffer_size
        clear!
      end

      def add!(item)
        @buffer << item
        @ct += 1
        @buffer.shift(1) if @ct > @buffer_size
      end

      def clear!
        @buffer = []
        @ct = 0
      end

      def to_a
        @buffer
      end

      def each(&blk)
        @buffer.each(&blk)
      end

      def previous
        @buffer.last
      end

      def drop
        @buffer.pop
      end
    end
  end
end

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
honeybadger-4.5.1 lib/honeybadger/breadcrumbs/ring_buffer.rb
honeybadger-4.5.0 lib/honeybadger/breadcrumbs/ring_buffer.rb
honeybadger-4.4.2 lib/honeybadger/breadcrumbs/ring_buffer.rb
honeybadger-4.4.1 lib/honeybadger/breadcrumbs/ring_buffer.rb
honeybadger-4.4.0 lib/honeybadger/breadcrumbs/ring_buffer.rb