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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ulid.rb +30 -11
  3. data/lib/ulid/version.rb +1 -1
  4. data/sig/ulid.rbs +44 -40
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f33ff714eef2b217cb557fe4ae46087a77b834aa42c406e940f2f8a6630464c
4
- data.tar.gz: ce004faa759110d8494f34a058230a90d7231af4a309c5c7bf66b0f0963bcf81
3
+ metadata.gz: a1179379859a437f04ff904c941372fffc4aa3027d550ae50c67f744e4899d9a
4
+ data.tar.gz: 71781ee8481373bb7229f10f88a1703b2653c11a4f18cbc7b50eb4adbec58908
5
5
  SHA512:
6
- metadata.gz: d60377fcf5cd9523b09eb78be1feef0fb996b9f6ebf0e430db9bf3902899ee6e6e6d62518a9f33ecfb5d6786390e08bbf545d5a9b36f141fd4a95bc7ff8f6612
7
- data.tar.gz: 981bafadfcbb63254a276a1ae9b201b2b636555416450ce421ffa0f4482e343072811623ae4856970904227ae0da0fe2f991ffc809556d4bc6a33a910446c7dd
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.instance
77
+ MONOTONIC_GENERATOR = MonotonicGenerator.new
75
78
 
76
- private_constant :ENCODING_CHARS, :TIME_FORMAT_IN_INSPECT, :MonotonicGenerator
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?(self.class) ? (to_i <=> other.to_i) : nil
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?(self.class) && other.to_i == to_i)
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<Integer>]
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<Integer>]
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<Integer>]
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
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  class ULID
5
- VERSION = '0.0.7'
5
+ VERSION = '0.0.8'
6
6
  end
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: Integer
6
- RANDOMNESS_PART_LENGTH: Integer
7
- ENCODED_ID_LENGTH: Integer
8
- TIME_OCTETS_LENGTH: Integer
9
- RANDOMNESS_OCTETS_LENGTH: Integer
10
- OCTETS_LENGTH: Integer
11
- MAX_MILLISECONDS: Integer
12
- MAX_ENTROPY: Integer
13
- TIME_FORMAT_IN_INSPECT: String
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: Array[Integer]
19
- @time_octets: Array[Integer]
20
- @randomness_octets: Array[Integer]
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
- def to_s: -> String
59
+ alias to_s to_str
37
60
  def to_i: -> Integer
38
- def hash: -> Integer
39
- def <=>: (untyped other) -> Integer?
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
- def ==: (untyped other) -> bool
66
+ alias == eql?
43
67
  def ===: (untyped other) -> bool
44
68
  def to_time: -> Time
45
- def octets: -> Array[Integer]
46
- def time_octets: -> Array[Integer]
47
- def randomness_octets: -> Array[Integer]
69
+ def octets: -> octets
70
+ def time_octets: -> time_octets
71
+ def randomness_octets: -> randomness_octets
48
72
  def next: -> ULID
49
- def succ: -> ULID
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-ulid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenichi Kamiya