cool_id 0.1.8 → 0.1.9

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: a13030502ed2121afaaa78cd0443dc8c199c8ac0608080400ad5624947ed4a5a
4
- data.tar.gz: a626eeead1ca380bebf7a172eb46bc2e683073f8fd353c5ad9ecfe6721589e46
3
+ metadata.gz: 07011e094fb16385d448b80b4461b0e579d29efc0b4273fff4b41dd1df350a64
4
+ data.tar.gz: 11d8a83f95a7fce11598df98d1b7175c26b00f8a0d19d6af18ebaa35b8ddfa0b
5
5
  SHA512:
6
- metadata.gz: b22b2cd792877343af3f1e9b3aa51fcf8e5f6920fc1fa79407e9149c4b43acbea3c9bbd10959c46810f2cb4fc0a25c5d538211d274f10adc18ee8c5583f1040e
7
- data.tar.gz: 44d27992a9c89ba415db43cc31ee3c590037107135a5bc58dcf542f0b7fda702ae965798c39b01448c8a74fad3b43618b64ae1545a53ad85c85386770ce954fd
6
+ metadata.gz: f98ab9a494119599b383cc41bc9fdb797f68e793f626b07759899fee06ded282b6b8fe4f98d8ab82425127c64f4daac2511b8645f61458625e8356c8a428efe0
7
+ data.tar.gz: 6002ff50f083a330ad2fcdb582df9e12d9aea31d26e1d7f737dcd20650c0fa7454e2eb43a7f88c84e47bcdb5f9965557433d97acfca13b0afa854032c4de28fa
data/README.md CHANGED
@@ -35,9 +35,26 @@ and generate ids without creating a record
35
35
  # generate an id, e.g. for batch inserts or upserts
36
36
  User.generate_cool_id
37
37
  # => "usr_vktd1b5v84lr"
38
+ ```
39
+
40
+ you can use cool_id with a separate field, keeping the default primary key:
41
+
42
+ ```ruby
43
+ class Product < ActiveRecord::Base
44
+ include CoolId::Model
45
+ cool_id prefix: "prd", id_field: :public_id
46
+ end
38
47
 
48
+ product = Product.create!(name: "Cool Product")
49
+ product.id # => 1 (or another integer)
50
+ product.public_id # => "prd_vktd1b5v84lr"
51
+
52
+ # You can still use CoolId.locate with the public_id
53
+ CoolId.locate("prd_vktd1b5v84lr") # => #<Product id: 1, public_id: "prd_vktd1b5v84lr", name: "Cool Product">
39
54
  ```
40
55
 
56
+ this approach allows you to keep your primary key as an auto-incrementing integer while still benefiting from CoolId's functionality. it's particularly useful when you want to expose a public identifier that's separate from your internal primary key.
57
+
41
58
  it takes parameters to change the alphabet or length
42
59
 
43
60
  ```ruby
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CoolId
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.9"
5
5
  end
data/lib/cool_id.rb CHANGED
@@ -15,10 +15,10 @@ module CoolId
15
15
  DEFAULT_LENGTH = 12
16
16
  DEFAULT_MAX_RETRIES = 1000
17
17
 
18
- Id = Struct.new(:key, :prefix, :id, :model_class)
18
+ Id = Struct.new(:key, :prefix, :id, :model_class, :id_field)
19
19
 
20
20
  class << self
21
- attr_accessor :separator, :alphabet, :length, :max_retries
21
+ attr_accessor :separator, :alphabet, :length, :max_retries, :id_field
22
22
 
23
23
  def configure
24
24
  yield self
@@ -29,6 +29,7 @@ module CoolId
29
29
  self.alphabet = DEFAULT_ALPHABET
30
30
  self.length = DEFAULT_LENGTH
31
31
  self.max_retries = DEFAULT_MAX_RETRIES
32
+ self.id_field = nil
32
33
  end
33
34
 
34
35
  def registry
@@ -54,6 +55,10 @@ module CoolId
54
55
  end
55
56
  end
56
57
  end
58
+
59
+ def resolve_cool_id_field(model_class)
60
+ model_class.cool_id_config&.id_field || CoolId.id_field || model_class.primary_key
61
+ end
57
62
  end
58
63
 
59
64
  self.separator = DEFAULT_SEPARATOR
@@ -72,26 +77,31 @@ module CoolId
72
77
 
73
78
  def locate(id)
74
79
  parsed = parse(id)
75
- parsed&.model_class&.find_by(id: id)
80
+ return nil unless parsed
81
+
82
+ id_field = CoolId.resolve_cool_id_field(parsed.model_class)
83
+ parsed.model_class.find_by(id_field => id)
76
84
  end
77
85
 
78
86
  def parse(id)
79
87
  prefix, key = id.split(CoolId.separator, 2)
80
88
  model_class = @prefix_map[prefix]
81
89
  return nil unless model_class
82
- Id.new(key, prefix, id, model_class)
90
+ id_field = CoolId.resolve_cool_id_field(model_class)
91
+ Id.new(key, prefix, id, model_class, id_field)
83
92
  end
84
93
  end
85
94
 
86
95
  class Config
87
- attr_reader :prefix, :length, :alphabet, :max_retries, :model_class
96
+ attr_reader :prefix, :length, :alphabet, :max_retries, :model_class, :id_field
88
97
 
89
- def initialize(prefix:, model_class:, length: nil, alphabet: nil, max_retries: nil)
98
+ def initialize(prefix:, model_class:, length: nil, alphabet: nil, max_retries: nil, id_field: nil)
90
99
  @length = length
91
100
  @prefix = validate_prefix(prefix)
92
101
  @alphabet = validate_alphabet(alphabet)
93
102
  @max_retries = max_retries
94
103
  @model_class = model_class
104
+ @id_field = id_field
95
105
  end
96
106
 
97
107
  private
@@ -148,7 +158,8 @@ module CoolId
148
158
  private
149
159
 
150
160
  def set_cool_id
151
- self.id = self.class.generate_cool_id if id.blank?
161
+ id_field = CoolId.resolve_cool_id_field(self.class)
162
+ self[id_field] = self.class.generate_cool_id if self[id_field].blank?
152
163
  end
153
164
 
154
165
  def ensure_cool_id_configured
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cool_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Schilling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-23 00:00:00.000000000 Z
11
+ date: 2024-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nanoid