Module: FrOData::Query::InBatches

Included in:
FrOData::Query
Defined in:
lib/frodata/query/in_batches.rb

Constant Summary collapse

DEFAULT_BATCH_SIZE =
10

Instance Method Summary collapse

Instance Method Details

#in_batches(of: DEFAULT_BATCH_SIZE, &block) ⇒ Enumerator

Process results in batches.

When a block is given, yields ‘FrOData::Query::Result` objects of specified batch size to the block.

service['Products'].query.in_batches(of: 10) do |batch|
  batch.count # batch size (10 except for last batch)
  batch.is_a? FrOData::Query::Result # true
end

Returns an Enumerator to process results individually.

service['Products'].query.in_batches.each do |entity|
  entity.is_a? FrOData::Entity # true
end

Parameters:

  • of: (int) (defaults to: DEFAULT_BATCH_SIZE)

    batch size

Returns:

  • (Enumerator)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/frodata/query/in_batches.rb', line 24

def in_batches(of: DEFAULT_BATCH_SIZE, &block)
  per_page = of

  if block_given?
    each_batch(of, &block)
  else
    Enumerator.new do |result|
      each_batch(of) do |batch|
        batch.each { |entity| result << entity }
      end
    end
  end
end