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
5
6
7
8
|
# File 'lib/active_uxid/ulid.rb', line 5
def self.encode
klass = new
klass.uxid_encode
end
|
Instance Method Details
#uxid_bytes ⇒ Object
17
18
19
|
# File 'lib/active_uxid/ulid.rb', line 17
def uxid_bytes
"#{uxid_unixtime_48bit}#{SecureRandom.random_bytes(10)}"
end
|
#uxid_encode ⇒ Object
10
11
12
13
14
15
|
# File 'lib/active_uxid/ulid.rb', line 10
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
21
22
23
24
|
# File 'lib/active_uxid/ulid.rb', line 21
def uxid_octect
(hi, lo) = uxid_bytes.unpack('Q>Q>')
(hi << 64) | lo
end
|
#uxid_unixtime_48bit ⇒ Object
30
31
32
|
# File 'lib/active_uxid/ulid.rb', line 30
def uxid_unixtime_48bit
[uxid_unixtime_flex].pack('Q>')[2..-1]
end
|
#uxid_unixtime_flex ⇒ Object
26
27
28
|
# File 'lib/active_uxid/ulid.rb', line 26
def uxid_unixtime_flex
(Time.current.to_f * 10_000).to_i
end
|