Sha256: 62c9299a9a0c20e2f0234d4d4ba9dc507106b361b8e2aa9f07d1b445fd0e7b94

Contents?: true

Size: 1.09 KB

Versions: 24

Compression:

Stored size: 1.09 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?
        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
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
lhs-7.4.1 lib/lhs/concerns/record/batch.rb
lhs-7.4.0 lib/lhs/concerns/record/batch.rb
lhs-7.3.0 lib/lhs/concerns/record/batch.rb
lhs-7.2.5 lib/lhs/concerns/record/batch.rb
lhs-7.2.4 lib/lhs/concerns/record/batch.rb
lhs-7.2.3 lib/lhs/concerns/record/batch.rb
lhs-7.2.2 lib/lhs/concerns/record/batch.rb
lhs-7.2.1 lib/lhs/concerns/record/batch.rb
lhs-7.2.0 lib/lhs/concerns/record/batch.rb
lhs-7.1.0 lib/lhs/concerns/record/batch.rb
lhs-7.0.3 lib/lhs/concerns/record/batch.rb
lhs-7.0.2 lib/lhs/concerns/record/batch.rb
lhs-7.0.1 lib/lhs/concerns/record/batch.rb
lhs-7.0.0 lib/lhs/concerns/record/batch.rb
lhs-6.8.2 lib/lhs/concerns/record/batch.rb
lhs-6.8.1 lib/lhs/concerns/record/batch.rb
lhs-6.8.0 lib/lhs/concerns/record/batch.rb
lhs-6.7.2 lib/lhs/concerns/record/batch.rb
lhs-6.7.1 lib/lhs/concerns/record/batch.rb
lhs-6.7.0 lib/lhs/concerns/record/batch.rb