Class: ActiveUxid::Ulid
- Inherits:
-
Base
- Object
- Base
- ActiveUxid::Ulid
show all
- Defined in:
- lib/active_uxid/ulid.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#encoding_base, #initialize
Class Method Details
.encode ⇒ Object
6
7
8
9
|
# File 'lib/active_uxid/ulid.rb', line 6
def self.encode
klass = new
klass.uxid_encode
end
|
Instance Method Details
#uxid_bytes ⇒ Object
18
19
20
|
# File 'lib/active_uxid/ulid.rb', line 18
def uxid_bytes
"#{uxid_unixtime_48bit}#{SecureRandom.random_bytes(10)}"
end
|
#uxid_encode ⇒ Object
11
12
13
14
15
16
|
# File 'lib/active_uxid/ulid.rb', line 11
def uxid_encode
(1..encoding_length).reduce('') do |str, num|
shift = 128 - 5 * num
"#{str}#{encoding_chars[(uxid_octect >> shift) & 0x1f]}"
end
end
|
#uxid_octect ⇒ Object
22
23
24
25
|
# File 'lib/active_uxid/ulid.rb', line 22
def uxid_octect
(hi, lo) = uxid_bytes.unpack('Q>Q>')
(hi << 64) | lo
end
|
#uxid_unixtime_48bit ⇒ Object
31
32
33
|
# File 'lib/active_uxid/ulid.rb', line 31
def uxid_unixtime_48bit
[uxid_unixtime_flex].pack('Q>')[2..-1]
end
|
#uxid_unixtime_flex ⇒ Object
27
28
29
|
# File 'lib/active_uxid/ulid.rb', line 27
def uxid_unixtime_flex
(Time.current.to_f * 10_000).to_i
end
|