Sha256: f6e930c273a4b9fc1c14b32bdf51b65cd6d6fd86985f414a03c377cf2c312364

Contents?: true

Size: 632 Bytes

Versions: 2

Compression:

Stored size: 632 Bytes

Contents

# frozen_string_literal: true

class ActiveUxid::Ulid < ActiveUxid::Base

  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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_uxid-5.1.0 lib/active_uxid/ulid.rb
active_uxid-5.0.2 lib/active_uxid/ulid.rb