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

Version Path
canvas_sync-0.17.16 lib/canvas_sync/batch_processor.rb
canvas_sync-0.17.15 lib/canvas_sync/batch_processor.rb
canvas_sync-0.17.14 lib/canvas_sync/batch_processor.rb
canvas_sync-0.17.13 lib/canvas_sync/batch_processor.rb
canvas_sync-0.17.12 lib/canvas_sync/batch_processor.rb
canvas_sync-0.17.10.beta2 lib/canvas_sync/batch_processor.rb
canvas_sync-0.17.10.beta1 lib/canvas_sync/batch_processor.rb