lite-uxid 1.4.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +129 -91
- data/README.md +36 -3
- data/lib/generators/lite/uxid/templates/install.rb +1 -0
- data/lib/lite/uxid/base/irreversible.rb +82 -0
- data/lib/lite/uxid/base/reversible.rb +36 -0
- data/lib/lite/uxid/configuration.rb +5 -1
- data/lib/lite/uxid/hashid.rb +5 -3
- data/lib/lite/uxid/nanoid.rb +4 -2
- data/lib/lite/uxid/record/hashid.rb +10 -4
- data/lib/lite/uxid/record/nanoid.rb +7 -2
- data/lib/lite/uxid/record/ulid.rb +2 -1
- data/lib/lite/uxid/record/uuid.rb +2 -1
- data/lib/lite/uxid/ulid.rb +1 -1
- data/lib/lite/uxid/uuid.rb +7 -2
- data/lib/lite/uxid/version.rb +1 -1
- data/lib/lite/uxid.rb +2 -2
- metadata +5 -5
- data/lib/lite/uxid/irreversible.rb +0 -71
- data/lib/lite/uxid/reversible.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c35d2426c2fffc91f8195a96c94ff26df0c41ffdb79041c5499952c93ccd240
|
4
|
+
data.tar.gz: 5a72d11d4fc6d3cec87411a623d072dec4ba1f17b810814b15c543da3a512d7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a84caf603e6393456ac15d30afaf23fda68eec1480997ce64a54339abefcfc5fadf1afe4fb505f6b3f54119188e3f3405e040771e4a76cc4cae3f8b7f842693
|
7
|
+
data.tar.gz: 0d345ae245719fcfe669230925aee58feab58153c978c6089379cc750ae417325d98bd6b660aba695e2df85d540d88a543bfd5a11ce88556fb2ef835f81a860d
|
data/.rubocop.yml
CHANGED
@@ -6,6 +6,8 @@ AllCops:
|
|
6
6
|
NewCops: enable
|
7
7
|
DisplayCopNames: true
|
8
8
|
DisplayStyleGuide: true
|
9
|
+
Gemspec/DevelopmentDependencies:
|
10
|
+
EnforcedStyle: gemspec
|
9
11
|
Gemspec/RequiredRubyVersion:
|
10
12
|
Enabled: false
|
11
13
|
Layout/EmptyLinesAroundAttributeAccessor:
|
@@ -33,6 +35,8 @@ Metrics/MethodLength:
|
|
33
35
|
Enabled: false
|
34
36
|
RSpec/MultipleExpectations:
|
35
37
|
Enabled: false
|
38
|
+
RSpec/MultipleMemoizedHelpers:
|
39
|
+
Enabled: false
|
36
40
|
Style/ArgumentsForwarding:
|
37
41
|
Enabled: false
|
38
42
|
Style/Documentation:
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.5.1] - 2024-09-20
|
10
|
+
### Changed
|
11
|
+
- Use after commit on create instead of after create
|
12
|
+
|
13
|
+
## [1.5.0] - 2024-09-20
|
14
|
+
### Added
|
15
|
+
- Added uuid version option
|
16
|
+
- Added prefix option to hashid and nanoid
|
17
|
+
### Changed
|
18
|
+
- Move reversible and irreversible files to base namespace
|
19
|
+
- Ruby send perf improvements
|
20
|
+
|
9
21
|
## [1.4.0] - 2022-11-20
|
10
22
|
### Changed
|
11
23
|
- Improved global config flexibility
|
data/Gemfile.lock
CHANGED
@@ -1,131 +1,169 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lite-uxid (1.
|
4
|
+
lite-uxid (1.5.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
actionpack (7.
|
10
|
-
actionview (= 7.
|
11
|
-
activesupport (= 7.
|
12
|
-
|
9
|
+
actionpack (7.2.1)
|
10
|
+
actionview (= 7.2.1)
|
11
|
+
activesupport (= 7.2.1)
|
12
|
+
nokogiri (>= 1.8.5)
|
13
|
+
racc
|
14
|
+
rack (>= 2.2.4, < 3.2)
|
15
|
+
rack-session (>= 1.0.1)
|
13
16
|
rack-test (>= 0.6.3)
|
14
|
-
rails-dom-testing (~> 2.
|
15
|
-
rails-html-sanitizer (~> 1.
|
16
|
-
|
17
|
-
|
17
|
+
rails-dom-testing (~> 2.2)
|
18
|
+
rails-html-sanitizer (~> 1.6)
|
19
|
+
useragent (~> 0.16)
|
20
|
+
actionview (7.2.1)
|
21
|
+
activesupport (= 7.2.1)
|
18
22
|
builder (~> 3.1)
|
19
|
-
erubi (~> 1.
|
20
|
-
rails-dom-testing (~> 2.
|
21
|
-
rails-html-sanitizer (~> 1.
|
22
|
-
activemodel (7.
|
23
|
-
activesupport (= 7.
|
24
|
-
activerecord (7.
|
25
|
-
activemodel (= 7.
|
26
|
-
activesupport (= 7.
|
27
|
-
|
28
|
-
|
23
|
+
erubi (~> 1.11)
|
24
|
+
rails-dom-testing (~> 2.2)
|
25
|
+
rails-html-sanitizer (~> 1.6)
|
26
|
+
activemodel (7.2.1)
|
27
|
+
activesupport (= 7.2.1)
|
28
|
+
activerecord (7.2.1)
|
29
|
+
activemodel (= 7.2.1)
|
30
|
+
activesupport (= 7.2.1)
|
31
|
+
timeout (>= 0.4.0)
|
32
|
+
activesupport (7.2.1)
|
33
|
+
base64
|
34
|
+
bigdecimal
|
35
|
+
concurrent-ruby (~> 1.0, >= 1.3.1)
|
36
|
+
connection_pool (>= 2.2.5)
|
37
|
+
drb
|
29
38
|
i18n (>= 1.6, < 2)
|
39
|
+
logger (>= 1.4.2)
|
30
40
|
minitest (>= 5.1)
|
31
|
-
|
41
|
+
securerandom (>= 0.3)
|
42
|
+
tzinfo (~> 2.0, >= 2.0.5)
|
32
43
|
ast (2.4.2)
|
33
|
-
|
34
|
-
|
35
|
-
|
44
|
+
base64 (0.2.0)
|
45
|
+
bigdecimal (3.1.8)
|
46
|
+
builder (3.3.0)
|
47
|
+
concurrent-ruby (1.3.4)
|
48
|
+
connection_pool (2.4.1)
|
36
49
|
crass (1.0.6)
|
37
|
-
database_cleaner (2.0.
|
38
|
-
database_cleaner-active_record (
|
39
|
-
database_cleaner-active_record (2.0
|
50
|
+
database_cleaner (2.0.2)
|
51
|
+
database_cleaner-active_record (>= 2, < 3)
|
52
|
+
database_cleaner-active_record (2.2.0)
|
40
53
|
activerecord (>= 5.a)
|
41
54
|
database_cleaner-core (~> 2.0.0)
|
42
55
|
database_cleaner-core (2.0.1)
|
43
|
-
diff-lcs (1.5.
|
44
|
-
|
45
|
-
|
46
|
-
|
56
|
+
diff-lcs (1.5.1)
|
57
|
+
drb (2.2.1)
|
58
|
+
erubi (1.13.0)
|
59
|
+
fasterer (0.11.0)
|
47
60
|
ruby_parser (>= 3.19.1)
|
48
|
-
generator_spec (0.
|
61
|
+
generator_spec (0.10.0)
|
49
62
|
activesupport (>= 3.0.0)
|
50
63
|
railties (>= 3.0.0)
|
51
|
-
i18n (1.
|
64
|
+
i18n (1.14.6)
|
52
65
|
concurrent-ruby (~> 1.0)
|
53
|
-
|
54
|
-
|
66
|
+
io-console (0.7.2)
|
67
|
+
irb (1.14.0)
|
68
|
+
rdoc (>= 4.0.0)
|
69
|
+
reline (>= 0.4.2)
|
70
|
+
json (2.7.2)
|
71
|
+
language_server-protocol (3.17.0.3)
|
72
|
+
logger (1.6.1)
|
73
|
+
loofah (2.22.0)
|
55
74
|
crass (~> 1.0.2)
|
56
|
-
nokogiri (>= 1.
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
mini_portile2 (~> 2.8.0)
|
75
|
+
nokogiri (>= 1.12.0)
|
76
|
+
mini_portile2 (2.8.7)
|
77
|
+
minitest (5.25.1)
|
78
|
+
nokogiri (1.16.7)
|
79
|
+
mini_portile2 (~> 2.8.2)
|
62
80
|
racc (~> 1.4)
|
63
|
-
parallel (1.
|
64
|
-
parser (3.
|
81
|
+
parallel (1.26.3)
|
82
|
+
parser (3.3.5.0)
|
65
83
|
ast (~> 2.4.1)
|
66
|
-
|
67
|
-
|
68
|
-
|
84
|
+
racc
|
85
|
+
psych (5.1.2)
|
86
|
+
stringio
|
87
|
+
racc (1.8.1)
|
88
|
+
rack (3.1.7)
|
89
|
+
rack-session (2.0.0)
|
90
|
+
rack (>= 3.0.0)
|
91
|
+
rack-test (2.1.0)
|
69
92
|
rack (>= 1.3)
|
70
|
-
|
71
|
-
|
93
|
+
rackup (2.1.0)
|
94
|
+
rack (>= 3)
|
95
|
+
webrick (~> 1.8)
|
96
|
+
rails-dom-testing (2.2.0)
|
97
|
+
activesupport (>= 5.0.0)
|
98
|
+
minitest
|
72
99
|
nokogiri (>= 1.6)
|
73
|
-
rails-html-sanitizer (1.
|
74
|
-
loofah (~> 2.
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
100
|
+
rails-html-sanitizer (1.6.0)
|
101
|
+
loofah (~> 2.21)
|
102
|
+
nokogiri (~> 1.14)
|
103
|
+
railties (7.2.1)
|
104
|
+
actionpack (= 7.2.1)
|
105
|
+
activesupport (= 7.2.1)
|
106
|
+
irb (~> 1.13)
|
107
|
+
rackup (>= 1.0.0)
|
79
108
|
rake (>= 12.2)
|
80
|
-
thor (~> 1.0)
|
81
|
-
zeitwerk (~> 2.
|
109
|
+
thor (~> 1.0, >= 1.2.2)
|
110
|
+
zeitwerk (~> 2.6)
|
82
111
|
rainbow (3.1.1)
|
83
|
-
rake (13.
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
rspec-
|
92
|
-
|
112
|
+
rake (13.2.1)
|
113
|
+
rdoc (6.7.0)
|
114
|
+
psych (>= 4.0.0)
|
115
|
+
regexp_parser (2.9.2)
|
116
|
+
reline (0.5.10)
|
117
|
+
io-console (~> 0.5)
|
118
|
+
rspec (3.13.0)
|
119
|
+
rspec-core (~> 3.13.0)
|
120
|
+
rspec-expectations (~> 3.13.0)
|
121
|
+
rspec-mocks (~> 3.13.0)
|
122
|
+
rspec-core (3.13.1)
|
123
|
+
rspec-support (~> 3.13.0)
|
124
|
+
rspec-expectations (3.13.3)
|
93
125
|
diff-lcs (>= 1.2.0, < 2.0)
|
94
|
-
rspec-support (~> 3.
|
95
|
-
rspec-mocks (3.
|
126
|
+
rspec-support (~> 3.13.0)
|
127
|
+
rspec-mocks (3.13.1)
|
96
128
|
diff-lcs (>= 1.2.0, < 2.0)
|
97
|
-
rspec-support (~> 3.
|
98
|
-
rspec-support (3.
|
99
|
-
rubocop (1.
|
129
|
+
rspec-support (~> 3.13.0)
|
130
|
+
rspec-support (3.13.1)
|
131
|
+
rubocop (1.66.1)
|
100
132
|
json (~> 2.3)
|
133
|
+
language_server-protocol (>= 3.17.0)
|
101
134
|
parallel (~> 1.10)
|
102
|
-
parser (>= 3.
|
135
|
+
parser (>= 3.3.0.2)
|
103
136
|
rainbow (>= 2.2.2, < 4.0)
|
104
|
-
regexp_parser (>=
|
105
|
-
|
106
|
-
rubocop-ast (>= 1.23.0, < 2.0)
|
137
|
+
regexp_parser (>= 2.4, < 3.0)
|
138
|
+
rubocop-ast (>= 1.32.2, < 2.0)
|
107
139
|
ruby-progressbar (~> 1.7)
|
108
|
-
unicode-display_width (>=
|
109
|
-
rubocop-ast (1.
|
110
|
-
parser (>= 3.
|
111
|
-
rubocop-performance (1.
|
112
|
-
rubocop (>= 1.
|
113
|
-
rubocop-ast (>=
|
140
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
141
|
+
rubocop-ast (1.32.3)
|
142
|
+
parser (>= 3.3.1.0)
|
143
|
+
rubocop-performance (1.22.1)
|
144
|
+
rubocop (>= 1.48.1, < 2.0)
|
145
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
114
146
|
rubocop-rake (0.6.0)
|
115
147
|
rubocop (~> 1.0)
|
116
|
-
rubocop-rspec (
|
117
|
-
rubocop (~> 1.
|
118
|
-
ruby-progressbar (1.
|
119
|
-
ruby_parser (3.
|
148
|
+
rubocop-rspec (3.0.5)
|
149
|
+
rubocop (~> 1.61)
|
150
|
+
ruby-progressbar (1.13.0)
|
151
|
+
ruby_parser (3.21.1)
|
152
|
+
racc (~> 1.5)
|
120
153
|
sexp_processor (~> 4.16)
|
121
|
-
|
122
|
-
|
154
|
+
securerandom (0.3.1)
|
155
|
+
sexp_processor (4.17.2)
|
156
|
+
sqlite3 (2.0.4)
|
123
157
|
mini_portile2 (~> 2.8.0)
|
124
|
-
|
125
|
-
|
158
|
+
stringio (3.1.1)
|
159
|
+
thor (1.3.2)
|
160
|
+
timeout (0.4.1)
|
161
|
+
tzinfo (2.0.6)
|
126
162
|
concurrent-ruby (~> 1.0)
|
127
|
-
unicode-display_width (2.
|
128
|
-
|
163
|
+
unicode-display_width (2.6.0)
|
164
|
+
useragent (0.16.10)
|
165
|
+
webrick (1.8.1)
|
166
|
+
zeitwerk (2.6.18)
|
129
167
|
|
130
168
|
PLATFORMS
|
131
169
|
ruby
|
@@ -147,4 +185,4 @@ DEPENDENCIES
|
|
147
185
|
sqlite3
|
148
186
|
|
149
187
|
BUNDLED WITH
|
150
|
-
2.
|
188
|
+
2.5.19
|
data/README.md
CHANGED
@@ -49,6 +49,7 @@ Lite::Uxid.configure do |config|
|
|
49
49
|
config.nanoid_size = 21
|
50
50
|
config.ulid_charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
51
51
|
config.ulid_size = 26
|
52
|
+
config.uuid_version = 4
|
52
53
|
end
|
53
54
|
```
|
54
55
|
|
@@ -92,7 +93,7 @@ Lite::Uxid::Ulid.encode #=> '01GJAY9KGR539EZF4QWYEJGSN7'
|
|
92
93
|
|
93
94
|
## UUID
|
94
95
|
|
95
|
-
Implements v4 of the specification. [More information](https://en.wikipedia.org/wiki/Universally_unique_identifier)
|
96
|
+
Implements `v4` and `v7` of the specification. [More information](https://en.wikipedia.org/wiki/Universally_unique_identifier)
|
96
97
|
|
97
98
|
```ruby
|
98
99
|
Lite::Uxid::Uuid.encode #=> '4376a67e-1189-44b3-a599-7f7566bf105b'
|
@@ -103,7 +104,19 @@ Lite::Uxid::Uuid.encode #=> '4376a67e-1189-44b3-a599-7f7566bf105b'
|
|
103
104
|
Local options can be passed to override global options.
|
104
105
|
|
105
106
|
```ruby
|
106
|
-
Lite::Uxid::Ulid.encode(
|
107
|
+
Lite::Uxid::Ulid.encode(charset: 'abc123', size: 12) #=> 'a3b12c12c3ca'
|
108
|
+
```
|
109
|
+
|
110
|
+
Passable options are:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
{
|
114
|
+
charset: 'string', # Available for: hashid, nanoid, ulid
|
115
|
+
salt: 'string', # Available for: hashid
|
116
|
+
size: 'integer', # Available for: hashid, nanoid, ulid
|
117
|
+
version: 'integer', # Available for: uuid
|
118
|
+
prefix: 'string' # Available for: hashid, nanoid
|
119
|
+
}
|
107
120
|
```
|
108
121
|
|
109
122
|
## ActiveRecord
|
@@ -113,7 +126,15 @@ Lite::Uxid::Ulid.encode(chars: 'abc123', size: 12) #=> 'a3b12c12c3ca'
|
|
113
126
|
Add the following attribute to all corresponding tables.
|
114
127
|
|
115
128
|
```ruby
|
116
|
-
|
129
|
+
# NOTE: null: true has to be set for HashID's
|
130
|
+
# since an ID must exist before it gets created.
|
131
|
+
t.string :uxid, null: false, index: { unique: true }
|
132
|
+
```
|
133
|
+
|
134
|
+
If using UUID and your database supports it:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
t.uuid :uxid, null: false, index: { unique: true }
|
117
138
|
```
|
118
139
|
|
119
140
|
**Setup**
|
@@ -148,6 +169,18 @@ class User < ActiveRecord::Base
|
|
148
169
|
end
|
149
170
|
```
|
150
171
|
|
172
|
+
Add a prefix to `hashid` and `nanoid` record types by adding a `uxid_prefix` method.
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
class User < ActiveRecord::Base
|
176
|
+
include Lite::Uxid::Record::Hashid
|
177
|
+
|
178
|
+
def uxid_prefix
|
179
|
+
"sub_"
|
180
|
+
end
|
181
|
+
end
|
182
|
+
```
|
183
|
+
|
151
184
|
**Usage**
|
152
185
|
|
153
186
|
Using one of the mixins above provides a handy method to find records by uxid.
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "securerandom" unless defined?(SecureRandom)
|
4
|
+
|
5
|
+
module Lite
|
6
|
+
module Uxid
|
7
|
+
module Base
|
8
|
+
class Irreversible
|
9
|
+
|
10
|
+
attr_reader :opts
|
11
|
+
|
12
|
+
def initialize(opts = {})
|
13
|
+
@opts = opts
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
|
18
|
+
def encode(opts = {})
|
19
|
+
klass = new(opts)
|
20
|
+
klass.encode
|
21
|
+
end
|
22
|
+
|
23
|
+
def decode(opts = {})
|
24
|
+
klass = new(opts)
|
25
|
+
klass.decode
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def encode
|
31
|
+
raise NotImplementedError, "override method in #{coder_class}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def decode
|
35
|
+
raise NotImplementedError, "coder does not support decoding"
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def coder_value_for(key)
|
41
|
+
sym_key = :"#{coder_class.downcase}_#{key}"
|
42
|
+
return unless Lite::Uxid.configuration.respond_to?(sym_key)
|
43
|
+
|
44
|
+
opts.delete(key) || Lite::Uxid.configuration.send(sym_key)
|
45
|
+
end
|
46
|
+
|
47
|
+
def coder_bytes
|
48
|
+
@coder_bytes ||= SecureRandom.random_bytes(coder_size).bytes
|
49
|
+
end
|
50
|
+
|
51
|
+
def coder_charset
|
52
|
+
@coder_charset ||= coder_value_for(:charset)
|
53
|
+
end
|
54
|
+
|
55
|
+
def coder_class
|
56
|
+
@coder_class ||= self.class.name.split("::").last
|
57
|
+
end
|
58
|
+
|
59
|
+
def coder_length
|
60
|
+
@coder_length ||= coder_charset.size
|
61
|
+
end
|
62
|
+
|
63
|
+
def coder_prefix
|
64
|
+
@coder_prefix ||= opts.delete(:prefix)
|
65
|
+
end
|
66
|
+
|
67
|
+
def coder_salt
|
68
|
+
@coder_salt ||= coder_value_for(:salt)
|
69
|
+
end
|
70
|
+
|
71
|
+
def coder_size
|
72
|
+
@coder_size ||= coder_value_for(:size)
|
73
|
+
end
|
74
|
+
|
75
|
+
def coder_version
|
76
|
+
@coder_version ||= coder_value_for(:version)
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lite
|
4
|
+
module Uxid
|
5
|
+
module Base
|
6
|
+
class Reversible < Irreversible
|
7
|
+
|
8
|
+
attr_reader :id
|
9
|
+
|
10
|
+
def initialize(id, opts = {})
|
11
|
+
@id = id
|
12
|
+
super(opts)
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
|
17
|
+
def encode(id, opts = {})
|
18
|
+
klass = new(id, opts)
|
19
|
+
klass.encode
|
20
|
+
end
|
21
|
+
|
22
|
+
def decode(id, opts = {})
|
23
|
+
klass = new(id, opts)
|
24
|
+
klass.decode
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def decode
|
30
|
+
raise NotImplementedError, "override method in #{coder_class}"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -5,7 +5,10 @@ module Lite
|
|
5
5
|
|
6
6
|
class Configuration
|
7
7
|
|
8
|
-
attr_accessor :hashid_charset, :
|
8
|
+
attr_accessor :hashid_charset, :hashid_size, :hashid_salt,
|
9
|
+
:nanoid_charset, :nanoid_size,
|
10
|
+
:ulid_charset, :ulid_size,
|
11
|
+
:uuid_version
|
9
12
|
|
10
13
|
def initialize
|
11
14
|
@hashid_charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
@@ -15,6 +18,7 @@ module Lite
|
|
15
18
|
@nanoid_size = 21
|
16
19
|
@ulid_charset = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
17
20
|
@ulid_size = 26
|
21
|
+
@uuid_version = 4
|
18
22
|
end
|
19
23
|
|
20
24
|
end
|
data/lib/lite/uxid/hashid.rb
CHANGED
@@ -2,14 +2,16 @@
|
|
2
2
|
|
3
3
|
module Lite
|
4
4
|
module Uxid
|
5
|
-
class Hashid < Reversible
|
5
|
+
class Hashid < Base::Reversible
|
6
6
|
|
7
7
|
def encode
|
8
|
-
encode_chars((id + coder_salt) << coder_size)
|
8
|
+
uxid = encode_chars((id + coder_salt) << coder_size)
|
9
|
+
"#{coder_prefix}#{uxid}"
|
9
10
|
end
|
10
11
|
|
11
12
|
def decode
|
12
|
-
|
13
|
+
uxid = id.delete_prefix(coder_prefix.to_s)
|
14
|
+
(decode_chars(uxid) >> coder_size) - coder_salt
|
13
15
|
end
|
14
16
|
|
15
17
|
private
|
data/lib/lite/uxid/nanoid.rb
CHANGED
@@ -2,12 +2,14 @@
|
|
2
2
|
|
3
3
|
module Lite
|
4
4
|
module Uxid
|
5
|
-
class Nanoid < Irreversible
|
5
|
+
class Nanoid < Base::Irreversible
|
6
6
|
|
7
7
|
def encode
|
8
|
-
(0...coder_size).each_with_object(+"") do |i, str|
|
8
|
+
uxid = (0...coder_size).each_with_object(+"") do |i, str|
|
9
9
|
str << coder_charset[coder_bytes[i] & 63]
|
10
10
|
end
|
11
|
+
|
12
|
+
"#{coder_prefix}#{uxid}"
|
11
13
|
end
|
12
14
|
|
13
15
|
end
|
@@ -10,12 +10,14 @@ module Lite
|
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
12
|
included do
|
13
|
-
|
13
|
+
after_commit :callback_generate_uxid!,
|
14
|
+
if: proc { respond_to?(:uxid) && !uxid? },
|
15
|
+
on: :create
|
14
16
|
end
|
15
17
|
|
16
18
|
class_methods do
|
17
19
|
def find_by_uxid(uxid)
|
18
|
-
decoded_id = Lite::Uxid::Hashid.decode(uxid)
|
20
|
+
decoded_id = Lite::Uxid::Hashid.decode(uxid, prefix: new.uxid_prefix)
|
19
21
|
find_by(id: decoded_id)
|
20
22
|
end
|
21
23
|
|
@@ -30,13 +32,17 @@ module Lite
|
|
30
32
|
def id_to_uxid
|
31
33
|
return unless respond_to?(:uxid)
|
32
34
|
|
33
|
-
Lite::Uxid::Hashid.encode(id)
|
35
|
+
Lite::Uxid::Hashid.encode(id, prefix: uxid_prefix)
|
34
36
|
end
|
35
37
|
|
36
38
|
def uxid_to_id
|
37
39
|
return unless respond_to?(:uxid)
|
38
40
|
|
39
|
-
Lite::Uxid::Hashid.decode(uxid)
|
41
|
+
Lite::Uxid::Hashid.decode(uxid, prefix: uxid_prefix)
|
42
|
+
end
|
43
|
+
|
44
|
+
def uxid_prefix
|
45
|
+
nil
|
40
46
|
end
|
41
47
|
|
42
48
|
private
|
@@ -10,7 +10,12 @@ module Lite
|
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
12
|
included do
|
13
|
-
before_create :callback_generate_uxid!,
|
13
|
+
before_create :callback_generate_uxid!,
|
14
|
+
if: proc { respond_to?(:uxid) && !uxid? }
|
15
|
+
end
|
16
|
+
|
17
|
+
def uxid_prefix
|
18
|
+
nil
|
14
19
|
end
|
15
20
|
|
16
21
|
private
|
@@ -19,7 +24,7 @@ module Lite
|
|
19
24
|
random_nanoid = nil
|
20
25
|
|
21
26
|
loop do
|
22
|
-
random_nanoid = Lite::Uxid::Nanoid.encode
|
27
|
+
random_nanoid = Lite::Uxid::Nanoid.encode(prefix: uxid_prefix)
|
23
28
|
break unless self.class.exists?(uxid: random_nanoid)
|
24
29
|
end
|
25
30
|
|
data/lib/lite/uxid/ulid.rb
CHANGED
data/lib/lite/uxid/uuid.rb
CHANGED
data/lib/lite/uxid/version.rb
CHANGED
data/lib/lite/uxid.rb
CHANGED
@@ -4,8 +4,8 @@ require "generators/lite/uxid/install_generator" if defined?(Rails::Generators)
|
|
4
4
|
|
5
5
|
require "lite/uxid/version"
|
6
6
|
require "lite/uxid/configuration"
|
7
|
-
require "lite/uxid/irreversible"
|
8
|
-
require "lite/uxid/reversible"
|
7
|
+
require "lite/uxid/base/irreversible"
|
8
|
+
require "lite/uxid/base/reversible"
|
9
9
|
require "lite/uxid/record/hashid"
|
10
10
|
require "lite/uxid/record/nanoid"
|
11
11
|
require "lite/uxid/record/ulid"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lite-uxid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -217,15 +217,15 @@ files:
|
|
217
217
|
- lib/generators/lite/uxid/install_generator.rb
|
218
218
|
- lib/generators/lite/uxid/templates/install.rb
|
219
219
|
- lib/lite/uxid.rb
|
220
|
+
- lib/lite/uxid/base/irreversible.rb
|
221
|
+
- lib/lite/uxid/base/reversible.rb
|
220
222
|
- lib/lite/uxid/configuration.rb
|
221
223
|
- lib/lite/uxid/hashid.rb
|
222
|
-
- lib/lite/uxid/irreversible.rb
|
223
224
|
- lib/lite/uxid/nanoid.rb
|
224
225
|
- lib/lite/uxid/record/hashid.rb
|
225
226
|
- lib/lite/uxid/record/nanoid.rb
|
226
227
|
- lib/lite/uxid/record/ulid.rb
|
227
228
|
- lib/lite/uxid/record/uuid.rb
|
228
|
-
- lib/lite/uxid/reversible.rb
|
229
229
|
- lib/lite/uxid/ulid.rb
|
230
230
|
- lib/lite/uxid/uuid.rb
|
231
231
|
- lib/lite/uxid/version.rb
|
@@ -254,7 +254,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
254
254
|
- !ruby/object:Gem::Version
|
255
255
|
version: '0'
|
256
256
|
requirements: []
|
257
|
-
rubygems_version: 3.
|
257
|
+
rubygems_version: 3.5.19
|
258
258
|
signing_key:
|
259
259
|
specification_version: 4
|
260
260
|
summary: Generate or obfuscate Id's using different patterns
|
@@ -1,71 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "securerandom" unless defined?(SecureRandom)
|
4
|
-
|
5
|
-
module Lite
|
6
|
-
module Uxid
|
7
|
-
class Irreversible
|
8
|
-
|
9
|
-
attr_reader :opts
|
10
|
-
|
11
|
-
def initialize(opts = {})
|
12
|
-
@opts = opts
|
13
|
-
end
|
14
|
-
|
15
|
-
class << self
|
16
|
-
|
17
|
-
def encode(opts = {})
|
18
|
-
klass = new(opts)
|
19
|
-
klass.encode
|
20
|
-
end
|
21
|
-
|
22
|
-
def decode(opts = {})
|
23
|
-
klass = new(opts)
|
24
|
-
klass.decode
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
def encode
|
30
|
-
raise NotImplementedError, "override method in #{coder_class}"
|
31
|
-
end
|
32
|
-
|
33
|
-
def decode
|
34
|
-
raise NotImplementedError, "coder does not support decoding"
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def coder_bytes
|
40
|
-
@coder_bytes ||= SecureRandom.random_bytes(coder_size).bytes
|
41
|
-
end
|
42
|
-
|
43
|
-
def coder_charset
|
44
|
-
@coder_charset ||=
|
45
|
-
opts.delete(:charset) ||
|
46
|
-
Lite::Uxid.configuration.send("#{coder_class.downcase}_charset")
|
47
|
-
end
|
48
|
-
|
49
|
-
def coder_class
|
50
|
-
@coder_class ||= self.class.name.split("::").last
|
51
|
-
end
|
52
|
-
|
53
|
-
def coder_length
|
54
|
-
@coder_length ||= coder_charset.size
|
55
|
-
end
|
56
|
-
|
57
|
-
def coder_salt
|
58
|
-
@coder_salt ||=
|
59
|
-
opts.delete(:salt) ||
|
60
|
-
Lite::Uxid.configuration.send("#{coder_class.downcase}_salt")
|
61
|
-
end
|
62
|
-
|
63
|
-
def coder_size
|
64
|
-
@coder_size ||=
|
65
|
-
opts.delete(:size) ||
|
66
|
-
Lite::Uxid.configuration.send("#{coder_class.downcase}_size")
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
data/lib/lite/uxid/reversible.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Lite
|
4
|
-
module Uxid
|
5
|
-
class Reversible < Irreversible
|
6
|
-
|
7
|
-
attr_reader :id
|
8
|
-
|
9
|
-
def initialize(id, opts = {})
|
10
|
-
@id = id
|
11
|
-
super(opts)
|
12
|
-
end
|
13
|
-
|
14
|
-
class << self
|
15
|
-
|
16
|
-
def encode(id, opts = {})
|
17
|
-
klass = new(id, opts)
|
18
|
-
klass.encode
|
19
|
-
end
|
20
|
-
|
21
|
-
def decode(id, opts = {})
|
22
|
-
klass = new(id, opts)
|
23
|
-
klass.decode
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
def decode
|
29
|
-
raise NotImplementedError, "override method in #{coder_class}"
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|