cool_id 0.1.2 → 0.1.3

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: e34a657a23c3321adf021f9ab101a6cee1f792a3e8b71c11702663dd92ac6883
4
- data.tar.gz: f364193fe7ec136a8a436032a5de7b16a7db6b32b4901baa72f3eaa4a39267ea
3
+ metadata.gz: 6a61d8d06c3414dcec52ecd9a3f28696d0fafb683b2b206e025c33a1deb03de9
4
+ data.tar.gz: bc13a903ed6d5239eae6ee216f5da45699a525d755e914f9f9c2b81b6e33d26c
5
5
  SHA512:
6
- metadata.gz: 54f60190090f171c8c9f3df697378ad65fb9224551b208fadaddf0b4e2da37818126cbb52127a79e32ede2156f9e59cfe81eedfd20a7bd50dcac83371056806e
7
- data.tar.gz: eb277e15108b217785785803a854d6f88405d2e9fd2b8bcf84583890fbb1fb8a48b2392c2b2a9aa20567d547f92fd5ccd2bce946636acc8ccb1e7781d8985e87
6
+ metadata.gz: 77aac33c12253962d7beabffc5d7101379f40d991b580c67ea0a306cd82f8d2befda21b23fbd3c3735a516863ef4c3565b18084f67ed001172b7a395527f3f4a
7
+ data.tar.gz: d3c269343bd10098fdd902424c10c22e676a0cea67d5dff404628863ea4d085319017b31d6c602364da024d36c30a470e1bd3ade7b687379bbbd3f011a72191c
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CoolId
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/cool_id.rb CHANGED
@@ -7,6 +7,7 @@ require "active_support/concern"
7
7
  module CoolId
8
8
  class Error < StandardError; end
9
9
 
10
+ # defaults based on https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api
10
11
  DEFAULT_SEPARATOR = "_"
11
12
  DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"
12
13
  DEFAULT_LENGTH = 12
@@ -27,7 +28,7 @@ module CoolId
27
28
  end
28
29
 
29
30
  def registry
30
- @registry ||= Registry.new
31
+ @prefix_map ||= Registry.new
31
32
  end
32
33
 
33
34
  def generate_id(config)
@@ -39,18 +40,17 @@ module CoolId
39
40
  end
40
41
  end
41
42
 
42
- # defaults based on https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api
43
43
  self.separator = DEFAULT_SEPARATOR
44
44
  self.alphabet = DEFAULT_ALPHABET
45
45
  self.length = DEFAULT_LENGTH
46
46
 
47
47
  class Registry
48
48
  def initialize
49
- @registry = {}
49
+ @prefix_map = {}
50
50
  end
51
51
 
52
52
  def register(prefix, model_class)
53
- @registry[prefix] = model_class
53
+ @prefix_map[prefix] = model_class
54
54
  end
55
55
 
56
56
  def locate(id)
@@ -60,7 +60,7 @@ module CoolId
60
60
 
61
61
  def parse(id)
62
62
  prefix, key = id.split(CoolId.separator, 2)
63
- model_class = @registry[prefix]
63
+ model_class = @prefix_map[prefix]
64
64
  return nil unless model_class
65
65
  Id.new(key, prefix, id, model_class)
66
66
  end
@@ -79,7 +79,7 @@ module CoolId
79
79
 
80
80
  def validate_prefix(value)
81
81
  raise ArgumentError, "Prefix cannot be nil" if value.nil?
82
- raise ArgumentError, "Prefix cannot consist only of whitespace" if value.strip.empty?
82
+ raise ArgumentError, "Prefix cannot be empty" if value.empty?
83
83
  value
84
84
  end
85
85
 
@@ -94,7 +94,8 @@ module CoolId
94
94
  extend ActiveSupport::Concern
95
95
 
96
96
  class_methods do
97
- attr_reader :cool_id_config
97
+ attr_accessor :cool_id_config
98
+ attr_accessor :cool_id_setup_required
98
99
 
99
100
  def cool_id(options)
100
101
  @cool_id_config = Config.new(**options)
@@ -104,16 +105,38 @@ module CoolId
104
105
  def generate_cool_id
105
106
  CoolId.generate_id(@cool_id_config)
106
107
  end
108
+
109
+ def ensure_cool_id_setup
110
+ @cool_id_setup_required = true
111
+ end
112
+
113
+ def skip_cool_id_setup
114
+ @cool_id_setup_required = false
115
+ end
116
+
117
+ def inherited(subclass)
118
+ super
119
+ if @cool_id_setup_required && !subclass.instance_variable_defined?(:@cool_id_setup_required)
120
+ subclass.instance_variable_set(:@cool_id_setup_required, true)
121
+ end
122
+ end
107
123
  end
108
124
 
109
125
  included do
110
126
  before_create :set_cool_id
127
+ after_initialize :ensure_cool_id_configured
111
128
 
112
129
  private
113
130
 
114
131
  def set_cool_id
115
132
  self.id = self.class.generate_cool_id if id.blank?
116
133
  end
134
+
135
+ def ensure_cool_id_configured
136
+ if self.class.cool_id_setup_required && self.class.cool_id_config.nil?
137
+ raise Error, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_cool_id_setup' to opt out."
138
+ end
139
+ end
117
140
  end
118
141
  end
119
142
 
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.2
4
+ version: 0.1.3
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-17 00:00:00.000000000 Z
11
+ date: 2024-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nanoid