Sha256: 445fd17ff5c3d0033857053db9acd92615d20c86f9691710afcbd560b9ee9a7b
Contents?: true
Size: 832 Bytes
Versions: 7
Compression:
Stored size: 832 Bytes
Contents
module CanvasSync # An array that "processes" after so many items are added. # # Example Usage: # batches = BatchProcessor.new(of: 1000) do |batch| # # Process the batch somehow # end # enumerator_of_some_kind.each { |item| batches << item } # batches.flush class BatchProcessor attr_reader :batch_size def initialize(of: 1000, &blk) @batch_size = of @block = blk @current_batch = [] end def <<(item) @current_batch << item process_batch if @current_batch.count >= batch_size end def add_all(items) items.each do |i| self << i end end def flush process_batch if @current_batch.present? end protected def process_batch @block.call(@current_batch) @current_batch = [] end end end
Version data entries
7 entries across 7 versions & 1 rubygems