ruby-ulid 0.0.7 → 0.0.8
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 +30 -11
- data/lib/ulid/version.rb +1 -1
- data/sig/ulid.rbs +44 -40
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1179379859a437f04ff904c941372fffc4aa3027d550ae50c67f744e4899d9a
|
4
|
+
data.tar.gz: 71781ee8481373bb7229f10f88a1703b2653c11a4f18cbc7b50eb4adbec58908
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d48e489221851290a486ffe3a39847196e900bb54751fa7d0c808f7580c8838d1a6382b203da1b90ad4f74dd9e998239e0a6c996b06d99881c39489ff607697
|
7
|
+
data.tar.gz: fbef689db966901800b24e18820ee6744aeeaeb2476f14349b74d634ed1e5d359a4bff4abb3eb0eee94e4d0183f5db1420719d7e94afd3e803d21d62510087d7
|
data/lib/ulid.rb
CHANGED
@@ -3,11 +3,14 @@
|
|
3
3
|
# Copyright (C) 2021 Kenichi Kamiya
|
4
4
|
|
5
5
|
require 'securerandom'
|
6
|
-
require 'singleton'
|
7
6
|
require 'integer/base'
|
8
7
|
require_relative 'ulid/version'
|
9
8
|
|
10
9
|
# @see https://github.com/ulid/spec
|
10
|
+
# @!attribute [r] milliseconds
|
11
|
+
# @return [Integer]
|
12
|
+
# @!attribute [r] entropy
|
13
|
+
# @return [Integer]
|
11
14
|
class ULID
|
12
15
|
include Comparable
|
13
16
|
|
@@ -33,14 +36,13 @@ class ULID
|
|
33
36
|
TIME_FORMAT_IN_INSPECT = '%Y-%m-%d %H:%M:%S.%3N %Z'
|
34
37
|
|
35
38
|
class MonotonicGenerator
|
36
|
-
include Singleton
|
37
|
-
|
38
39
|
attr_accessor :latest_milliseconds, :latest_entropy
|
39
40
|
|
40
41
|
def initialize
|
41
42
|
reset
|
42
43
|
end
|
43
44
|
|
45
|
+
# @raise [OverflowError] if the entropy part is larger than the ULID limit in same milliseconds
|
44
46
|
# @return [ULID]
|
45
47
|
def generate
|
46
48
|
milliseconds = ULID.current_milliseconds
|
@@ -65,15 +67,16 @@ class ULID
|
|
65
67
|
self
|
66
68
|
end
|
67
69
|
|
70
|
+
# @raise [TypeError] always raises exception and does not freeze self
|
68
71
|
# @return [void]
|
69
72
|
def freeze
|
70
73
|
raise TypeError, "cannot freeze #{self.class}"
|
71
74
|
end
|
72
75
|
end
|
73
76
|
|
74
|
-
MONOTONIC_GENERATOR = MonotonicGenerator.
|
77
|
+
MONOTONIC_GENERATOR = MonotonicGenerator.new
|
75
78
|
|
76
|
-
private_constant :ENCODING_CHARS, :TIME_FORMAT_IN_INSPECT
|
79
|
+
private_constant :ENCODING_CHARS, :TIME_FORMAT_IN_INSPECT
|
77
80
|
|
78
81
|
# @param [Integer, Time] moment
|
79
82
|
# @param [Integer] entropy
|
@@ -83,8 +86,17 @@ class ULID
|
|
83
86
|
new milliseconds: milliseconds, entropy: entropy
|
84
87
|
end
|
85
88
|
|
89
|
+
# @deprecated This method actually changes class state. Use {ULID::MonotonicGenerator} instead.
|
90
|
+
# @raise [OverflowError] if the entropy part is larger than the ULID limit in same milliseconds
|
86
91
|
# @return [ULID]
|
87
92
|
def self.monotonic_generate
|
93
|
+
warning = "`ULID.monotonic_generate` actually changes class state. Use `ULID::MonotonicGenerator` instead."
|
94
|
+
if RUBY_VERSION >= '3.0'
|
95
|
+
Warning.warn(warning, category: :deprecated)
|
96
|
+
else
|
97
|
+
Warning.warn(warning)
|
98
|
+
end
|
99
|
+
|
88
100
|
MONOTONIC_GENERATOR.generate
|
89
101
|
end
|
90
102
|
|
@@ -106,6 +118,8 @@ class ULID
|
|
106
118
|
|
107
119
|
# @param [String, #to_str] string
|
108
120
|
# @return [ULID]
|
121
|
+
# @raise [ParserError] if the given format is not correct for ULID specs
|
122
|
+
# @raise [OverflowError] if the given value is larger than the ULID limit
|
109
123
|
def self.parse(string)
|
110
124
|
begin
|
111
125
|
string = string.to_str
|
@@ -123,7 +137,6 @@ class ULID
|
|
123
137
|
new milliseconds: milliseconds, entropy: entropy
|
124
138
|
end
|
125
139
|
|
126
|
-
# @param [String] string
|
127
140
|
# @return [Boolean]
|
128
141
|
def self.valid?(string)
|
129
142
|
parse(string)
|
@@ -135,6 +148,11 @@ class ULID
|
|
135
148
|
|
136
149
|
attr_reader :milliseconds, :entropy
|
137
150
|
|
151
|
+
# @param [Integer] milliseconds
|
152
|
+
# @param [Integer] entropy
|
153
|
+
# @return [void]
|
154
|
+
# @raise [OverflowError] if the given value is larger than the ULID limit
|
155
|
+
# @raise [ArgumentError] if the given milliseconds and/or entropy is negative number
|
138
156
|
def initialize(milliseconds:, entropy:)
|
139
157
|
milliseconds = milliseconds.to_int
|
140
158
|
entropy = entropy.to_int
|
@@ -160,7 +178,7 @@ class ULID
|
|
160
178
|
|
161
179
|
# @return [Integer, nil]
|
162
180
|
def <=>(other)
|
163
|
-
other.kind_of?(
|
181
|
+
other.kind_of?(ULID) ? (to_i <=> other.to_i) : nil
|
164
182
|
end
|
165
183
|
|
166
184
|
# @return [String]
|
@@ -170,7 +188,7 @@ class ULID
|
|
170
188
|
|
171
189
|
# @return [Boolean]
|
172
190
|
def eql?(other)
|
173
|
-
other.equal?(self) || (other.kind_of?(
|
191
|
+
other.equal?(self) || (other.kind_of?(ULID) && other.to_i == to_i)
|
174
192
|
end
|
175
193
|
alias_method :==, :eql?
|
176
194
|
|
@@ -195,21 +213,22 @@ class ULID
|
|
195
213
|
@time ||= Time.at(0, @milliseconds, :millisecond).utc
|
196
214
|
end
|
197
215
|
|
198
|
-
# @return [Array
|
216
|
+
# @return [Array(Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)]
|
199
217
|
def octets
|
200
218
|
@octets ||= (time_octets + randomness_octets).freeze
|
201
219
|
end
|
202
220
|
|
203
|
-
# @return [Array
|
221
|
+
# @return [Array(Integer, Integer, Integer, Integer, Integer, Integer)]
|
204
222
|
def time_octets
|
205
223
|
@time_octets ||= octets_from_integer(@milliseconds, length: TIME_OCTETS_LENGTH).freeze
|
206
224
|
end
|
207
225
|
|
208
|
-
# @return [Array
|
226
|
+
# @return [Array(Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)]
|
209
227
|
def randomness_octets
|
210
228
|
@randomness_octets ||= octets_from_integer(@entropy, length: RANDOMNESS_OCTETS_LENGTH).freeze
|
211
229
|
end
|
212
230
|
|
231
|
+
# @raise [OverflowError] if the next entropy part is larger than the ULID limit
|
213
232
|
# @return [ULID]
|
214
233
|
def next
|
215
234
|
@next ||= self.class.new(milliseconds: @milliseconds, entropy: @entropy + 1)
|
data/lib/ulid/version.rb
CHANGED
data/sig/ulid.rbs
CHANGED
@@ -2,22 +2,45 @@
|
|
2
2
|
class ULID
|
3
3
|
VERSION: String
|
4
4
|
ENCODING_CHARS: Array[String]
|
5
|
-
TIME_PART_LENGTH:
|
6
|
-
RANDOMNESS_PART_LENGTH:
|
7
|
-
ENCODED_ID_LENGTH:
|
8
|
-
TIME_OCTETS_LENGTH:
|
9
|
-
RANDOMNESS_OCTETS_LENGTH:
|
10
|
-
OCTETS_LENGTH:
|
11
|
-
MAX_MILLISECONDS:
|
12
|
-
MAX_ENTROPY:
|
13
|
-
TIME_FORMAT_IN_INSPECT:
|
5
|
+
TIME_PART_LENGTH: 10
|
6
|
+
RANDOMNESS_PART_LENGTH: 16
|
7
|
+
ENCODED_ID_LENGTH: 26
|
8
|
+
TIME_OCTETS_LENGTH: 6
|
9
|
+
RANDOMNESS_OCTETS_LENGTH: 10
|
10
|
+
OCTETS_LENGTH: 16
|
11
|
+
MAX_MILLISECONDS: 281474976710655
|
12
|
+
MAX_ENTROPY: 1208925819614629174706175
|
13
|
+
TIME_FORMAT_IN_INSPECT: '%Y-%m-%d %H:%M:%S.%3N %Z'
|
14
14
|
MONOTONIC_GENERATOR: MonotonicGenerator
|
15
15
|
include Comparable
|
16
|
+
|
17
|
+
class Error < StandardError
|
18
|
+
end
|
19
|
+
|
20
|
+
class OverflowError < Error
|
21
|
+
end
|
22
|
+
|
23
|
+
class ParserError < Error
|
24
|
+
end
|
25
|
+
|
26
|
+
class MonotonicGenerator
|
27
|
+
attr_accessor latest_milliseconds: Integer?
|
28
|
+
attr_accessor latest_entropy: Integer?
|
29
|
+
def initialize: -> void
|
30
|
+
def generate: -> ULID
|
31
|
+
def reset: -> void
|
32
|
+
def freeze: -> void
|
33
|
+
end
|
34
|
+
|
35
|
+
type octets = [Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer]
|
36
|
+
type time_octets = [Integer, Integer, Integer, Integer, Integer, Integer]
|
37
|
+
type randomness_octets = [Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer]
|
38
|
+
|
16
39
|
@string: String
|
17
40
|
@integer: Integer
|
18
|
-
@octets:
|
19
|
-
@time_octets:
|
20
|
-
@randomness_octets:
|
41
|
+
@octets: octets
|
42
|
+
@time_octets: time_octets
|
43
|
+
@randomness_octets: randomness_octets
|
21
44
|
@inspect: String
|
22
45
|
@time: Time
|
23
46
|
@next: ULID
|
@@ -33,43 +56,24 @@ class ULID
|
|
33
56
|
attr_reader entropy: Integer
|
34
57
|
def initialize: (milliseconds: Integer, entropy: Integer) -> void
|
35
58
|
def to_str: -> String
|
36
|
-
|
59
|
+
alias to_s to_str
|
37
60
|
def to_i: -> Integer
|
38
|
-
|
39
|
-
def <=>: (
|
61
|
+
alias hash to_i
|
62
|
+
def <=>: (ULID other) -> Integer
|
63
|
+
| (untyped other) -> Integer?
|
40
64
|
def inspect: -> String
|
41
65
|
def eql?: (untyped other) -> bool
|
42
|
-
|
66
|
+
alias == eql?
|
43
67
|
def ===: (untyped other) -> bool
|
44
68
|
def to_time: -> Time
|
45
|
-
def octets: ->
|
46
|
-
def time_octets: ->
|
47
|
-
def randomness_octets: ->
|
69
|
+
def octets: -> octets
|
70
|
+
def time_octets: -> time_octets
|
71
|
+
def randomness_octets: -> randomness_octets
|
48
72
|
def next: -> ULID
|
49
|
-
|
73
|
+
alias succ next
|
50
74
|
def freeze: -> self
|
51
75
|
|
52
76
|
private
|
53
77
|
def octets_from_integer: (Integer integer, length: Integer) -> Array[Integer]
|
54
78
|
def inverse_of_digits: (Array[Integer] reversed_digits) -> Integer
|
55
|
-
|
56
|
-
class Error < StandardError
|
57
|
-
end
|
58
|
-
|
59
|
-
class OverflowError < Error
|
60
|
-
end
|
61
|
-
|
62
|
-
class ParserError < Error
|
63
|
-
end
|
64
|
-
|
65
|
-
class MonotonicGenerator
|
66
|
-
include Singleton
|
67
|
-
|
68
|
-
attr_accessor latest_milliseconds: Integer?
|
69
|
-
attr_accessor latest_entropy: Integer?
|
70
|
-
def initialize: -> void
|
71
|
-
def generate: -> ULID
|
72
|
-
def reset: -> void
|
73
|
-
def freeze: -> void
|
74
|
-
end
|
75
79
|
end
|