Sha256: cfc7b5e7c82a377b57212002b7ae4dac78a50afbe65c0e90e7dc2e1dc160eecb

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

require 'active_support'

class LHS::Record

  module Batch
    extend ActiveSupport::Concern

    module ClassMethods
      # Process single entries fetched in batches
      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

      # Process batches of entries
      def find_in_batches(options = {})
        fail '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: batch_size, offset: start))
          batch_size = data._raw[:limit]
          left = data._raw[:total].to_i - data._raw[:offset].to_i - data._raw[:limit].to_i
          yield new(data)
          break if left <= 0
          start += batch_size
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lhs-5.6.5 lib/lhs/concerns/record/batch.rb
lhs-5.6.4 lib/lhs/concerns/record/batch.rb
lhs-5.6.3 lib/lhs/concerns/record/batch.rb
lhs-5.6.2 lib/lhs/concerns/record/batch.rb
lhs-5.6.1 lib/lhs/concerns/record/batch.rb
lhs-5.6.0 lib/lhs/concerns/record/batch.rb
lhs-5.5.0 lib/lhs/concerns/record/batch.rb
lhs-5.4.2 lib/lhs/concerns/record/batch.rb
lhs-5.4.1 lib/lhs/concerns/record/batch.rb
lhs-5.4.0 lib/lhs/concerns/record/batch.rb