Module: ULID::Generate
Constant Summary
Constants included
from Constants
Constants::B32REF, Constants::ENCODING, Constants::MAX_ENTROPY, Constants::MAX_TIME, Constants::MIN_ENTROPY, Constants::MIN_TIME
Instance Method Summary
collapse
Instance Method Details
#encode32 ⇒ Object
returns the binary ULID as Base32 encoded string.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/ulid/generate.rb', line 10
def encode32
id = @bytes.bytes
output = ''
output << ENCODING[(id[0]&224)>>5]
output << ENCODING[id[0]&31]
output << ENCODING[(id[1]&248)>>3]
output << ENCODING[((id[1]&7)<<2)|((id[2]&192)>>6)]
output << ENCODING[(id[2]&62)>>1]
output << ENCODING[((id[2]&1)<<4)|((id[3]&240)>>4)]
output << ENCODING[((id[3]&15)<<1)|((id[4]&128)>>7)]
output << ENCODING[(id[4]&124)>>2]
output << ENCODING[((id[4]&3)<<3)|((id[5]&224)>>5)]
output << ENCODING[id[5]&31]
output << ENCODING[(id[6]&248)>>3]
output << ENCODING[((id[6]&7)<<2)|((id[7]&192)>>6)]
output << ENCODING[(id[7]&62)>>1]
output << ENCODING[((id[7]&1)<<4)|((id[8]&240)>>4)]
output << ENCODING[((id[8]&15)<<1)|((id[9]&128)>>7)]
output << ENCODING[(id[9]&124)>>2]
output << ENCODING[((id[9]&3)<<3)|((id[10]&224)>>5)]
output << ENCODING[id[10]&31]
output << ENCODING[(id[11]&248)>>3]
output << ENCODING[((id[11]&7)<<2)|((id[12]&192)>>6)]
output << ENCODING[(id[12]&62)>>1]
output << ENCODING[((id[12]&1)<<4)|((id[13]&240)>>4)]
output << ENCODING[((id[13]&15)<<1)|((id[14]&128)>>7)]
output << ENCODING[(id[14]&124)>>2]
output << ENCODING[((id[14]&3)<<3)|((id[15]&224)>>5)]
output << ENCODING[id[15]&31]
output
end
|
#hundred_micro_time ⇒ Object
63
64
65
|
# File 'lib/ulid/generate.rb', line 63
def hundred_micro_time
(@time.to_f * 10_000).to_i
end
|
#random_bytes ⇒ Object
59
60
61
|
# File 'lib/ulid/generate.rb', line 59
def random_bytes
SecureRandom.random_bytes(10)
end
|
#time_48bit ⇒ Object
67
68
69
|
# File 'lib/ulid/generate.rb', line 67
def time_48bit
[hundred_micro_time].pack("Q>")[2..-1]
end
|