Sha256: ea06371024a8af88aeb4956e164373d697d3de4000047bb188e88100d97b3036

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

require 'active_support'

class LHS::Service

  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 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 data
          break if left <= 0
          start += batch_size
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lhs-2.2.2 lib/lhs/concerns/service/batch.rb
lhs-2.2.1 lib/lhs/concerns/service/batch.rb
lhs-2.2.0 lib/lhs/concerns/service/batch.rb
lhs-2.1.1 lib/lhs/concerns/service/batch.rb
lhs-2.1.0 lib/lhs/concerns/service/batch.rb