Sha256: d2994889195ac0b5d234dd66b8df27b07b30591ea7a6a3d79d8fbb7e09aa24bd

Contents?: true

Size: 712 Bytes

Versions: 4

Compression:

Stored size: 712 Bytes

Contents

# frozen_string_literal: true

module ActiveUxid
  class Ulid < ActiveUxid::Base

    def initialize; 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

4 entries across 4 versions & 1 rubygems

Version Path
active_uxid-1.0.3 lib/active_uxid/ulid.rb
active_uxid-1.0.2 lib/active_uxid/ulid.rb
active_uxid-1.0.1 lib/active_uxid/ulid.rb
active_uxid-1.0.0 lib/active_uxid/ulid.rb