Sha256: 414edc73bd1cebce14d88c5847130ad4e74979308414e71afd93cac4be143459

Contents?: true

Size: 1.04 KB

Versions: 15

Compression:

Stored size: 1.04 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 |data|
          data.each do |record|
            item = LHS::Item.new(LHS::Data.new(record, data, self))
            yield new(LHS::Data.new(item, 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] || 100
        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

15 entries across 15 versions & 1 rubygems

Version Path
lhs-3.3.6 lib/lhs/concerns/record/batch.rb
lhs-3.3.5 lib/lhs/concerns/record/batch.rb
lhs-3.3.3 lib/lhs/concerns/record/batch.rb
lhs-3.3.2 lib/lhs/concerns/record/batch.rb
lhs-3.3.1 lib/lhs/concerns/record/batch.rb
lhs-3.3.0 lib/lhs/concerns/record/batch.rb
lhs-3.2.0 lib/lhs/concerns/record/batch.rb
lhs-3.1.3 lib/lhs/concerns/record/batch.rb
lhs-3.1.1 lib/lhs/concerns/record/batch.rb
lhs-3.1.0 lib/lhs/concerns/record/batch.rb
lhs-3.0.5 lib/lhs/concerns/record/batch.rb
lhs-3.0.4 lib/lhs/concerns/record/batch.rb
lhs-3.0.3 lib/lhs/concerns/record/batch.rb
lhs-3.0.2 lib/lhs/concerns/record/batch.rb
lhs-3.0.1 lib/lhs/concerns/record/batch.rb