lite-uxid 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +7 -7
- data/README.md +23 -11
- data/lib/lite/uxid/nanoid.rb +26 -0
- data/lib/lite/uxid/record/nanoid.rb +37 -0
- data/lib/lite/uxid/version.rb +1 -1
- data/lib/lite/uxid.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a8f42192c67ad0638ff6b3001a5887b5c5fc1bd9e05cb7e6a757c53d11ce15d
|
4
|
+
data.tar.gz: '058af68572f76f3c6227f2a437e254b25e783e9495e86bddd57b5bf2295b5446'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c29f1a9f5cd7721cf67f07b5f4077ed1e0aa312bcf7fa095ebaf4a51d4c0ec4b518ab719bd32b3ece0f431c248f1f27ef3aaeb2f60f99d6f197dba8375248539
|
7
|
+
data.tar.gz: d476f672216ee1ee7e136ffe1dd335c3a6ebd15ab29a7ade8d7f1fed7e74416fd3109067d5e1af6ce70fea240b11c6af4a88d59c7f8fed81d620d72178dbd281
|
data/CHANGELOG.md
CHANGED
@@ -6,8 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
-
## [1.0.
|
9
|
+
## [1.0.2] - 2019-07-30
|
10
10
|
### Added
|
11
|
+
- Added nanoid
|
12
|
+
|
13
|
+
## [1.0.1] - 2019-06-25
|
14
|
+
### Removed
|
11
15
|
- Remove unused activesupport dependency
|
12
16
|
|
13
17
|
## [1.0.0] - 2019-06-25
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lite-uxid (1.0.
|
4
|
+
lite-uxid (1.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -39,7 +39,7 @@ GEM
|
|
39
39
|
database_cleaner (1.7.0)
|
40
40
|
diff-lcs (1.3)
|
41
41
|
erubi (1.8.0)
|
42
|
-
fasterer (0.
|
42
|
+
fasterer (0.6.0)
|
43
43
|
colorize (~> 0.7)
|
44
44
|
ruby_parser (>= 3.13.0)
|
45
45
|
generator_spec (0.9.4)
|
@@ -74,12 +74,12 @@ GEM
|
|
74
74
|
rake (>= 0.8.7)
|
75
75
|
thor (>= 0.19.0, < 2.0)
|
76
76
|
rainbow (3.0.0)
|
77
|
-
rake (12.3.
|
77
|
+
rake (12.3.3)
|
78
78
|
rspec (3.8.0)
|
79
79
|
rspec-core (~> 3.8.0)
|
80
80
|
rspec-expectations (~> 3.8.0)
|
81
81
|
rspec-mocks (~> 3.8.0)
|
82
|
-
rspec-core (3.8.
|
82
|
+
rspec-core (3.8.2)
|
83
83
|
rspec-support (~> 3.8.0)
|
84
84
|
rspec-expectations (3.8.4)
|
85
85
|
diff-lcs (>= 1.2.0, < 2.0)
|
@@ -88,16 +88,16 @@ GEM
|
|
88
88
|
diff-lcs (>= 1.2.0, < 2.0)
|
89
89
|
rspec-support (~> 3.8.0)
|
90
90
|
rspec-support (3.8.2)
|
91
|
-
rubocop (0.
|
91
|
+
rubocop (0.73.0)
|
92
92
|
jaro_winkler (~> 1.5.1)
|
93
93
|
parallel (~> 1.10)
|
94
94
|
parser (>= 2.6)
|
95
95
|
rainbow (>= 2.2.2, < 4.0)
|
96
96
|
ruby-progressbar (~> 1.7)
|
97
97
|
unicode-display_width (>= 1.4.0, < 1.7)
|
98
|
-
rubocop-performance (1.4.
|
98
|
+
rubocop-performance (1.4.1)
|
99
99
|
rubocop (>= 0.71.0)
|
100
|
-
rubocop-rspec (1.
|
100
|
+
rubocop-rspec (1.34.0)
|
101
101
|
rubocop (>= 0.60.0)
|
102
102
|
ruby-progressbar (1.10.1)
|
103
103
|
ruby_parser (3.13.1)
|
data/README.md
CHANGED
@@ -28,6 +28,7 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
* [Configuration](#configuration)
|
30
30
|
* [Hash](#hash)
|
31
|
+
* [NanoID](#nanoid)
|
31
32
|
* [ULID](#ulid)
|
32
33
|
* [ActiveRecord](#active_record)
|
33
34
|
* [Port](#port)
|
@@ -54,15 +55,23 @@ Lite::Uxid::Hash.encode(10) #=> 'q5D8inm0'
|
|
54
55
|
Lite::Uxid::Hash.decode('q5D8inm0') #=> 10
|
55
56
|
```
|
56
57
|
|
58
|
+
## NanoID
|
59
|
+
|
60
|
+
NanoID are not reversible and are the most performant random ID's but while unlikely can produce collisions.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
Lite::Uxid::Nanoid.encode #=> '0bmHjB5Gx8FTBqJekX6dS6XIXf'
|
64
|
+
```
|
65
|
+
|
57
66
|
## ULID
|
58
67
|
|
59
|
-
ULID are not reversible and more performant than Hash
|
68
|
+
ULID are not reversible and more performant than Hash ID's.
|
60
69
|
|
61
70
|
```ruby
|
62
71
|
Lite::Uxid::Ulid.encode #=> '1mqfg9qa96s8s5f02o1ucf8lcc'
|
63
72
|
```
|
64
73
|
|
65
|
-
|
74
|
+
#### ActiveRecord
|
66
75
|
|
67
76
|
**Table**
|
68
77
|
|
@@ -70,19 +79,23 @@ Add the following attribute to all corresponding tables.
|
|
70
79
|
|
71
80
|
```ruby
|
72
81
|
# omitted
|
73
|
-
t.binary :uxid, limit:
|
82
|
+
t.binary :uxid, limit: 26, index: { unique: true }
|
74
83
|
# omitted
|
75
84
|
```
|
76
85
|
|
77
86
|
**Setup**
|
78
87
|
|
79
|
-
All `uxid` attributes will be automatically generated and applied when the record is created.
|
88
|
+
All `nanoid` and `uxid` attributes will be automatically generated and applied when the record is created.
|
80
89
|
|
81
90
|
```ruby
|
82
91
|
class User < ActiveRecord::Base
|
83
92
|
include Lite::Uxid::Record::Hash
|
84
93
|
|
85
|
-
#
|
94
|
+
# - or -
|
95
|
+
|
96
|
+
include Lite::Uxid::Record::Nanoid
|
97
|
+
|
98
|
+
# - or -
|
86
99
|
|
87
100
|
include Lite::Uxid::Record::Ulid
|
88
101
|
end
|
@@ -93,19 +106,18 @@ end
|
|
93
106
|
The following methods are for Hash based Uxid's.
|
94
107
|
|
95
108
|
```ruby
|
96
|
-
User.
|
97
|
-
User.
|
109
|
+
User.find_by_ulid('x123') #=> Find record by uxid
|
110
|
+
User.find_by_ulid!('x123') #=> Raises an ActiveRecord::RecordNotFound error if not found
|
98
111
|
|
99
112
|
user = User.new
|
100
|
-
user.
|
113
|
+
user.hash_to_id #=> Decodes the records uxid to id (only for Hash based Id's)
|
101
114
|
```
|
102
115
|
|
103
116
|
## Port
|
104
117
|
|
105
|
-
`Lite::Uxid` is compatible port of [ActiveUxid](https://github.com/drexed/active_uxid).
|
118
|
+
`Lite::Uxid` is a compatible port of [ActiveUxid](https://github.com/drexed/active_uxid).
|
106
119
|
|
107
|
-
Switching is as easy as renaming `ActiveUxid
|
108
|
-
and `ActiveUxid::Ulid` to `Lite::Uxid::Ulid`.
|
120
|
+
Switching is as easy as renaming `ActiveUxid` to `Lite::Uxid`.
|
109
121
|
|
110
122
|
## Development
|
111
123
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
module Lite
|
6
|
+
module Uxid
|
7
|
+
class Nanoid < Lite::Uxid::Base
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def encode
|
12
|
+
klass = new
|
13
|
+
klass.nanoid_encode
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def nanoid_encode
|
19
|
+
encoding_length.times.with_object('') do |_i, str|
|
20
|
+
str + encoding_chars[(SecureRandom.rand * encoding_base).floor]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
module Lite
|
7
|
+
module Uxid
|
8
|
+
module Record
|
9
|
+
module Nanoid
|
10
|
+
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
included do
|
14
|
+
before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) }
|
15
|
+
|
16
|
+
def self.find_by_uxid(uxid)
|
17
|
+
find_by(uxid: uxid)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.find_by_uxid!(uxid)
|
21
|
+
record = find_by_uxid(uxid)
|
22
|
+
return record unless record.nil?
|
23
|
+
|
24
|
+
raise ActiveRecord::RecordNotFound
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def callback_generate_uxid!
|
31
|
+
self.uxid = Lite::Uxid::Nanoid.encode
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/lite/uxid/version.rb
CHANGED
data/lib/lite/uxid.rb
CHANGED
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.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -192,7 +192,9 @@ files:
|
|
192
192
|
- lib/lite/uxid/base.rb
|
193
193
|
- lib/lite/uxid/configuration.rb
|
194
194
|
- lib/lite/uxid/hash.rb
|
195
|
+
- lib/lite/uxid/nanoid.rb
|
195
196
|
- lib/lite/uxid/record/hash.rb
|
197
|
+
- lib/lite/uxid/record/nanoid.rb
|
196
198
|
- lib/lite/uxid/record/ulid.rb
|
197
199
|
- lib/lite/uxid/ulid.rb
|
198
200
|
- lib/lite/uxid/version.rb
|