Sha256: 9d178a2403f6431360d750b5404eb1aa36be216ad73c69693a493787bb3fce8b
Contents?: true
Size: 830 Bytes
Versions: 21
Compression:
Stored size: 830 Bytes
Contents
module PandaPal # 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
21 entries across 21 versions & 1 rubygems