based_uuid 0.5.2 → 0.6.0
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/README.md +25 -16
- data/lib/based_uuid/has_based_uuid.rb +1 -1
- data/lib/based_uuid/version.rb +1 -1
- data/lib/based_uuid.rb +6 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44d2023b06834d9043fc245f4df1a8963a360337dfdde32f8bb99b579f4ab317
|
4
|
+
data.tar.gz: 7a5c34de026288973f1a41bb7a49f176ec2bdb77b6ed2d3ff68096311b3f1715
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3369d39151be720015bbdc9a330631405b5d90e9021755500d933cfd60dd9b432eda9d25de220e612d4393a4bcb71d057ba82c31cc5da83054828d7c34648283
|
7
|
+
data.tar.gz: 982f29d49439441ede750b3fa1c6cb06a09aec805a4745ac8ae0b2ef3f9f2cd5cdf96a4a8c3394c4617333e536dbaff2e01810d77dd8a6b00bd34e2fe2b89a5e
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# BasedUUID:
|
1
|
+
# BasedUUID: URL-friendly UUIDs for Rails models
|
2
2
|
|
3
3
|
[](https://github.com/pch/based_uuid/actions) [](https://badge.fury.io/rb/based_uuid)
|
4
4
|
|
@@ -6,13 +6,13 @@
|
|
6
6
|
Generate “double-clickable”, URL-friendly UUIDs with (optional) prefixes:
|
7
7
|
|
8
8
|
```
|
9
|
-
|
10
|
-
|
9
|
+
user_763j02ryxh8dbs56mgcjqrmmgt #=> e61c802c-7bb1-4357-929a-9064af8a521a
|
10
|
+
bpo_12dm1qresn83st62reqdw7f7cv #=> 226d037c-3b35-40f3-a30b-0ebb78779d9b
|
11
11
|
```
|
12
12
|
|
13
|
-
This gem
|
13
|
+
This gem encodes UUID primary keys into 26-character lowercase strings using [Crockford’s base32](https://www.crockford.com/base32.html) encoding. The optional prefix helps you identify the model it represents.
|
14
14
|
|
15
|
-
BasedUUID doesn’t affect how your
|
15
|
+
BasedUUID assumes that you have a [UUID primary key](https://guides.rubyonrails.org/v5.0/active_record_postgresql.html#uuid) (`id`) in your ActiveRecord model. It doesn’t affect how your primary key UUIDs are stored in the database. Prefixes and base32-encoded strings are only used for presentation.
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
@@ -24,7 +24,7 @@ gem "based_uuid"
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
|
27
|
+
Add the following line to your model class:
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
class BlogPost < ApplicationRecord
|
@@ -32,8 +32,8 @@ class BlogPost < ApplicationRecord
|
|
32
32
|
end
|
33
33
|
|
34
34
|
post = BlogPost.last
|
35
|
-
post.based_uuid #=>
|
36
|
-
post.based_uuid(prefix: false) #=>
|
35
|
+
post.based_uuid #=> bpo_12dm1qresn83st62reqdw7f7cv
|
36
|
+
post.based_uuid(prefix: false) #=> 12dm1qresn83st62reqdw7f7cv
|
37
37
|
```
|
38
38
|
|
39
39
|
### Lookup
|
@@ -41,13 +41,13 @@ post.based_uuid(prefix: false) #=> 3ah4veflijgy7de6bflk7k4ld4
|
|
41
41
|
BasedUUID includes a `find_by_based_uuid` model method to look up records:
|
42
42
|
|
43
43
|
```ruby
|
44
|
-
BlogPost.find_by_based_uuid("
|
44
|
+
BlogPost.find_by_based_uuid("bpo_12dm1qresn83st62reqdw7f7cv")
|
45
45
|
|
46
46
|
# or without the prefix:
|
47
|
-
BlogPost.find_by_based_uuid("
|
47
|
+
BlogPost.find_by_based_uuid("12dm1qresn83st62reqdw7f7cv")
|
48
48
|
|
49
49
|
# there’s also the bang version:
|
50
|
-
BlogPost.find_by_based_uuid!("
|
50
|
+
BlogPost.find_by_based_uuid!("12dm1qresn83st62reqdw7f7cv")
|
51
51
|
```
|
52
52
|
|
53
53
|
### Generic lookup
|
@@ -55,17 +55,17 @@ BlogPost.find_by_based_uuid!("3ah4veflijgy7de6bflk7k4ld4")
|
|
55
55
|
The gem provides a generic lookup method to help you find the correct model for the UUID, based on prefix:
|
56
56
|
|
57
57
|
```ruby
|
58
|
-
BasedUUID.find("
|
58
|
+
BasedUUID.find("bpo_12dm1qresn83st62reqdw7f7cv")
|
59
59
|
#=> #<BlogPost>
|
60
|
-
BasedUUID.find("
|
61
|
-
#=> #<
|
60
|
+
BasedUUID.find("user_763j02ryxh8dbs56mgcjqrmmgt")
|
61
|
+
#=> #<User>
|
62
62
|
```
|
63
63
|
|
64
64
|
**⚠️ NOTE:** Rails lazy-loads models in the development environment, so this method won’t know about your models until you’ve referenced them at least once. If you’re using this method in a Rails console, you’ll need to run `BlogPost` (or any other model) before you can use it.
|
65
65
|
|
66
66
|
### BasedUUID as default URL identifiers
|
67
67
|
|
68
|
-
BasedUUID aims to be
|
68
|
+
BasedUUID aims to be non-intrusive and it doesn’t affect how Rails URLs are generated, so if you want to use it as default URL param, add this to your model:
|
69
69
|
|
70
70
|
```ruby
|
71
71
|
def to_param
|
@@ -73,6 +73,15 @@ def to_param
|
|
73
73
|
end
|
74
74
|
```
|
75
75
|
|
76
|
+
### Use outside ActiveRecord
|
77
|
+
|
78
|
+
BasedUUID can be used outside ActiveRecord, too. You can encode any UUID with it:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
BasedUUID.encode(uuid: "226d037c-3b35-40f3-a30b-0ebb78779d9b", prefix: :bpo)
|
82
|
+
BasedUUID.decode("bpo_12dm1qresn83st62reqdw7f7cv")
|
83
|
+
```
|
84
|
+
|
76
85
|
* * *
|
77
86
|
|
78
87
|
## Development
|
@@ -89,4 +98,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/pch/ba
|
|
89
98
|
|
90
99
|
This gem is heavily inspired by [Stripe IDs](https://stripe.com/docs/api) and the [prefixed_ids](https://github.com/excid3/prefixed_ids/tree/master) gem by Chris Oliver.
|
91
100
|
|
92
|
-
Parts of the base32 encoding code are borrowed from the [ulid
|
101
|
+
Parts of the base32 encoding code are borrowed from the [ulid](https://github.com/rafaelsales/ulid) gem by Rafael Sales.
|
@@ -41,7 +41,7 @@ module BasedUUID
|
|
41
41
|
def based_uuid(prefix: true)
|
42
42
|
raise ArgumentError, "UUID is empty" if _primary_key_value.blank?
|
43
43
|
|
44
|
-
BasedUUID.
|
44
|
+
BasedUUID.encode(uuid: _primary_key_value, prefix: prefix ? self.class._based_uuid_prefix : nil)
|
45
45
|
end
|
46
46
|
|
47
47
|
private
|
data/lib/based_uuid/version.rb
CHANGED
data/lib/based_uuid.rb
CHANGED
@@ -30,11 +30,16 @@ module BasedUUID
|
|
30
30
|
[prefix.presence, uuid_base32]
|
31
31
|
end
|
32
32
|
|
33
|
-
def
|
33
|
+
def encode(uuid:, prefix:)
|
34
34
|
uuid_base32 = Base32UUID.encode(uuid)
|
35
35
|
return uuid_base32 unless prefix
|
36
36
|
|
37
37
|
"#{prefix}#{delimiter}#{uuid_base32}"
|
38
38
|
end
|
39
|
+
|
40
|
+
def decode(token)
|
41
|
+
_, uuid_base32 = split(token)
|
42
|
+
Base32UUID.decode(uuid_base32)
|
43
|
+
end
|
39
44
|
end
|
40
45
|
end
|