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
@param max_items
[Integer] the initial maximum number of items
# File lib/bugsnag/utility/circular_buffer.rb, line 14 def initialize(max_items = 25) @max_items = max_items @buffer = [] end
Public Instance Methods
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
# File lib/bugsnag/utility/circular_buffer.rb, line 26 def <<(item) @buffer << item trim_buffer self end
Iterates over the buffer
@yield [Object] sequentially gives stored items to the block
# File lib/bugsnag/utility/circular_buffer.rb, line 36 def each(&block) @buffer.each(&block) 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
# File lib/bugsnag/utility/circular_buffer.rb, line 47 def max_items=(new_max_items) @max_items = new_max_items trim_buffer end
Private Instance Methods
Trims the buffer down to the current maximum allowable item number
# 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