ruby-ulid 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ulid.rb +29 -5
- data/lib/ulid/monotonic_generator.rb +15 -13
- data/lib/ulid/version.rb +1 -1
- data/sig/ulid.rbs +8 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0adfc974863e1b2d248eac28305322e20d6b2dc94465b7ca3462fdd2c8676352
|
4
|
+
data.tar.gz: caecb83a030fa1eda5534e748ee3206208ebf25a1d9f76d33ad04030b4ea0f0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a216a8032385e9be00252fb0d9faf1aaa18bdfe75a0ae4b0a2cb1aa438a8786a7e9c477debd87fb6871274c0a50e1579cee41584513199c47b0198c51c6ac6a
|
7
|
+
data.tar.gz: 664d85fe6070a9c0017e831727a33a87799d1b10e7c4992b44b77be288fcc3cab51ecb528ce01fe0ce5d7a6262f61a2786ce30c54dcd90e8ef36c698f3f76f45
|
data/lib/ulid.rb
CHANGED
@@ -45,8 +45,7 @@ class ULID
|
|
45
45
|
# @param [Integer] entropy
|
46
46
|
# @return [ULID]
|
47
47
|
def self.generate(moment: current_milliseconds, entropy: reasonable_entropy)
|
48
|
-
milliseconds
|
49
|
-
new milliseconds: milliseconds, entropy: entropy
|
48
|
+
new milliseconds: milliseconds_from_moment(moment), entropy: entropy
|
50
49
|
end
|
51
50
|
|
52
51
|
# @param [Integer, Time] moment
|
@@ -122,17 +121,33 @@ class ULID
|
|
122
121
|
new milliseconds: milliseconds, entropy: entropy
|
123
122
|
end
|
124
123
|
|
124
|
+
# @param [Time] time
|
125
|
+
# @return [Time]
|
126
|
+
def self.floor(time)
|
127
|
+
if RUBY_VERSION >= '2.7'
|
128
|
+
time.floor(3)
|
129
|
+
else
|
130
|
+
Time.at(0, milliseconds_from_time(time), :millisecond)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
125
134
|
# @return [Integer]
|
126
135
|
def self.current_milliseconds
|
127
|
-
|
136
|
+
milliseconds_from_time(Time.now)
|
128
137
|
end
|
129
138
|
|
130
139
|
# @param [Time] time
|
131
140
|
# @return [Integer]
|
132
|
-
def self.
|
141
|
+
def self.milliseconds_from_time(time)
|
133
142
|
(time.to_r * 1000).to_i
|
134
143
|
end
|
135
144
|
|
145
|
+
# @param [Time, Integer] moment
|
146
|
+
# @return [Integer]
|
147
|
+
def self.milliseconds_from_moment(moment)
|
148
|
+
moment.kind_of?(Time) ? milliseconds_from_time(moment) : moment.to_int
|
149
|
+
end
|
150
|
+
|
136
151
|
# @return [Integer]
|
137
152
|
def self.reasonable_entropy
|
138
153
|
SecureRandom.random_number(MAX_ENTROPY)
|
@@ -168,6 +183,7 @@ class ULID
|
|
168
183
|
true
|
169
184
|
end
|
170
185
|
|
186
|
+
# @api private
|
171
187
|
# @param [Integer] integer
|
172
188
|
# @param [Integer] length
|
173
189
|
# @return [Array<Integer>]
|
@@ -179,6 +195,7 @@ class ULID
|
|
179
195
|
digits.reverse!
|
180
196
|
end
|
181
197
|
|
198
|
+
# @api private
|
182
199
|
# @see The logics taken from https://bugs.ruby-lang.org/issues/14401, thanks!
|
183
200
|
# @param [Array<Integer>] reversed_digits
|
184
201
|
# @return [Integer]
|
@@ -193,6 +210,7 @@ class ULID
|
|
193
210
|
|
194
211
|
attr_reader :milliseconds, :entropy
|
195
212
|
|
213
|
+
# @api private
|
196
214
|
# @param [Integer] milliseconds
|
197
215
|
# @param [Integer] entropy
|
198
216
|
# @return [void]
|
@@ -255,7 +273,13 @@ class ULID
|
|
255
273
|
|
256
274
|
# @return [Time]
|
257
275
|
def to_time
|
258
|
-
@time ||=
|
276
|
+
@time ||= begin
|
277
|
+
if RUBY_VERSION >= '2.7'
|
278
|
+
Time.at(0, @milliseconds, :millisecond, in: 'UTC').freeze
|
279
|
+
else
|
280
|
+
Time.at(0, @milliseconds, :millisecond).utc.freeze
|
281
|
+
end
|
282
|
+
end
|
259
283
|
end
|
260
284
|
|
261
285
|
# @return [Array(Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)]
|
@@ -4,35 +4,37 @@
|
|
4
4
|
|
5
5
|
class ULID
|
6
6
|
class MonotonicGenerator
|
7
|
+
# @api private
|
7
8
|
attr_accessor :latest_milliseconds, :latest_entropy
|
8
9
|
|
9
10
|
def initialize
|
10
11
|
reset
|
11
12
|
end
|
12
13
|
|
13
|
-
# @
|
14
|
+
# @param [Time, Integer] moment
|
14
15
|
# @return [ULID]
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
# @raise [OverflowError] if the entropy part is larger than the ULID limit in same milliseconds
|
17
|
+
# @raise [ArgumentError] if the given moment(milliseconds) is negative number
|
18
|
+
def generate(moment: ULID.current_milliseconds)
|
19
|
+
milliseconds = ULID.milliseconds_from_moment(moment)
|
20
|
+
raise ArgumentError, "milliseconds should not be negative: given: #{milliseconds}" if milliseconds.negative?
|
18
21
|
|
19
|
-
@latest_milliseconds
|
20
|
-
@latest_entropy ||= reasonable_entropy
|
21
|
-
if @latest_milliseconds != milliseconds
|
22
|
+
if @latest_milliseconds < milliseconds
|
22
23
|
@latest_milliseconds = milliseconds
|
23
|
-
@latest_entropy = reasonable_entropy
|
24
|
+
@latest_entropy = ULID.reasonable_entropy
|
24
25
|
else
|
25
26
|
@latest_entropy += 1
|
26
27
|
end
|
27
28
|
|
28
|
-
ULID.new milliseconds:
|
29
|
+
ULID.new milliseconds: @latest_milliseconds, entropy: @latest_entropy
|
29
30
|
end
|
30
31
|
|
31
|
-
# @
|
32
|
+
# @api private
|
33
|
+
# @return [void]
|
32
34
|
def reset
|
33
|
-
@latest_milliseconds =
|
34
|
-
@latest_entropy =
|
35
|
-
|
35
|
+
@latest_milliseconds = 0
|
36
|
+
@latest_entropy = ULID.reasonable_entropy
|
37
|
+
nil
|
36
38
|
end
|
37
39
|
|
38
40
|
# @raise [TypeError] always raises exception and does not freeze self
|
data/lib/ulid/version.rb
CHANGED
data/sig/ulid.rbs
CHANGED
@@ -18,6 +18,8 @@ class ULID
|
|
18
18
|
MONOTONIC_GENERATOR: MonotonicGenerator
|
19
19
|
include Comparable
|
20
20
|
|
21
|
+
type moment = Time | Integer
|
22
|
+
|
21
23
|
class Error < StandardError
|
22
24
|
end
|
23
25
|
|
@@ -28,15 +30,14 @@ class ULID
|
|
28
30
|
end
|
29
31
|
|
30
32
|
class MonotonicGenerator
|
31
|
-
attr_accessor latest_milliseconds: Integer
|
32
|
-
attr_accessor latest_entropy: Integer
|
33
|
+
attr_accessor latest_milliseconds: Integer
|
34
|
+
attr_accessor latest_entropy: Integer
|
33
35
|
def initialize: -> void
|
34
|
-
def generate: -> ULID
|
36
|
+
def generate: (?moment: moment) -> ULID
|
35
37
|
def reset: -> void
|
36
38
|
def freeze: -> void
|
37
39
|
end
|
38
40
|
|
39
|
-
type moment = Time | Integer
|
40
41
|
type octets = [Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer]
|
41
42
|
type timestamp_octets = [Integer, Integer, Integer, Integer, Integer, Integer]
|
42
43
|
type randomness_octets = [Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer]
|
@@ -60,7 +61,9 @@ class ULID
|
|
60
61
|
def self.generate: (?moment: moment, ?entropy: Integer) -> ULID
|
61
62
|
def self.monotonic_generate: -> ULID
|
62
63
|
def self.current_milliseconds: -> Integer
|
63
|
-
def self.
|
64
|
+
def self.milliseconds_from_time: (Time time) -> Integer
|
65
|
+
def self.milliseconds_from_moment: (moment moment) -> Integer
|
66
|
+
def self.floor: (Time time) -> Time
|
64
67
|
def self.reasonable_entropy: -> Integer
|
65
68
|
def self.parse: (String string) -> ULID
|
66
69
|
def self.from_uuidv4: (String uuid) -> ULID
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-ulid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenichi Kamiya
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: integer-base
|