Class: Killbill::Plugin::ActiveMerchant::ActiveRecord::StreamyResultSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.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.



11
12
13
14
15
# File 'lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb', line 11

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

Instance Method Details

#each(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb', line 17

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
ensure
  # Make sure to return DB connections to the Pool
  close_connection
end

#to_aObject



29
30
31
# File 'lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb', line 29

def to_a
  super.to_a.flatten
end