ruby-ulid 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a390455a27395fd77ef398b0613e72b321b96d44e8454c8ab9346ae15eddfdc
4
- data.tar.gz: 1150658ae26cc591f3946898ccb356ce1c6135292de0600ef0604aceb7a00554
3
+ metadata.gz: 01dca4f18f5bb3168b135847b6a1171d00a6ceb03b1d2ef68b6df212975cafc1
4
+ data.tar.gz: e1b0cb033ded88591d4817395507f517f46b7d3dd3d3a72021a216dcfef09d12
5
5
  SHA512:
6
- metadata.gz: 4ffb5c69ec1327bacbb54b4afdd6718f8cc9b5afd450f739588d8e414c645eca9c1090c77efb464293f0e8e1b960cf4df75289785d3cd7b489404e31c24ecc8a
7
- data.tar.gz: bf6a08216e0745d0727c356a5a239126bac8518f8fbb57f295b417e14ed0878c8f0c32774b83137b1feb58d8230140492cb469bb18dc9d5df5db5aa088410ffb
6
+ metadata.gz: c9c1a60687c05cfa99ce37f4e692938bff96dd15afa6b0adce40de5b897303b6e57301f069299c06d726bdd7a425e1ed581b7b60e15efc88d69abc417c577005
7
+ data.tar.gz: 71f333e7a6d6169f346b0e8fba65c9413fa24c32ce6799623e11b789544fb057fa0f67f83ed94a896cbac9df123ee4f9c9598735953217bd6fec332df659ee9f
File without changes
data/README.md CHANGED
@@ -49,7 +49,7 @@ Should be installed!
49
49
  Add this line to your application/library's `Gemfile` is needed in basic use-case
50
50
 
51
51
  ```ruby
52
- gem 'ruby-ulid', '>= 0.1.4', '< 0.2.0'
52
+ gem 'ruby-ulid', '>= 0.1.5', '< 0.2.0'
53
53
  ```
54
54
 
55
55
  ### Generator and Parser
@@ -192,7 +192,7 @@ ULID.floor(time) #=> 2000-01-01 00:00:00.123 UTC
192
192
  For rough operations, `ULID.scan` might be useful.
193
193
 
194
194
  ```ruby
195
- json =<<'EOD'
195
+ json = <<'JSON'
196
196
  {
197
197
  "id": "01F4GNAV5ZR6FJQ5SFQC7WDSY3",
198
198
  "author": {
@@ -217,7 +217,7 @@ json =<<'EOD'
217
217
  }
218
218
  ]
219
219
  }
220
- EOD
220
+ JSON
221
221
 
222
222
  ULID.scan(json).to_a
223
223
  #=>
data/lib/ulid.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # coding: us-ascii
2
2
  # frozen_string_literal: true
3
+
3
4
  # Copyright (C) 2021 Kenichi Kamiya
4
5
 
5
6
  require 'securerandom'
@@ -60,6 +61,7 @@ class ULID
60
61
  # @return [ULID]
61
62
  def self.at(time)
62
63
  raise ArgumentError, 'ULID.at takes only `Time` instance' unless Time === time
64
+
63
65
  from_milliseconds_and_entropy(milliseconds: milliseconds_from_time(time), entropy: reasonable_entropy)
64
66
  end
65
67
 
@@ -91,19 +93,21 @@ class ULID
91
93
  # * Do not take random generator for the arguments
92
94
  # * Raising error instead of truncating elements for the given number
93
95
  def self.sample(*args, period: nil)
94
- int_generator = if period
95
- ulid_range = range(period)
96
- min, max, exclude_end = ulid_range.begin.to_i, ulid_range.end.to_i, ulid_range.exclude_end?
96
+ int_generator = (
97
+ if period
98
+ ulid_range = range(period)
99
+ min, max, exclude_end = ulid_range.begin.to_i, ulid_range.end.to_i, ulid_range.exclude_end?
97
100
 
98
- possibilities = (max - min) + (exclude_end ? 0 : 1)
99
- raise ArgumentError, "given range `#{ulid_range.inspect}` does not have possibilities" unless possibilities.positive?
101
+ possibilities = (max - min) + (exclude_end ? 0 : 1)
102
+ raise ArgumentError, "given range `#{ulid_range.inspect}` does not have possibilities" unless possibilities.positive?
100
103
 
101
- -> {
102
- SecureRandom.random_number(possibilities) + min
103
- }
104
- else
105
- RANDOM_INTEGER_GENERATOR
106
- end
104
+ -> {
105
+ SecureRandom.random_number(possibilities) + min
106
+ }
107
+ else
108
+ RANDOM_INTEGER_GENERATOR
109
+ end
110
+ )
107
111
 
108
112
  case args.size
109
113
  when 0
@@ -134,6 +138,7 @@ class ULID
134
138
  string = String.try_convert(string)
135
139
  raise ArgumentError, 'ULID.scan takes only strings' unless string
136
140
  return to_enum(__callee__, string) unless block_given?
141
+
137
142
  string.scan(SCANNING_PATTERN) do |matched|
138
143
  yield parse(matched)
139
144
  end
@@ -156,7 +161,7 @@ class ULID
156
161
  milliseconds = n32encoded_timestamp.to_i(32)
157
162
  entropy = n32encoded_randomness.to_i(32)
158
163
 
159
- new milliseconds: milliseconds, entropy: entropy, integer: integer
164
+ new(milliseconds: milliseconds, entropy: entropy, integer: integer)
160
165
  end
161
166
 
162
167
  # @param [Range<Time>, Range<nil>, Range[ULID]] period
@@ -164,6 +169,7 @@ class ULID
164
169
  # @raise [ArgumentError] if the given period is not a `Range[Time]`, `Range[nil]` or `Range[ULID]`
165
170
  def self.range(period)
166
171
  raise ArgumentError, 'ULID.range takes only `Range[Time]`, `Range[nil]` or `Range[ULID]`' unless Range === period
172
+
167
173
  begin_element, end_element, exclude_end = period.begin, period.end, period.exclude_end?
168
174
  return period if self === begin_element && self === end_element
169
175
 
@@ -180,11 +186,7 @@ class ULID
180
186
 
181
187
  case end_element
182
188
  when Time
183
- if exclude_end
184
- end_ulid = min(end_element)
185
- else
186
- end_ulid = max(end_element)
187
- end
189
+ end_ulid = exclude_end ? min(end_element) : max(end_element)
188
190
  when nil
189
191
  # The end should be max and include end, because nil end means to cover endless ULIDs until the limit
190
192
  end_ulid = MAX
@@ -335,7 +337,7 @@ class ULID
335
337
  n32encoded_randomness = entropy.to_s(32).rjust(RANDOMNESS_ENCODED_LENGTH, '0')
336
338
  integer = (n32encoded_timestamp + n32encoded_randomness).to_i(32)
337
339
 
338
- new milliseconds: milliseconds, entropy: entropy, integer: integer
340
+ new(milliseconds: milliseconds, entropy: entropy, integer: integer)
339
341
  end
340
342
 
341
343
  attr_reader :milliseconds, :entropy
@@ -406,7 +408,7 @@ class ULID
406
408
  def octets
407
409
  digits = @integer.digits(256)
408
410
  (OCTETS_LENGTH - digits.size).times do
409
- digits.push 0
411
+ digits.push(0)
410
412
  end
411
413
  digits.reverse!
412
414
  end
@@ -477,6 +479,20 @@ class ULID
477
479
  super
478
480
  end
479
481
 
482
+ # @api private
483
+ # @return [Integer]
484
+ def marshal_dump
485
+ @integer
486
+ end
487
+
488
+ # @api private
489
+ # @param [Integer] integer
490
+ # @return [void]
491
+ def marshal_load(integer)
492
+ unmarshaled = ULID.from_integer(integer)
493
+ initialize(integer: unmarshaled.to_i, milliseconds: unmarshaled.milliseconds, entropy: unmarshaled.entropy)
494
+ end
495
+
480
496
  # @return [self]
481
497
  def to_ulid
482
498
  self
@@ -1,9 +1,12 @@
1
1
  # coding: us-ascii
2
2
  # frozen_string_literal: true
3
+
3
4
  # Copyright (C) 2021 Kenichi Kamiya
4
5
 
5
6
  class ULID
6
- # Currently supporting only for `subset` for actual use-case`
7
+ # @see https://www.crockford.com/base32.html
8
+ #
9
+ # This module supporting only `subset of original crockford for actual use-case` in ULID context.
7
10
  # Original decoding spec allows other characters.
8
11
  # But I think ULID should allow `subset` of Crockford's Base32.
9
12
  # See below
@@ -46,18 +49,22 @@ class ULID
46
49
  end
47
50
  end.freeze
48
51
  raise SetupError, 'obvious bug exists in the mapping algorithm' unless N32_CHAR_BY_CROCKFORD_BASE32_CHAR.keys == crockford_base32_mappings.keys
52
+
49
53
  CROCKFORD_BASE32_CHAR_PATTERN = /[#{N32_CHAR_BY_CROCKFORD_BASE32_CHAR.keys.join}]/.freeze
50
54
 
51
55
  CROCKFORD_BASE32_CHAR_BY_N32_CHAR = N32_CHAR_BY_CROCKFORD_BASE32_CHAR.invert.freeze
52
56
  N32_CHAR_PATTERN = /[#{CROCKFORD_BASE32_CHAR_BY_N32_CHAR.keys.join}]/.freeze
53
57
 
54
- VARIANT_BY_STANDARD = {
58
+ STANDARD_BY_VARIANT = {
55
59
  'L' => '1',
60
+ 'l' => '1',
56
61
  'I' => '1',
62
+ 'i' => '1',
57
63
  'O' => '0',
64
+ 'o' => '0',
58
65
  '-' => ''
59
66
  }.freeze
60
- VARIANT_PATTERN = /[#{VARIANT_BY_STANDARD.keys.join}]/.freeze
67
+ VARIANT_PATTERN = /[#{STANDARD_BY_VARIANT.keys.join}]/.freeze
61
68
 
62
69
  # @api private
63
70
  # @param [String] string
@@ -79,7 +86,7 @@ class ULID
79
86
  # @param [String] string
80
87
  # @return [String]
81
88
  def self.normalize(string)
82
- string.upcase.gsub(VARIANT_PATTERN, VARIANT_BY_STANDARD)
89
+ string.gsub(VARIANT_PATTERN, STANDARD_BY_VARIANT)
83
90
  end
84
91
  end
85
92
  end
@@ -1,5 +1,6 @@
1
1
  # coding: us-ascii
2
2
  # frozen_string_literal: true
3
+
3
4
  # Copyright (C) 2021 Kenichi Kamiya
4
5
 
5
6
  class ULID
@@ -36,19 +37,23 @@ class ULID
36
37
 
37
38
  milliseconds = ULID.milliseconds_from_moment(moment)
38
39
 
39
- ulid = if @prev.milliseconds < milliseconds
40
- ULID.generate(moment: milliseconds)
41
- else
42
- ULID.from_milliseconds_and_entropy(milliseconds: @prev.milliseconds, entropy: @prev.entropy.succ)
43
- end
40
+ ulid = (
41
+ if @prev.milliseconds < milliseconds
42
+ ULID.generate(moment: milliseconds)
43
+ else
44
+ ULID.from_milliseconds_and_entropy(milliseconds: @prev.milliseconds, entropy: @prev.entropy.succ)
45
+ end
46
+ )
44
47
 
45
48
  unless ulid > @prev
46
49
  base_message = "monotonicity broken from unexpected reasons # generated: #{ulid.inspect}, prev: #{@prev.inspect}"
47
- additional_information = if Thread.list == [Thread.main]
48
- '# NOTE: looks single thread only exist'
49
- else
50
- '# NOTE: ran on multi threads, so this might from concurrency issue'
51
- end
50
+ additional_information = (
51
+ if Thread.list == [Thread.main]
52
+ '# NOTE: looks single thread only exist'
53
+ else
54
+ '# NOTE: ran on multi threads, so this might from concurrency issue'
55
+ end
56
+ )
52
57
 
53
58
  raise UnexpectedError, base_message + additional_information
54
59
  end
data/lib/ulid/uuid.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # coding: us-ascii
2
2
  # frozen_string_literal: true
3
+
3
4
  # Copyright (C) 2021 Kenichi Kamiya
4
5
 
5
6
  # Extracted features around UUID from some reasons
@@ -8,7 +9,7 @@
8
9
  # * https://github.com/kachick/ruby-ulid/issues/76
9
10
  class ULID
10
11
  # Imported from https://stackoverflow.com/a/38191104/1212807, thank you!
11
- UUIDV4_PATTERN = /\A[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\z/i.freeze
12
+ UUIDV4_PATTERN = /\A[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}\z/i.freeze
12
13
  private_constant :UUIDV4_PATTERN
13
14
 
14
15
  # @param [String, #to_str] uuid
data/lib/ulid/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class ULID
5
- VERSION = '0.1.4'
5
+ VERSION = '0.1.5'
6
6
  end
data/sig/ulid.rbs CHANGED
@@ -43,7 +43,7 @@ class ULID < Object
43
43
  CROCKFORD_BASE32_CHAR_PATTERN: Regexp
44
44
  CROCKFORD_BASE32_CHAR_BY_N32_CHAR: Hash[String, String]
45
45
  N32_CHAR_PATTERN: Regexp
46
- VARIANT_BY_STANDARD: Hash[String, String]
46
+ STANDARD_BY_VARIANT: Hash[String, String]
47
47
  VARIANT_PATTERN: Regexp
48
48
 
49
49
  # A pribate API. Should not be used in your code.
@@ -143,7 +143,7 @@ class ULID < Object
143
143
  #
144
144
  # If you want to keep sortable even if in same timestamp, See also [ULID::MonotonicGenerator](https://github.com/kachick/ruby-ulid#how-to-keep-sortable-even-if-in-same-timestamp)
145
145
  #
146
- def self.generate: (?moment: moment, ?entropy: Integer) -> self
146
+ def self.generate: (?moment: moment, ?entropy: Integer) -> ULID
147
147
 
148
148
  # Shorthand of `ULID.generate(moment: Time)`
149
149
  # See also [ULID.generate](https://kachick.github.io/ruby-ulid/ULID.html#generate-class_method)
@@ -157,7 +157,7 @@ class ULID < Object
157
157
  # end
158
158
  # ulids.sort == ulids #=> true
159
159
  # ```
160
- def self.at: (Time time) -> self
160
+ def self.at: (Time time) -> ULID
161
161
 
162
162
  # A pribate API. Should not be used in your code.
163
163
  def self.current_milliseconds: -> Integer
@@ -212,7 +212,7 @@ class ULID < Object
212
212
  # ulid = ULID.parse('01ARZ3NDEKTSV4RRFFQ69G5FAV')
213
213
  # #=> ULID(2016-07-30 23:54:10.259 UTC: 01ARZ3NDEKTSV4RRFFQ69G5FAV)
214
214
  # ```
215
- def self.parse: (_ToStr string) -> self
215
+ def self.parse: (_ToStr string) -> ULID
216
216
 
217
217
  # ```ruby
218
218
  # # Currently experimental feature, so needed to load the extension.
@@ -226,7 +226,7 @@ class ULID < Object
226
226
  #
227
227
  # See also [Why this is experimental?](https://github.com/kachick/ruby-ulid/issues/76)
228
228
  def self.from_uuidv4: (String uuid) -> ULID
229
- def self.from_integer: (Integer integer) -> self
229
+ def self.from_integer: (Integer integer) -> ULID
230
230
 
231
231
  # Returns termination values for ULID spec.
232
232
  #
@@ -318,8 +318,8 @@ class ULID < Object
318
318
  # # ULID(2021-04-28 16:49:06.484 UTC: 01F4CP4PDM214Q6H3KJP7DYJRR),
319
319
  # # ULID(2021-04-28 15:05:06.808 UTC: 01F4CG68ZRST94T056KRZ5K9S4)]
320
320
  # ```
321
- def self.sample: (?period: period) -> self
322
- | (Integer number, ?period: period) -> Array[self]
321
+ def self.sample: (?period: period) -> ULID
322
+ | (Integer number, ?period: period) -> Array[ULID]
323
323
  def self.valid?: (untyped) -> bool
324
324
 
325
325
  # Returns normalized string
@@ -384,8 +384,8 @@ class ULID < Object
384
384
  # # ULID(2021-04-30 05:53:12.478 UTC: 01F4GND4RYYSKNAADHQ9BNXAWJ)]
385
385
  # ```
386
386
  def self.scan: (_ToStr string) -> Enumerator[self, singleton(ULID)]
387
- | (_ToStr string) { (self ulid) -> void } -> singleton(ULID)
388
- def self.from_milliseconds_and_entropy: (milliseconds: Integer, entropy: Integer) -> self
387
+ | (_ToStr string) { (ULID ulid) -> void } -> singleton(ULID)
388
+ def self.from_milliseconds_and_entropy: (milliseconds: Integer, entropy: Integer) -> ULID
389
389
  def self.try_convert: (_ToULID) -> ULID
390
390
  | (untyped) -> nil
391
391
 
@@ -507,6 +507,12 @@ class ULID < Object
507
507
  def pred: -> ULID?
508
508
  def freeze: -> self
509
509
 
510
+ # A pribate API. Should not be used in your code.
511
+ def marshal_dump: -> Integer
512
+
513
+ # A pribate API. Should not be used in your code.
514
+ def marshal_load: (Integer integer) -> void
515
+
510
516
  # Returns `self`
511
517
  def to_ulid: -> self
512
518
 
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.1.4
4
+ version: 0.1.5
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-17 00:00:00.000000000 Z
11
+ date: 2021-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -130,7 +130,7 @@ dependencies:
130
130
  requirements:
131
131
  - - ">="
132
132
  - !ruby/object:Gem::Version
133
- version: 2.8.4
133
+ version: 2.9.1
134
134
  - - "<"
135
135
  - !ruby/object:Gem::Version
136
136
  version: '3'
@@ -140,7 +140,7 @@ dependencies:
140
140
  requirements:
141
141
  - - ">="
142
142
  - !ruby/object:Gem::Version
143
- version: 2.8.4
143
+ version: 2.9.1
144
144
  - - "<"
145
145
  - !ruby/object:Gem::Version
146
146
  version: '3'
@@ -190,76 +190,100 @@ dependencies:
190
190
  requirements:
191
191
  - - ">="
192
192
  - !ruby/object:Gem::Version
193
- version: 1.14.0
193
+ version: 1.15.0
194
194
  - - "<"
195
195
  - !ruby/object:Gem::Version
196
- version: 1.15.0
196
+ version: 1.16.0
197
197
  type: :development
198
198
  prerelease: false
199
199
  version_requirements: !ruby/object:Gem::Requirement
200
200
  requirements:
201
201
  - - ">="
202
202
  - !ruby/object:Gem::Version
203
- version: 1.14.0
203
+ version: 1.15.0
204
204
  - - "<"
205
205
  - !ruby/object:Gem::Version
206
- version: 1.15.0
206
+ version: 1.16.0
207
207
  - !ruby/object:Gem::Dependency
208
208
  name: rubocop-rake
209
209
  requirement: !ruby/object:Gem::Requirement
210
210
  requirements:
211
211
  - - ">="
212
212
  - !ruby/object:Gem::Version
213
- version: '0'
213
+ version: 0.5.1
214
+ - - "<"
215
+ - !ruby/object:Gem::Version
216
+ version: 0.6.0
214
217
  type: :development
215
218
  prerelease: false
216
219
  version_requirements: !ruby/object:Gem::Requirement
217
220
  requirements:
218
221
  - - ">="
219
222
  - !ruby/object:Gem::Version
220
- version: '0'
223
+ version: 0.5.1
224
+ - - "<"
225
+ - !ruby/object:Gem::Version
226
+ version: 0.6.0
221
227
  - !ruby/object:Gem::Dependency
222
228
  name: rubocop-performance
223
229
  requirement: !ruby/object:Gem::Requirement
224
230
  requirements:
225
231
  - - ">="
226
232
  - !ruby/object:Gem::Version
227
- version: '0'
233
+ version: 1.11.3
234
+ - - "<"
235
+ - !ruby/object:Gem::Version
236
+ version: 1.12.0
228
237
  type: :development
229
238
  prerelease: false
230
239
  version_requirements: !ruby/object:Gem::Requirement
231
240
  requirements:
232
241
  - - ">="
233
242
  - !ruby/object:Gem::Version
234
- version: '0'
243
+ version: 1.11.3
244
+ - - "<"
245
+ - !ruby/object:Gem::Version
246
+ version: 1.12.0
235
247
  - !ruby/object:Gem::Dependency
236
248
  name: rubocop-rubycw
237
249
  requirement: !ruby/object:Gem::Requirement
238
250
  requirements:
239
251
  - - ">="
240
252
  - !ruby/object:Gem::Version
241
- version: '0'
253
+ version: 0.1.6
254
+ - - "<"
255
+ - !ruby/object:Gem::Version
256
+ version: 0.2.0
242
257
  type: :development
243
258
  prerelease: false
244
259
  version_requirements: !ruby/object:Gem::Requirement
245
260
  requirements:
246
261
  - - ">="
247
262
  - !ruby/object:Gem::Version
248
- version: '0'
263
+ version: 0.1.6
264
+ - - "<"
265
+ - !ruby/object:Gem::Version
266
+ version: 0.2.0
249
267
  - !ruby/object:Gem::Dependency
250
268
  name: rubocop-md
251
269
  requirement: !ruby/object:Gem::Requirement
252
270
  requirements:
253
271
  - - ">="
254
272
  - !ruby/object:Gem::Version
255
- version: '0'
273
+ version: 1.0.1
274
+ - - "<"
275
+ - !ruby/object:Gem::Version
276
+ version: 2.0.0
256
277
  type: :development
257
278
  prerelease: false
258
279
  version_requirements: !ruby/object:Gem::Requirement
259
280
  requirements:
260
281
  - - ">="
261
282
  - !ruby/object:Gem::Version
262
- version: '0'
283
+ version: 1.0.1
284
+ - - "<"
285
+ - !ruby/object:Gem::Version
286
+ version: 2.0.0
263
287
  description: |2
264
288
  The ULID(Universally Unique Lexicographically Sortable Identifier) has useful specs for applications (e.g. `Database key`), especially possess all `uniqueness`, `randomness`, `extractable timestamps` and `sortable` features.
265
289
  This gem aims to provide the generator, monotonic generator, parser and handy manipulation features around the ULID.
@@ -270,7 +294,7 @@ executables: []
270
294
  extensions: []
271
295
  extra_rdoc_files: []
272
296
  files:
273
- - LICENSE
297
+ - LICENSE.txt
274
298
  - README.md
275
299
  - lib/ulid.rb
276
300
  - lib/ulid/crockford_base32.rb
@@ -285,6 +309,7 @@ metadata:
285
309
  documentation_uri: https://kachick.github.io/ruby-ulid/
286
310
  homepage_uri: https://github.com/kachick/ruby-ulid
287
311
  source_code_uri: https://github.com/kachick/ruby-ulid
312
+ bug_tracker_uri: https://github.com/kachick/ruby-ulid/issues
288
313
  post_install_message:
289
314
  rdoc_options: []
290
315
  require_paths:
@@ -303,5 +328,5 @@ requirements: []
303
328
  rubygems_version: 3.2.15
304
329
  signing_key:
305
330
  specification_version: 4
306
- summary: A handy ULID library
331
+ summary: Useful ULID library
307
332
  test_files: []