Sha256: ece6dac846b1d991b4c17a8ce0958714729cd8b21eead2fcf75010a424402837

Contents?: true

Size: 895 Bytes

Versions: 1

Compression:

Stored size: 895 Bytes

Contents

# frozen_string_literal: true

module ActiveUxid
  class Ulid

    ENCODING_CHARS ||= ActiveUxid.configuration.encoding_chars
    ENCODING_LENGTH = ActiveUxid.configuration.encoding_length

    def initialize
      @encoding_length = ActiveUxid.configuration.encoding_length
    end

    def self.encode
      klass = new
      klass.uxid_encode
    end

    def uxid_encode
      (1..@encoding_length).reduce('') do |str, num|
        shift = 128 - 5 * num
        "#{str}#{ENCODING_CHARS[(uxid_octect >> shift) & 0x1f]}"
      end
    end

    def uxid_bytes
      "#{uxid_unixtime_48bit}#{SecureRandom.random_bytes(10)}"
    end

    def uxid_octect
      (hi, lo) = uxid_bytes.unpack('Q>Q>')
      (hi << 64) | lo
    end

    def uxid_unixtime_flex
      (Time.current.to_f * 10_000).to_i
    end

    def uxid_unixtime_48bit
      [uxid_unixtime_flex].pack('Q>')[2..-1]
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_uxid-1.0.10 lib/active_uxid/ulid.rb