Sha256: a7b289ce25a92923df899c18fc8f6788affc00056c022fa1914bce4bf03feaae
Contents?: true
Size: 1.21 KB
Versions: 34
Compression:
Stored size: 1.21 KB
Contents
module Cuboid module Support::Buffer # A buffer implementation which flushes itself when it gets full or a number # of push attempts is reached between flushes. # # @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com> class AutoFlush < Base attr_reader :max_pushes # @param [Integer] max_size # Maximum buffer size -- a flush will be triggered when that limit is # reached. # @param [Integer] max_pushes # Maximum number of pushes between flushes. # @param [#<<, #|, #clear, #size, #empty?] type # Internal storage class to use. def initialize( max_size = nil, max_pushes = nil, type = Array ) super( max_size, type ) @max_pushes = max_pushes @pushes = 0 end def <<( *args ) super( *args ) ensure handle_push end def batch_push( *args ) super( *args ) ensure handle_push end def flush super ensure @pushes = 0 end private def handle_push @pushes += 1 flush if flush? end def flush? !!(full? || push_limit_reached?) end def push_limit_reached? max_pushes && @pushes >= max_pushes end end end end
Version data entries
34 entries across 34 versions & 1 rubygems