Skip to content

Commit

Permalink
Breadcrumbs/Circular buffer: Added yard docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec committed Dec 6, 2018
1 parent 1b39175 commit 2e3e125
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/bugsnag/utility/circular_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,57 @@ module Bugsnag::Utility
class CircularBuffer
include Enumerable

# @return [Numeric] the current maximum allowable number of items
attr_reader :max_items

##
# Creates a circular buffer
#
# @api private
#
# @param max_items [Integer] the initial maximum number of items
def initialize(max_items = 25)
@max_items = max_items
@buffer = []
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 [Any] the item to add to the buffer
# @return [self] returns itself to allow method chaining
def <<(item)
@buffer << item
trim_buffer
self
end

##
# Iterates over the buffer
#
# @yield [item] sequentially gives stored items to the block
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
def max_items=(new_max_items)
@max_items = new_max_items
trim_buffer
end

private

##
# Trims the buffer down to the current maximum allowable item number
def trim_buffer
trim_size = @buffer.size - @max_items
trim_size = 0 if trim_size < 0
Expand Down

0 comments on commit 2e3e125

Please sign in to comment.