Sha256: e908c11ae0e2028b58bf8d1f357433b805f52ac91412525720c0ce24e753a7ae

Contents?: true

Size: 763 Bytes

Versions: 16

Compression:

Stored size: 763 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).order(:id)
        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

16 entries across 16 versions & 1 rubygems

Version Path
superstore-1.2.0 lib/superstore/scope/batches.rb
superstore-1.1.4 lib/superstore/scope/batches.rb
superstore-1.1.3 lib/superstore/scope/batches.rb
superstore-1.1.2 lib/superstore/scope/batches.rb
superstore-1.1.1 lib/superstore/scope/batches.rb
superstore-1.1.0 lib/superstore/scope/batches.rb
superstore-1.0.12 lib/superstore/scope/batches.rb
superstore-1.0.11 lib/superstore/scope/batches.rb
superstore-1.0.10 lib/superstore/scope/batches.rb
superstore-1.0.9 lib/superstore/scope/batches.rb
superstore-1.0.8 lib/superstore/scope/batches.rb
superstore-1.0.7 lib/superstore/scope/batches.rb
superstore-1.0.6 lib/superstore/scope/batches.rb
superstore-1.0.5 lib/superstore/scope/batches.rb
superstore-1.0.4 lib/superstore/scope/batches.rb
superstore-1.0.3 lib/superstore/scope/batches.rb