Module: Superstore::Batches

Included in:
Scope
Defined in:
lib/superstore/scope/batches.rb

Instance Method Summary collapse

Instance Method Details

#find_each(options = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/superstore/scope/batches.rb', line 3

def find_each(options = {})
  batch_size = options[:batch_size] || 1000

  klass.adapter.scroll(self, batch_size) do |key, attributes|
    yield klass.instantiate(key, attributes)
  end
end

#find_in_batches(options = {}) {|batch| ... } ⇒ Object

Yields:

  • (batch)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/superstore/scope/batches.rb', line 11

def find_in_batches(options = {})
  batch_size = options[:batch_size] || 1000
  batch = []

  find_each(options) do |record|
    batch << record

    if batch.size == batch_size
      yield batch
      batch = []
    end
  end

  yield(batch) if batch.any?
end