Sha256: 0e7ea9696c65367563aa9c9551e98526174ad094840aae24e89d99b90da843fc

Contents?: true

Size: 1.17 KB

Versions: 40

Compression:

Stored size: 1.17 KB

Contents

module Killbill
  module Plugin
    module ActiveMerchant
      module ActiveRecord
        require 'active_record'

        # Closest from a streaming API as we can get with ActiveRecord
        class StreamyResultSet
          include Enumerable

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

          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

          def to_a
            super.to_a.flatten
          end

          private

          def close_connection
            pool = ::ActiveRecord::Base.connection_pool
            return unless pool.active_connection?

            ::ActiveRecord::Base.connection.close # check-in to pool
          end
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
killbill-9.4.1 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.3.6 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-9.4.0 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-9.3.2 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-9.3.1 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.3.5 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-9.3.0 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-9.2.2 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.3.4 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.3.3 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-9.2.1 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-9.2.0 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.3.2 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.3.1 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.3.0 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.2.0 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.1.0 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-7.0.6 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.0.1 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb
killbill-8.0.0 lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb