Sha256: a6be3ff0252eb5a0cdb5ae99b6429ec2c9c8b1ec3c103dbf8146361499917f43

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 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 = {})
        raise 'No block given' unless block_given?
        options = options.dup
        start = options.delete(:start) || 1
        batch_size = options.delete(:batch_size) || LHS::Pagination::Base::DEFAULT_LIMIT
        loop do # as suggested by Matz
          options = options.dup
          options[:params] = (options[:params] || {}).merge(limit_key(:parameter) => batch_size, pagination_key(:parameter) => start)
          data = request(options)
          batch_size = data._raw.dig(*limit_key(:body))
          left = data._raw.dig(*total_key).to_i - data._raw.dig(*pagination_key(:body)).to_i - data._raw.dig(*limit_key(:body)).to_i
          yield new(data)
          break if left <= 0
          start += batch_size
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lhs-16.1.5 lib/lhs/concerns/record/batch.rb
lhs-16.1.4 lib/lhs/concerns/record/batch.rb
lhs-16.1.3 lib/lhs/concerns/record/batch.rb
lhs-16.1.2 lib/lhs/concerns/record/batch.rb
lhs-16.1.1 lib/lhs/concerns/record/batch.rb
lhs-16.1.0 lib/lhs/concerns/record/batch.rb