class Bugsnag::Utility::CircularBuffer
A container class with a maximum size, that removes oldest items as required.
@api private
Attributes
@return [Integer] the current maximum allowable number of items
Public Class Methods
Source
# File lib/bugsnag/utility/circular_buffer.rb, line 14 def initialize(max_items = 25) @max_items = max_items @buffer = [] end
@param max_items
[Integer] the initial maximum number of items
Public Instance Methods
Source
# File lib/bugsnag/utility/circular_buffer.rb, line 26 def <<(item) @buffer << item trim_buffer self end
Adds an item to the circular buffer
If this causes the buffer to exceed its maximum items, the oldest item will be removed
@param item [Object] the item to add to the buffer @return [self] returns itself to allow method chaining
Source
# File lib/bugsnag/utility/circular_buffer.rb, line 36 def each(&block) @buffer.each(&block) end
Iterates over the buffer
@yield [Object] sequentially gives stored items to the block
Source
# File lib/bugsnag/utility/circular_buffer.rb, line 47 def max_items=(new_max_items) @max_items = new_max_items trim_buffer end
Sets the maximum allowable number of items
If the current number of items exceeds the new maximum, oldest items will be removed until this is no longer the case
@param new_max_items [Integer] the new allowed item maximum
Private Instance Methods
Source
# File lib/bugsnag/utility/circular_buffer.rb, line 56 def trim_buffer trim_size = @buffer.size - @max_items trim_size = 0 if trim_size < 0 @buffer.shift(trim_size) end
Trims the buffer down to the current maximum allowable item number