Sha256: c4095c0205f44ef62758e352d51e0600b5954dfbb79c00d7299aed5c91485058

Contents?: true

Size: 1.33 KB

Versions: 16

Compression:

Stored size: 1.33 KB

Contents

class Integer
  def base(b)
    self < b ? [self] : (self/b).base(b) + [self%b]
  end
end

module Killbill::Litle
  class Utils
    # Use base 62 to be safe on the Litle side
    BASE62 = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a

    def self.compact_uuid(uuid)
      uuid = uuid.gsub(/-/, '')
      uuid.hex.base(62).map{ |i| BASE62[i].chr } * ''
    end

    def self.unpack_uuid(base62_uuid)
      as_hex = base62_uuid.split(//).inject(0) { |i,e| i*62 + BASE62.index(e[0]) }
      no_hyphens = "%x" % as_hex
      no_hyphens = '0' * (32 - no_hyphens.size) + no_hyphens
      no_hyphens.insert(8, "-").insert(13, "-").insert(18, "-").insert(23, "-")
    end
  end

  # 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
      # Make sure to return DB connections to the Pool
      ActiveRecord::Base.connection.close
    end

    def to_a
      super.to_a.flatten
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
killbill-litle-5.0.0 lib/litle/litle_utils.rb
killbill-litle-4.0.1 lib/litle/litle_utils.rb
killbill-litle-4.0.0 lib/litle/litle_utils.rb
killbill-litle-3.0.0 lib/litle/litle_utils.rb
killbill-litle-2.0.0 lib/litle/litle_utils.rb
killbill-litle-1.10.0 lib/litle/litle_utils.rb
killbill-litle-1.9.9 lib/litle/litle_utils.rb
killbill-litle-1.9.8 lib/litle/litle_utils.rb
killbill-litle-1.9.7 lib/litle/litle_utils.rb
killbill-litle-1.9.6 lib/litle/litle_utils.rb
killbill-litle-1.9.5 lib/litle/litle_utils.rb
killbill-litle-1.9.4 lib/litle/litle_utils.rb
killbill-litle-1.9.3 lib/litle/litle_utils.rb
killbill-litle-1.9.2 lib/litle/litle_utils.rb
killbill-litle-1.9.1 lib/litle/litle_utils.rb
killbill-litle-1.9.0 lib/litle/litle_utils.rb