Class: UXID::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/uxid/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entropyObject

Returns the value of attribute entropy.



5
6
7
# File 'lib/uxid/model.rb', line 5

def entropy
  @entropy
end

#entropy_encodedObject

Returns the value of attribute entropy_encoded.



5
6
7
# File 'lib/uxid/model.rb', line 5

def entropy_encoded
  @entropy_encoded
end

#prefixObject

Returns the value of attribute prefix.



5
6
7
# File 'lib/uxid/model.rb', line 5

def prefix
  @prefix
end

#rand_sizeObject

Returns the value of attribute rand_size.



5
6
7
# File 'lib/uxid/model.rb', line 5

def rand_size
  @rand_size
end

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/uxid/model.rb', line 5

def size
  @size
end

#timeObject

Returns the value of attribute time.



5
6
7
# File 'lib/uxid/model.rb', line 5

def time
  @time
end

#time_encodedObject

Returns the value of attribute time_encoded.



5
6
7
# File 'lib/uxid/model.rb', line 5

def time_encoded
  @time_encoded
end

Instance Method Details

#encodeObject



7
8
9
10
# File 'lib/uxid/model.rb', line 7

def encode
  encoder = ::UXID::Encoder.new self
  encoder.encode
end

#encodedObject



30
31
32
33
34
35
36
37
38
# 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_bytesObject



22
23
24
25
26
27
28
# 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?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/uxid/model.rb', line 40

def has_prefix?
  !(prefix.nil? || prefix == "")
end

#time_bytesObject

Returns time as 48 bits



13
14
15
16
17
18
19
20
# 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