Class: UXID::Model
- Inherits:
-
Object
- Object
- UXID::Model
- Defined in:
- lib/uxid/model.rb
Instance Attribute Summary collapse
-
#entropy ⇒ Object
Returns the value of attribute entropy.
-
#entropy_encoded ⇒ Object
Returns the value of attribute entropy_encoded.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#rand_size ⇒ Object
Returns the value of attribute rand_size.
-
#size ⇒ Object
Returns the value of attribute size.
-
#time ⇒ Object
Returns the value of attribute time.
-
#time_encoded ⇒ Object
Returns the value of attribute time_encoded.
Instance Method Summary collapse
- #encode ⇒ Object
- #encoded ⇒ Object
- #entropy_bytes ⇒ Object
- #has_prefix? ⇒ Boolean
-
#time_bytes ⇒ Object
Returns time as 48 bits.
Instance Attribute Details
#entropy ⇒ Object
Returns the value of attribute entropy.
5 6 7 |
# File 'lib/uxid/model.rb', line 5 def entropy @entropy end |
#entropy_encoded ⇒ Object
Returns the value of attribute entropy_encoded.
5 6 7 |
# File 'lib/uxid/model.rb', line 5 def entropy_encoded @entropy_encoded end |
#prefix ⇒ Object
Returns the value of attribute prefix.
5 6 7 |
# File 'lib/uxid/model.rb', line 5 def prefix @prefix end |
#rand_size ⇒ Object
Returns the value of attribute rand_size.
5 6 7 |
# File 'lib/uxid/model.rb', line 5 def rand_size @rand_size end |
#size ⇒ Object
Returns the value of attribute size.
5 6 7 |
# File 'lib/uxid/model.rb', line 5 def size @size end |
#time ⇒ Object
Returns the value of attribute time.
5 6 7 |
# File 'lib/uxid/model.rb', line 5 def time @time end |
#time_encoded ⇒ Object
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
#encode ⇒ Object
7 8 9 10 |
# File 'lib/uxid/model.rb', line 7 def encode encoder = ::UXID::Encoder.new self encoder.encode end |
#encoded ⇒ Object
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_bytes ⇒ Object
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
40 41 42 |
# File 'lib/uxid/model.rb', line 40 def has_prefix? !(prefix.nil? || prefix == "") end |
#time_bytes ⇒ Object
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 |