Sha256: 6e851182477eda4a7185c7d663e4d12b446ea029981f393f58319bab2790aa39

Contents?: true

Size: 1.27 KB

Versions: 76

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

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

76 entries across 76 versions & 1 rubygems

Version Path
lhs-26.2.0 lib/lhs/concerns/record/batch.rb
lhs-26.1.0 lib/lhs/concerns/record/batch.rb
lhs-26.0.1 lib/lhs/concerns/record/batch.rb
lhs-26.0.0 lib/lhs/concerns/record/batch.rb
lhs-25.2.0 lib/lhs/concerns/record/batch.rb
lhs-25.1.0 lib/lhs/concerns/record/batch.rb
lhs-25.0.4 lib/lhs/concerns/record/batch.rb
lhs-25.0.3 lib/lhs/concerns/record/batch.rb
lhs-25.0.2 lib/lhs/concerns/record/batch.rb
lhs-25.0.1 lib/lhs/concerns/record/batch.rb
lhs-25.0.0 lib/lhs/concerns/record/batch.rb
lhs-24.1.2 lib/lhs/concerns/record/batch.rb
lhs-24.1.1 lib/lhs/concerns/record/batch.rb
lhs-24.1.0 lib/lhs/concerns/record/batch.rb
lhs-24.1.0.pre.2 lib/lhs/concerns/record/batch.rb
lhs-24.1.0.pre.1 lib/lhs/concerns/record/batch.rb
lhs-24.0.0 lib/lhs/concerns/record/batch.rb
lhs-23.0.2 lib/lhs/concerns/record/batch.rb
lhs-23.0.1 lib/lhs/concerns/record/batch.rb
lhs-23.0.0 lib/lhs/concerns/record/batch.rb