Class: Killbill::Litle::StreamyResultSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/litle/litle_utils.rb

Overview

Closest from a streaming API as we can get with ActiveRecord

Instance Method Summary collapse

Constructor Details

#initialize(limit, batch_size = 100, &delegate) ⇒ StreamyResultSet

Returns a new instance of StreamyResultSet.



29
30
31
32
33
# File 'lib/litle/litle_utils.rb', line 29

def initialize(limit, batch_size = 100, &delegate)
  @limit = limit
  @batch = [batch_size, limit].min
  @delegate = delegate
end

Instance Method Details

#each(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/litle/litle_utils.rb', line 35

def each(&block)
  (0..(@limit - @batch)).step(@batch) do |i|
    result = @delegate.call(i, @batch)
    block.call(result)
    # Optimization: bail out if no more results
    break if result.nil? || result.empty?
  end if @batch > 0
  # Make sure to return DB connections to the Pool
  ActiveRecord::Base.connection.close
end

#to_aObject



46
47
48
# File 'lib/litle/litle_utils.rb', line 46

def to_a
  super.to_a.flatten
end