cool_id 0.1.7 → 0.1.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd52ffe11cf7b25bf473b06e879a7df67eb4aea3b9169b0961342fef30a6fce7
4
- data.tar.gz: b92c14dd88880ad52de1884f8a1e2720915b5ee24b742c0db8a966071bd07196
3
+ metadata.gz: a13030502ed2121afaaa78cd0443dc8c199c8ac0608080400ad5624947ed4a5a
4
+ data.tar.gz: a626eeead1ca380bebf7a172eb46bc2e683073f8fd353c5ad9ecfe6721589e46
5
5
  SHA512:
6
- metadata.gz: f41bf3c022796fdaf4e8f53af035b6515c6a6b5ac59b749c52a17247bb6710b7296424d2a86c20f484500cae15f7daec32ec1a3980717441a0fec186ad528945
7
- data.tar.gz: c81557d5b123b122be77208ef6c74421d81c048dd409f289421ee1bfbaea075550621c77d0c6d92f5b11df393b4fd3be820e62cf2ef491a8e48958705e218d5c
6
+ metadata.gz: b22b2cd792877343af3f1e9b3aa51fcf8e5f6920fc1fa79407e9149c4b43acbea3c9bbd10959c46810f2cb4fc0a25c5d538211d274f10adc18ee8c5583f1040e
7
+ data.tar.gz: 44d27992a9c89ba415db43cc31ee3c590037107135a5bc58dcf542f0b7fda702ae965798c39b01448c8a74fad3b43618b64ae1545a53ad85c85386770ce954fd
data/.aider.conf.yml ADDED
@@ -0,0 +1,9 @@
1
+ # https://aider.chat/docs/config/aider_conf.html
2
+
3
+ lint-cmd: bundle exec standardrb --fix
4
+ auto-lint: true
5
+
6
+ test-cmd: bundle exec rspec --format progress --no-profile --no-color --fail-fast
7
+ auto-test: true
8
+
9
+ read: cool_id.gemspec
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CoolId
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
5
5
  end
data/lib/cool_id.rb CHANGED
@@ -7,15 +7,18 @@ require "active_support/concern"
7
7
  module CoolId
8
8
  class NotConfiguredError < StandardError; end
9
9
 
10
+ class MaxRetriesExceededError < StandardError; end
11
+
10
12
  # defaults based on https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api
11
13
  DEFAULT_SEPARATOR = "_"
12
14
  DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"
13
15
  DEFAULT_LENGTH = 12
16
+ DEFAULT_MAX_RETRIES = 1000
14
17
 
15
18
  Id = Struct.new(:key, :prefix, :id, :model_class)
16
19
 
17
20
  class << self
18
- attr_accessor :separator, :alphabet, :length
21
+ attr_accessor :separator, :alphabet, :length, :max_retries
19
22
 
20
23
  def configure
21
24
  yield self
@@ -25,6 +28,7 @@ module CoolId
25
28
  self.separator = DEFAULT_SEPARATOR
26
29
  self.alphabet = DEFAULT_ALPHABET
27
30
  self.length = DEFAULT_LENGTH
31
+ self.max_retries = DEFAULT_MAX_RETRIES
28
32
  end
29
33
 
30
34
  def registry
@@ -34,15 +38,28 @@ module CoolId
34
38
  def generate_id(config)
35
39
  alphabet = config.alphabet || @alphabet
36
40
  length = config.length || @length
37
- id = Nanoid.generate(size: length, alphabet: alphabet)
41
+ max_retries = config.max_retries || @max_retries
42
+
43
+ retries = 0
44
+ loop do
45
+ nano_id = Nanoid.generate(size: length, alphabet: alphabet)
46
+ full_id = "#{config.prefix}#{separator}#{nano_id}"
47
+ if !config.model_class.exists?(id: full_id)
48
+ return full_id
49
+ end
38
50
 
39
- "#{config.prefix}#{separator}#{id}"
51
+ retries += 1
52
+ if retries >= max_retries
53
+ raise MaxRetriesExceededError, "Failed to generate a unique ID after #{max_retries} attempts"
54
+ end
55
+ end
40
56
  end
41
57
  end
42
58
 
43
59
  self.separator = DEFAULT_SEPARATOR
44
60
  self.alphabet = DEFAULT_ALPHABET
45
61
  self.length = DEFAULT_LENGTH
62
+ self.max_retries = DEFAULT_MAX_RETRIES
46
63
 
47
64
  class Registry
48
65
  def initialize
@@ -67,12 +84,14 @@ module CoolId
67
84
  end
68
85
 
69
86
  class Config
70
- attr_reader :prefix, :length, :alphabet
87
+ attr_reader :prefix, :length, :alphabet, :max_retries, :model_class
71
88
 
72
- def initialize(prefix:, length: nil, alphabet: nil)
89
+ def initialize(prefix:, model_class:, length: nil, alphabet: nil, max_retries: nil)
73
90
  @length = length
74
91
  @prefix = validate_prefix(prefix)
75
92
  @alphabet = validate_alphabet(alphabet)
93
+ @max_retries = max_retries
94
+ @model_class = model_class
76
95
  end
77
96
 
78
97
  private
@@ -98,7 +117,7 @@ module CoolId
98
117
  attr_accessor :cool_id_setup_required
99
118
 
100
119
  def cool_id(options)
101
- @cool_id_config = Config.new(**options)
120
+ @cool_id_config = Config.new(**options, model_class: self)
102
121
  CoolId.registry.register(options[:prefix], self)
103
122
  end
104
123
 
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.7
4
+ version: 0.1.8
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-20 00:00:00.000000000 Z
11
+ date: 2024-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nanoid
@@ -73,6 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".aider.conf.yml"
76
77
  - ".rspec"
77
78
  - ".standard.yml"
78
79
  - CHANGELOG.md