active_uxid 5.0.1 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/active_uxid/base.rb +11 -13
- data/lib/active_uxid/hash.rb +39 -41
- data/lib/active_uxid/ulid.rb +23 -25
- data/lib/active_uxid/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea6cf79cc6bb9d309af2a2c882680a5896cc72b3
|
4
|
+
data.tar.gz: 9a13d21b1a1f3105f17a9744a5101c1d78938512
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c06be4e3cc8faaef59d10eb215f8fea3d61faab74880f31b31bb609d1c8af52200fc3e8ae8492ce90b3e90ed1bf85cceaa191d1fdafdf9ba0402f8fb81efde44
|
7
|
+
data.tar.gz: 7da641ce0d21cb341f45094ef4f3ae29feb4c8165e95421d2960d65207d048d10b940aadc84c2d6f601093d98e86971dec47c090e81a8cf649e5711548c1c808
|
data/lib/active_uxid/base.rb
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Base
|
3
|
+
class ActiveUxid::Base
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
ActiveUxid.configuration.instance_variables.each do |setting|
|
11
|
-
setting = setting.to_s.tr(':@', '')
|
12
|
-
define_method(setting) { @config.send(setting) }
|
13
|
-
end
|
5
|
+
def initialize
|
6
|
+
@config = ActiveUxid.configuration
|
7
|
+
end
|
14
8
|
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
ActiveUxid.configuration.instance_variables.each do |setting|
|
10
|
+
setting = setting.to_s.tr(':@', '')
|
11
|
+
define_method(setting) { @config.send(setting) }
|
12
|
+
end
|
18
13
|
|
14
|
+
def encoding_base
|
15
|
+
encoding_chars.length
|
19
16
|
end
|
17
|
+
|
20
18
|
end
|
data/lib/active_uxid/hash.rb
CHANGED
@@ -1,59 +1,57 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Hash < ActiveUxid::Base
|
3
|
+
class ActiveUxid::Hash < ActiveUxid::Base
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def self.encode(id)
|
12
|
-
klass = new(id)
|
13
|
-
klass.encode_uxid
|
14
|
-
end
|
5
|
+
def initialize(id)
|
6
|
+
@id = id
|
7
|
+
super()
|
8
|
+
end
|
15
9
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
10
|
+
def self.encode(id)
|
11
|
+
klass = new(id)
|
12
|
+
klass.encode_uxid
|
13
|
+
end
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
def self.decode(id)
|
16
|
+
klass = new(id)
|
17
|
+
klass.decode_uxid
|
18
|
+
end
|
24
19
|
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
def encode_uxid
|
21
|
+
uxid_encode_chars((@id + encoding_salt) << encoding_length)
|
22
|
+
end
|
28
23
|
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
def decode_uxid
|
25
|
+
(uxid_decode_chars(@id) >> encoding_length) - encoding_salt
|
26
|
+
end
|
32
27
|
|
33
|
-
|
28
|
+
def uxid_encode_chars(id)
|
29
|
+
return '0' if id.zero?
|
30
|
+
return nil if id.negative?
|
34
31
|
|
35
|
-
|
36
|
-
str = "#{encoding_chars[id % encoding_base]}#{str}"
|
37
|
-
id /= encoding_base
|
38
|
-
end
|
32
|
+
str = ''
|
39
33
|
|
40
|
-
|
34
|
+
while id.positive?
|
35
|
+
str = "#{encoding_chars[id % encoding_base]}#{str}"
|
36
|
+
id /= encoding_base
|
41
37
|
end
|
42
38
|
|
43
|
-
|
44
|
-
|
45
|
-
num = 0
|
46
|
-
len = id.length
|
47
|
-
max = len - 1
|
39
|
+
str
|
40
|
+
end
|
48
41
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
42
|
+
def uxid_decode_chars(id)
|
43
|
+
pos = 0
|
44
|
+
num = 0
|
45
|
+
len = id.length
|
46
|
+
max = len - 1
|
54
47
|
|
55
|
-
|
48
|
+
while pos < len
|
49
|
+
pow = encoding_base**(max - pos)
|
50
|
+
num += encoding_chars.index(id[pos]) * pow
|
51
|
+
pos += 1
|
56
52
|
end
|
57
53
|
|
54
|
+
num
|
58
55
|
end
|
56
|
+
|
59
57
|
end
|
data/lib/active_uxid/ulid.rb
CHANGED
@@ -1,36 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Ulid < ActiveUxid::Base
|
3
|
+
class ActiveUxid::Ulid < ActiveUxid::Base
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def uxid_encode
|
12
|
-
(1..encoding_length).reduce('') do |str, num|
|
13
|
-
shift = 128 - 5 * num
|
14
|
-
"#{str}#{encoding_chars[(uxid_octect >> shift) & 0x1f]}"
|
15
|
-
end
|
16
|
-
end
|
5
|
+
def self.encode
|
6
|
+
klass = new
|
7
|
+
klass.uxid_encode
|
8
|
+
end
|
17
9
|
|
18
|
-
|
19
|
-
|
10
|
+
def uxid_encode
|
11
|
+
(1..encoding_length).reduce('') do |str, num|
|
12
|
+
shift = 128 - 5 * num
|
13
|
+
"#{str}#{encoding_chars[(uxid_octect >> shift) & 0x1f]}"
|
20
14
|
end
|
15
|
+
end
|
21
16
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
17
|
+
def uxid_bytes
|
18
|
+
"#{uxid_unixtime_48bit}#{SecureRandom.random_bytes(10)}"
|
19
|
+
end
|
26
20
|
|
27
|
-
|
28
|
-
|
29
|
-
|
21
|
+
def uxid_octect
|
22
|
+
(hi, lo) = uxid_bytes.unpack('Q>Q>')
|
23
|
+
(hi << 64) | lo
|
24
|
+
end
|
30
25
|
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
def uxid_unixtime_flex
|
27
|
+
(Time.current.to_f * 10_000).to_i
|
28
|
+
end
|
34
29
|
|
30
|
+
def uxid_unixtime_48bit
|
31
|
+
[uxid_unixtime_flex].pack('Q>')[2..-1]
|
35
32
|
end
|
33
|
+
|
36
34
|
end
|
data/lib/active_uxid/version.rb
CHANGED