based_uuid 0.6.0 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44d2023b06834d9043fc245f4df1a8963a360337dfdde32f8bb99b579f4ab317
4
- data.tar.gz: 7a5c34de026288973f1a41bb7a49f176ec2bdb77b6ed2d3ff68096311b3f1715
3
+ metadata.gz: 5efa8c1cc0d82b116e8e4bfd6a5170bb83fd01258f19eb35b32aa28a0bc39804
4
+ data.tar.gz: 15e4f3a03f286ec1bbc40f3fd1da6d19c2d5b0b17db9718762d4a632966484ac
5
5
  SHA512:
6
- metadata.gz: 3369d39151be720015bbdc9a330631405b5d90e9021755500d933cfd60dd9b432eda9d25de220e612d4393a4bcb71d057ba82c31cc5da83054828d7c34648283
7
- data.tar.gz: 982f29d49439441ede750b3fa1c6cb06a09aec805a4745ac8ae0b2ef3f9f2cd5cdf96a4a8c3394c4617333e536dbaff2e01810d77dd8a6b00bd34e2fe2b89a5e
6
+ metadata.gz: a94e6cf70c7cc5954f97c2720032885ad7bee008f30e41dd6af341c67ad04756940d4e5a8d865d33135abf737a0ed4c9170812d7135e711e848e68d23442b9ec
7
+ data.tar.gz: f7eba4f45b4deaadd65d4ac731411bc86a15b47197972bb9ee4c2f0f5ede773a99d79a6b3f50a3a30cc5b47c845f564cc89d628e18d9a28fce67b055b0d6b801
data/.rubocop.yml CHANGED
@@ -23,3 +23,7 @@ Layout/LineLength:
23
23
 
24
24
  Naming/PredicateName:
25
25
  Enabled: false
26
+
27
+ # https://github.com/rubocop/rubocop/issues/7298
28
+ Lint/UselessAccessModifier:
29
+ Enabled: false
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # BasedUUID: URL-friendly UUIDs for Rails models
2
2
 
3
- [![Build Status](https://github.com/pch/based_uuid/workflows/Tests/badge.svg)](https://github.com/pch/based_uuid/actions) [![Gem Version](https://badge.fury.io/rb/based_uuid.svg)](https://badge.fury.io/rb/based_uuid)
4
-
3
+ [![Build Status](https://github.com/pch/based_uuid/workflows/Tests/badge.svg)](https://github.com/pch/based_uuid/actions)
5
4
 
6
5
  Generate “double-clickable”, URL-friendly UUIDs with (optional) prefixes:
7
6
 
@@ -73,11 +72,32 @@ def to_param
73
72
  end
74
73
  ```
75
74
 
75
+ ### Custom UUID column name
76
+
77
+ `BasedUUID` will respect the value of `Model.primary_key`, so it supports custom primary key names:
78
+
79
+ ```ruby
80
+ class Transaction < ApplicationRecord
81
+ self.primary_key = "txid"
82
+
83
+ has_based_uuid prefix: :tx
84
+ end
85
+ ```
86
+
87
+ If you want to use a different column, other than the primary key, you can pass it as an option to `has_based_uuid`:
88
+
89
+ ```ruby
90
+ class Session < ApplicationRecord
91
+ has_based_uuid prefix: :sid, uuid_column: :session_id
92
+ end
93
+ ```
94
+
76
95
  ### Use outside ActiveRecord
77
96
 
78
97
  BasedUUID can be used outside ActiveRecord, too. You can encode any UUID with it:
79
98
 
80
99
  ```ruby
100
+ BasedUUID.encode(uuid: "226d037c-3b35-40f3-a30b-0ebb78779d9b")
81
101
  BasedUUID.encode(uuid: "226d037c-3b35-40f3-a30b-0ebb78779d9b", prefix: :bpo)
82
102
  BasedUUID.decode("bpo_12dm1qresn83st62reqdw7f7cv")
83
103
  ```
@@ -2,7 +2,6 @@ require "active_support/lazy_load_hooks"
2
2
  require "active_support/concern"
3
3
  require "active_support/core_ext/object/blank"
4
4
  require "active_support/core_ext/class/attribute"
5
- require "active_record"
6
5
 
7
6
  module BasedUUID
8
7
  module HasBasedUUID
@@ -10,13 +9,16 @@ module BasedUUID
10
9
 
11
10
  included do
12
11
  class_attribute :_based_uuid_prefix
12
+ class_attribute :_based_uuid_column
13
13
  end
14
14
 
15
15
  class_methods do
16
- def has_based_uuid(prefix: nil)
16
+ def has_based_uuid(prefix: nil, uuid_column: primary_key)
17
17
  include ModelExtensions
18
18
 
19
19
  self._based_uuid_prefix = prefix
20
+ self._based_uuid_column = uuid_column
21
+
20
22
  BasedUUID.register_model_prefix(prefix, self) if prefix
21
23
  end
22
24
  end
@@ -27,27 +29,34 @@ module BasedUUID
27
29
 
28
30
  class_methods do
29
31
  def find_by_based_uuid(token)
30
- prefix, uuid_base32 = BasedUUID.split(token)
31
- raise ArgumentError, "Invalid prefix" if prefix && prefix.to_sym != _based_uuid_prefix
32
-
33
- find_by(primary_key => Base32UUID.decode(uuid_base32))
32
+ decode_and_find_based_uuid(token:)
34
33
  end
35
34
 
36
35
  def find_by_based_uuid!(token)
37
- find_by_based_uuid(token) or raise ActiveRecord::RecordNotFound
36
+ decode_and_find_based_uuid(token:, raise_error: true)
37
+ end
38
+
39
+ private
40
+
41
+ def decode_and_find_based_uuid(token:, raise_error: false)
42
+ prefix, uuid_base32 = BasedUUID.split(token)
43
+ raise ArgumentError, "Invalid prefix" if prefix && prefix.to_sym != _based_uuid_prefix
44
+
45
+ method_name = raise_error ? :find_by! : :find_by
46
+ send(method_name, _based_uuid_column => Base32UUID.decode(uuid_base32))
38
47
  end
39
48
  end
40
49
 
41
50
  def based_uuid(prefix: true)
42
- raise ArgumentError, "UUID is empty" if _primary_key_value.blank?
51
+ raise ArgumentError, "UUID is empty" if _uuid_column_value.blank?
43
52
 
44
- BasedUUID.encode(uuid: _primary_key_value, prefix: prefix ? self.class._based_uuid_prefix : nil)
53
+ BasedUUID.encode(uuid: _uuid_column_value, prefix: prefix ? self.class._based_uuid_prefix : nil)
45
54
  end
46
55
 
47
56
  private
48
57
 
49
- def _primary_key_value
50
- self[self.class.primary_key]
58
+ def _uuid_column_value
59
+ self[self.class._based_uuid_column]
51
60
  end
52
61
  end
53
62
  end
@@ -1,3 +1,3 @@
1
1
  module BasedUUID
2
- VERSION = "0.6.0".freeze
2
+ VERSION = "0.6.2".freeze
3
3
  end
data/lib/based_uuid.rb CHANGED
@@ -30,7 +30,7 @@ module BasedUUID
30
30
  [prefix.presence, uuid_base32]
31
31
  end
32
32
 
33
- def encode(uuid:, prefix:)
33
+ def encode(uuid:, prefix: nil)
34
34
  uuid_base32 = Base32UUID.encode(uuid)
35
35
  return uuid_base32 unless prefix
36
36
 
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: based_uuid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Chmolowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-06 00:00:00.000000000 Z
11
+ date: 2023-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activerecord
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '7.0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '7.0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: activesupport
29
15
  requirement: !ruby/object:Gem::Requirement