Sha256: e873e83e330ed241040e54a7e0159042754b069e9b0918a876c4b1f2d35ad217

Contents?: true

Size: 752 Bytes

Versions: 2

Compression:

Stored size: 752 Bytes

Contents

module Superstore
  class Scope
    module Batches
      def find_each(options = {})
        find_in_batches(options) do |records|
          records.each { |record| yield record }
        end
      end

      def find_in_batches(options = {})
        batch_size = options.delete(:batch_size) || 1000
        start_key = nil

        scope = limit(batch_size + 1)
        records = scope.to_a

        while records.any?
          if records.size > batch_size
            next_record = records.pop
          else
             next_record = nil
          end

          yield records
          break if next_record.nil?

          records = scope.where("#{adapter.primary_key_column} >= '#{next_record.id}'").to_a
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
superstore-1.0.2 lib/superstore/scope/batches.rb
superstore-1.0.0 lib/superstore/scope/batches.rb