class UXID::Model

Attributes

entropy[RW]
entropy_encoded[RW]
prefix[RW]
rand_size[RW]
size[RW]
time[RW]
time_encoded[RW]

Public Instance Methods

encode() click to toggle source
# File lib/uxid/model.rb, line 7
def encode
  encoder = ::UXID::Encoder.new self
  encoder.encode
end
encoded() click to toggle source
# File lib/uxid/model.rb, line 30
def encoded
  id = time_encoded + entropy_encoded

  if has_prefix?
    [prefix, id].join UXID::DELIMITER
  else
    id
  end
end
entropy_bytes() click to toggle source
# File lib/uxid/model.rb, line 22
def entropy_bytes
  return @entropy_bytes if @entropy_bytes

  bytes_string = SecureRandom.random_bytes @rand_size
  @entropy_bytes = bytes_string.bytes
  return @entropy_bytes
end
has_prefix?() click to toggle source
# File lib/uxid/model.rb, line 40
def has_prefix?
  !(prefix.nil? || prefix == "")
end
time_bytes() click to toggle source

Returns time as 48 bits

# File lib/uxid/model.rb, line 13
def time_bytes
  return @time_bytes if @time_bytes

  time_ms = (@time.to_f * 1000).to_i
  bytes = [time_ms].pack "Q>"
  bytes_string = bytes[2..-1]
  @time_bytes = bytes_string.bytes
end