class Lite::Uxid::Ulid

Constants

MASK

Public Class Methods

encode() click to toggle source
# File lib/lite/uxid/ulid.rb, line 13
def encode
  klass = new
  klass.encode
end

Public Instance Methods

encode() click to toggle source
# File lib/lite/uxid/ulid.rb, line 20
def encode
  oct = octect
  ele = '0' * encoding_length
  pos = encoding_length - 1

  while oct.positive?
    ele[pos] = encoding_chars[oct & MASK]
    oct >>= 5
    pos -= 1
  end

  ele
end

Private Instance Methods

bytes() click to toggle source
# File lib/lite/uxid/ulid.rb, line 36
def bytes
  "#{unixtime_48bit}#{SecureRandom.random_bytes(10)}"
end
octect() click to toggle source
# File lib/lite/uxid/ulid.rb, line 40
def octect
  (hi, lo) = bytes.unpack('Q>Q>')
  (hi << 64) | lo
end
unixtime_48bit() click to toggle source
# File lib/lite/uxid/ulid.rb, line 50
def unixtime_48bit
  [unixtime_ms].pack('Q>')[2..-1]
end
unixtime_ms() click to toggle source
# File lib/lite/uxid/ulid.rb, line 45
def unixtime_ms
  time = Time.respond_to?(:current) ? Time.current : Time.now
  (time.to_f * 1_000).to_i
end