ruby-ulid 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +62 -51
- data/lib/ulid.rb +14 -4
- data/lib/ulid/version.rb +1 -1
- data/sig/ulid.rbs +2 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91f6d4c9dad8e12663686099c487dbbf3ec5b7995e0e4b2b210ac8a16fbcd91c
|
4
|
+
data.tar.gz: 1ca6e29d83771636b2a20aa71d90f15699d07d6a7a75f22c306094507bb51d89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22dcc2541f7e4fa1a1d4014f996aef3d3bfcb69b002c1cc6ca4864e17bce50a327e1ec98453b4dab0773400b678055dc024a7287342a08916ce30772aadb418d
|
7
|
+
data.tar.gz: 27c69c7319858a4c732f04f6944ff180f3dfc9a5df12bd67cd1e4cac91421a2bc00d1a79c8ff91190b425731f831fdaef1a2f5bc0c3ef4fdaff73a6ce6749e35
|
data/README.md
CHANGED
@@ -3,10 +3,8 @@
|
|
3
3
|
A handy `ULID` library
|
4
4
|
|
5
5
|
The `ULID` spec is defined on [ulid/spec](https://github.com/ulid/spec).
|
6
|
-
|
7
|
-
|
8
|
-
This gem aims to provide the generator, monotonic generator, parser and handy manipulation methods for the ID.
|
9
|
-
Also having rbs signature files.
|
6
|
+
This gem aims to provide the generator, monotonic generator, parser and handy manipulation features around the ULID.
|
7
|
+
Also providing rbs signature files.
|
10
8
|
|
11
9
|
---
|
12
10
|
|
@@ -57,13 +55,34 @@ ulid.octets #=> [1, 121, 20, 95, 7, 202, 187, 60, 175, 51, 76, 60, 49, 73, 37, 7
|
|
57
55
|
ulid.pattern #=> /(?<timestamp>01F4A5Y1YA)(?<randomness>QCYAYCTC7GRMJ9AA)/i
|
58
56
|
```
|
59
57
|
|
58
|
+
Generator can take `Time` instance
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
time = Time.at(946684800, in: 'UTC') #=> 2000-01-01 00:00:00 UTC
|
62
|
+
ULID.generate(moment: time) #=> ULID(2000-01-01 00:00:00.000 UTC: 00VHNCZB00N018DCPJA4H9379P)
|
63
|
+
ULID.generate(moment: time) #=> ULID(2000-01-01 00:00:00.000 UTC: 00VHNCZB006WQT3JTMN0T14EBP)
|
64
|
+
|
65
|
+
ulids = 1000.times.map do
|
66
|
+
ULID.generate(moment: time)
|
67
|
+
end
|
68
|
+
ulids.sort == ulids #=> false
|
69
|
+
|
70
|
+
ulids = 1000.times.map do |n|
|
71
|
+
ULID.generate(moment: time + n)
|
72
|
+
end
|
73
|
+
ulids.sort == ulids #=> true
|
74
|
+
```
|
75
|
+
|
60
76
|
You can parse from exists IDs
|
61
77
|
|
78
|
+
FYI: Current parser/validator/matcher implementation aims `strict`, It might be changed in [ulid/spec#57](https://github.com/ulid/spec/pull/57) and [ruby-ulid#57](https://github.com/kachick/ruby-ulid/issues/57).
|
79
|
+
|
62
80
|
```ruby
|
63
81
|
ulid = ULID.parse('01ARZ3NDEKTSV4RRFFQ69G5FAV') #=> ULID(2016-07-30 23:54:10.259 UTC: 01ARZ3NDEKTSV4RRFFQ69G5FAV)
|
82
|
+
ulid.to_time #=> 2016-07-30 23:54:10.259 UTC
|
64
83
|
```
|
65
84
|
|
66
|
-
|
85
|
+
ULIDs are sortable when they are generated in different timestamp with milliseconds precision
|
67
86
|
|
68
87
|
```ruby
|
69
88
|
ulids = 1000.times.map do
|
@@ -74,29 +93,40 @@ ulids.sort == ulids #=> true
|
|
74
93
|
ulids.uniq(&:to_time).size #=> 1000
|
75
94
|
```
|
76
95
|
|
77
|
-
|
96
|
+
The basic generator prefers `randomness`, it does not guarantee `sortable` for same milliseconds ULIDs.
|
78
97
|
|
79
98
|
```ruby
|
80
99
|
ulids = 10000.times.map do
|
81
100
|
ULID.generate
|
82
101
|
end
|
83
|
-
ulids.uniq(&:to_time).size #=> 35 (the
|
102
|
+
ulids.uniq(&:to_time).size #=> 35 (the size is not fixed, might be changed in environment)
|
84
103
|
ulids.sort == ulids #=> false
|
104
|
+
```
|
85
105
|
|
106
|
+
If you want to prefer `sortable` rather than the `strict randomness`, Use `MonotonicGenerator` instead. It is called as [Monotonicity](https://github.com/ulid/spec/tree/d0c7170df4517939e70129b4d6462cc162f2d5bf#monotonicity) on the spec.
|
107
|
+
(Though it starts with new random value when changed the timestamp)
|
86
108
|
|
109
|
+
```ruby
|
87
110
|
monotonic_generator = ULID::MonotonicGenerator.new
|
88
111
|
monotonic_ulids = 10000.times.map do
|
89
112
|
monotonic_generator.generate
|
90
113
|
end
|
91
|
-
monotonic_ulids.uniq(&:to_time)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
114
|
+
sample_ulids_by_the_time = monotonic_ulids.uniq(&:to_time)
|
115
|
+
sample_ulids_by_the_time.size #=> 34 (the size is not fixed, might be changed in environment)
|
116
|
+
sample_ulids_by_the_time.take(10).map(&:randomness)
|
117
|
+
#=>
|
118
|
+
["JZW56CTA8704D5AQ",
|
119
|
+
"JGEBH2A2B2EA97MW",
|
120
|
+
"0XPE4NS3MZH0NAJ4",
|
121
|
+
"E0S3ZAVADFBPW57Y",
|
122
|
+
"E5CX1T6281443THQ",
|
123
|
+
"3SK8WHSH03CVF7J2",
|
124
|
+
"DDS35BT0R20P3V49",
|
125
|
+
"60KG2W9FVEN1ZX8C",
|
126
|
+
"X59YJVXXVH7AXJJE",
|
127
|
+
"1ZBQ7SNGFKXGH1Y4"]
|
96
128
|
|
97
|
-
|
98
|
-
ULID.from_uuidv4('0983d0a2-ff15-4d83-8f37-7dd945b5aa39')
|
99
|
-
#=> ULID(2301-07-10 00:28:28.821 UTC: 09GF8A5ZRN9P1RYDVXV52VBAHS)
|
129
|
+
monotonic_ulids.sort == monotonic_ulids #=> true
|
100
130
|
```
|
101
131
|
|
102
132
|
For rough operations, `ULID.scan` might be useful.
|
@@ -150,50 +180,31 @@ ULID.min(moment: time) #=> ULID(2000-01-01 00:00:00.123 UTC: 00VHNCZB3V000000000
|
|
150
180
|
ULID.max(moment: time) #=> ULID(2000-01-01 00:00:00.123 UTC: 00VHNCZB3VZZZZZZZZZZZZZZZZ)
|
151
181
|
```
|
152
182
|
|
153
|
-
|
154
|
-
|
155
|
-
At first, you should install development dependencies
|
156
|
-
|
157
|
-
```console
|
158
|
-
$ git clone git@github.com:kachick/ruby-ulid.git
|
159
|
-
$ cd ./ruby-ulid
|
160
|
-
$ bundle install
|
161
|
-
# Executing first time might take longtime, because development mode dependent active_support via steep
|
162
|
-
```
|
163
|
-
|
164
|
-
Play with the behaviors in REPL.
|
165
|
-
|
166
|
-
```console
|
167
|
-
$ ./bin/console
|
168
|
-
# Starting up IRB with loading developing ULID library
|
169
|
-
irb(main):001:0>
|
170
|
-
```
|
183
|
+
`ULID#next` and `ULID#succ` returns next(successor) ULID
|
171
184
|
|
172
185
|
```ruby
|
173
|
-
|
174
|
-
|
175
|
-
|
186
|
+
ULID.parse('01BX5ZZKBKZZZZZZZZZZZZZZZY').next.to_s #=> "01BX5ZZKBKZZZZZZZZZZZZZZZZ"
|
187
|
+
ULID.parse('01BX5ZZKBKZZZZZZZZZZZZZZZZ').next.to_s #=> "01BX5ZZKBM0000000000000000"
|
188
|
+
ULID.parse('7ZZZZZZZZZZZZZZZZZZZZZZZZZ').next #=> nil
|
176
189
|
```
|
177
190
|
|
178
|
-
|
191
|
+
`ULID#pred` returns predecessor ULID
|
179
192
|
|
180
|
-
```
|
181
|
-
|
182
|
-
|
183
|
-
|
193
|
+
```ruby
|
194
|
+
ULID.parse('01BX5ZZKBK0000000000000001').pred.to_s #=> "01BX5ZZKBK0000000000000000"
|
195
|
+
ULID.parse('01BX5ZZKBK0000000000000000').pred.to_s #=> "01BX5ZZKBJZZZZZZZZZZZZZZZZ"
|
196
|
+
ULID.parse('00000000000000000000000000').pred #=> nil
|
184
197
|
```
|
185
198
|
|
186
|
-
|
199
|
+
UUIDv4 converter for migration use-cases. (Of course the timestamp will be useless one. Sortable benefit is lost.)
|
187
200
|
|
188
|
-
```
|
189
|
-
|
190
|
-
|
201
|
+
```ruby
|
202
|
+
ULID.from_uuidv4('0983d0a2-ff15-4d83-8f37-7dd945b5aa39')
|
203
|
+
#=> ULID(2301-07-10 00:28:28.821 UTC: 09GF8A5ZRN9P1RYDVXV52VBAHS)
|
191
204
|
```
|
192
205
|
|
193
|
-
##
|
194
|
-
|
195
|
-
- [API documents generated by YARD](https://kachick.github.io/ruby-ulid/)
|
196
|
-
|
197
|
-
## Author
|
206
|
+
## References
|
198
207
|
|
199
|
-
|
208
|
+
- [API documents](https://kachick.github.io/ruby-ulid/)
|
209
|
+
- [ulid/spec](https://github.com/ulid/spec)
|
210
|
+
- [Another choices are UUIDv6, UUIDv7, UUIDv8. But they are still in draft state](https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html)
|
data/lib/ulid.rb
CHANGED
@@ -107,6 +107,7 @@ class ULID
|
|
107
107
|
# @return [ULID]
|
108
108
|
# @raise [OverflowError] if the given integer is larger than the ULID limit
|
109
109
|
# @raise [ArgumentError] if the given integer is negative number
|
110
|
+
# @todo Need optimized for performance
|
110
111
|
def self.from_integer(integer)
|
111
112
|
integer = integer.to_int
|
112
113
|
raise OverflowError, "integer overflow: given #{integer}, max: #{MAX_INTEGER}" unless integer <= MAX_INTEGER
|
@@ -292,20 +293,29 @@ class ULID
|
|
292
293
|
@strict_pattern ||= /\A#{pattern.source}\z/i.freeze
|
293
294
|
end
|
294
295
|
|
295
|
-
# @
|
296
|
-
# @return [ULID]
|
296
|
+
# @return [ULID, nil] when called on ULID as `7ZZZZZZZZZZZZZZZZZZZZZZZZZ`, returns `nil` instead of ULID
|
297
297
|
def next
|
298
|
-
|
298
|
+
next_int = to_i.next
|
299
|
+
return nil if next_int > MAX_INTEGER
|
300
|
+
@next ||= self.class.from_integer(next_int)
|
299
301
|
end
|
300
302
|
alias_method :succ, :next
|
301
303
|
|
304
|
+
# @return [ULID, nil] when called on ULID as `00000000000000000000000000`, returns `nil` instead of ULID
|
305
|
+
def pred
|
306
|
+
pre_int = to_i.pred
|
307
|
+
return nil if pre_int.negative?
|
308
|
+
@pred ||= self.class.from_integer(pre_int)
|
309
|
+
end
|
310
|
+
|
302
311
|
# @return [self]
|
303
312
|
def freeze
|
304
313
|
# Evaluate all caching
|
305
314
|
inspect
|
306
315
|
octets
|
307
|
-
succ
|
308
316
|
to_i
|
317
|
+
succ
|
318
|
+
pred
|
309
319
|
strict_pattern
|
310
320
|
super
|
311
321
|
end
|
data/lib/ulid/version.rb
CHANGED
data/sig/ulid.rbs
CHANGED
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.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenichi Kamiya
|
@@ -70,11 +70,10 @@ dependencies:
|
|
70
70
|
- - "<"
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '2'
|
73
|
-
description:
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
Also having rbs signature files.
|
73
|
+
description: " ULID(Universally Unique Lexicographically Sortable Identifier) has
|
74
|
+
useful specs for applications (e.g. `Database key`). \n This gem aims to provide
|
75
|
+
the generator, monotonic generator, parser and handy manipulation features around
|
76
|
+
the ULID.\n Also providing `rbs` signature files.\n"
|
78
77
|
email:
|
79
78
|
- kachick1+ruby@gmail.com
|
80
79
|
executables: []
|