module ActiveRecord::Stream::ClassMethods

Public Instance Methods

stream(options={}) click to toggle source
# File lib/activerecord/stream.rb, line 12
def stream(options={})
  offset = options[:start] || 0
  batch_size = options[:batch_size] || 500
  scope = respond_to?(:scoped) ? scoped : all

  Enumerator.new do |yielder|
    loop do
      current_batch = load_batch(scope, batch_size, offset)
      current_batch.each{|r| yielder << r}

      break if current_batch.length < batch_size
      offset += batch_size
    end
  end
end

Private Instance Methods

load_batch(scope, limit, offset) click to toggle source
# File lib/activerecord/stream.rb, line 29
def load_batch(scope, limit, offset)
  scope.limit(limit).offset(offset).to_a
end