Sha256: 19f78e31daa7472f42682f87519c8266db91a3fc47d347a2b1e9370478502252

Contents?: true

Size: 1.03 KB

Versions: 14

Compression:

Stored size: 1.03 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 = {})
        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

14 entries across 14 versions & 1 rubygems

Version Path
lhs-5.3.0 lib/lhs/concerns/record/batch.rb
lhs-5.2.0 lib/lhs/concerns/record/batch.rb
lhs-5.1.0 lib/lhs/concerns/record/batch.rb
lhs-5.0.4 lib/lhs/concerns/record/batch.rb
lhs-5.0.3 lib/lhs/concerns/record/batch.rb
lhs-5.0.1 lib/lhs/concerns/record/batch.rb
lhs-5.0.0 lib/lhs/concerns/record/batch.rb
lhs-4.2.1 lib/lhs/concerns/record/batch.rb
lhs-4.2.0 lib/lhs/concerns/record/batch.rb
lhs-4.1.0 lib/lhs/concerns/record/batch.rb
lhs-4.0.0 lib/lhs/concerns/record/batch.rb
lhs-3.4.2 lib/lhs/concerns/record/batch.rb
lhs-3.4.1 lib/lhs/concerns/record/batch.rb
lhs-3.4.0 lib/lhs/concerns/record/batch.rb