Module: LHS::Record::Batch::ClassMethods

Defined in:
lib/lhs/concerns/record/batch.rb

Instance Method Summary collapse

Instance Method Details

#find_each(options = {}) ⇒ Object

Process single entries fetched in batches



10
11
12
13
14
15
16
17
# File 'lib/lhs/concerns/record/batch.rb', line 10

def find_each(options = {})
  find_in_batches(options) do |records|
    records.each do |record|
      item = LHS::Item.new(record)
      yield new(LHS::Data.new(item, records._data, self))
    end
  end
end

#find_in_batches(options = {}) ⇒ Object

Process batches of entries



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lhs/concerns/record/batch.rb', line 20

def find_in_batches(options = {})
  raise 'No block given' unless block_given?
  start = options[:start] || 1
  batch_size = options[:batch_size] || LHS::Pagination::DEFAULT_LIMIT
  params = options[:params] || {}
  loop do # as suggested by Matz
    data = request(params: params.merge(limit_key => batch_size, pagination_key => start))
    batch_size = data._raw[limit_key]
    left = data._raw[total_key].to_i - data._raw[pagination_key].to_i - data._raw[limit_key].to_i
    yield new(data)
    break if left <= 0
    start += batch_size
  end
end