opaque_id 1.7.0 → 1.7.7

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.
@@ -1,565 +0,0 @@
1
- ---
2
- layout: default
3
- title: Configuration
4
- nav_order: 5
5
- description: "Complete configuration guide with all options and examples"
6
- permalink: /configuration/
7
- ---
8
-
9
- # Configuration
10
-
11
- OpaqueId provides extensive configuration options to customize ID generation for your specific needs. This guide covers all available configuration options with practical examples.
12
-
13
- - TOC
14
- {:toc}
15
-
16
- ## Model-Level Configuration
17
-
18
- Configure OpaqueId on a per-model basis using class-level settings.
19
-
20
- ### Basic Configuration
21
-
22
- ```ruby
23
- class User < ApplicationRecord
24
- include OpaqueId::Model
25
-
26
- # Custom column name
27
- self.opaque_id_column = :public_id
28
-
29
- # Custom length (default: 18)
30
- self.opaque_id_length = 15
31
-
32
- # Custom alphabet (default: SLUG_LIKE_ALPHABET)
33
- self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
34
-
35
- # Require letter start (default: false)
36
- self.opaque_id_require_letter_start = true
37
-
38
- # Max retry attempts (default: 3)
39
- self.opaque_id_max_retry = 5
40
-
41
- # Characters to purge from generated IDs (default: [])
42
- self.opaque_id_purge_chars = ['0', 'O', 'l', 'I']
43
- end
44
- ```
45
-
46
- ### Configuration Options Reference
47
-
48
- | Option | Type | Default | Description |
49
- | -------------------------------- | ------- | -------------------- | --------------------------------------------- |
50
- | `opaque_id_column` | Symbol | `:opaque_id` | Column name for storing the opaque ID |
51
- | `opaque_id_length` | Integer | `18` | Length of generated IDs |
52
- | `opaque_id_alphabet` | String | `SLUG_LIKE_ALPHABET` | Character set for ID generation |
53
- | `opaque_id_require_letter_start` | Boolean | `false` | Require IDs to start with a letter |
54
- | `opaque_id_max_retry` | Integer | `3` | Maximum retry attempts for collision handling |
55
- | `opaque_id_purge_chars` | Array | `[]` | Characters to exclude from generated IDs |
56
-
57
- ## Global Configuration
58
-
59
- Configure OpaqueId globally using an initializer.
60
-
61
- ### Basic Global Configuration
62
-
63
- ```ruby
64
- # config/initializers/opaque_id.rb
65
- OpaqueId.configure do |config|
66
- config.default_length = 15
67
- config.default_alphabet = OpaqueId::STANDARD_ALPHABET
68
- end
69
- ```
70
-
71
- ### Advanced Global Configuration
72
-
73
- ```ruby
74
- # config/initializers/opaque_id.rb
75
- OpaqueId.configure do |config|
76
- # Set default values for all models
77
- config.default_length = 21
78
- config.default_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
79
-
80
- # Configure specific models
81
- config.models = {
82
- 'User' => {
83
- length: 15,
84
- alphabet: OpaqueId::STANDARD_ALPHABET,
85
- require_letter_start: true
86
- },
87
- 'Order' => {
88
- length: 12,
89
- alphabet: OpaqueId::ALPHANUMERIC_ALPHABET,
90
- column: :public_id
91
- }
92
- }
93
- end
94
- ```
95
-
96
- ## Alphabet Configuration
97
-
98
- ### Built-in Alphabets
99
-
100
- #### SLUG_LIKE_ALPHABET (Default)
101
-
102
- ```ruby
103
- # Characters: 0-9, a-z (36 characters)
104
- # Use case: URL-safe, double-click selectable, no confusing characters
105
- # Example output: "izkpm55j334u8x9y2a"
106
-
107
- class User < ApplicationRecord
108
- include OpaqueId::Model
109
- self.opaque_id_alphabet = OpaqueId::SLUG_LIKE_ALPHABET
110
- end
111
- ```
112
-
113
- #### ALPHANUMERIC_ALPHABET
114
-
115
- ```ruby
116
- # Characters: A-Z, a-z, 0-9 (62 characters)
117
- # Use case: General purpose, URL-safe
118
- # Example output: "V1StGXR8Z5jdHi6BmyT"
119
-
120
- class User < ApplicationRecord
121
- include OpaqueId::Model
122
- self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
123
- end
124
- ```
125
-
126
- #### STANDARD_ALPHABET
127
-
128
- ```ruby
129
- # Characters: A-Z, a-z, 0-9, -, _ (64 characters)
130
- # Use case: Fastest generation, URL-safe
131
- # Example output: "V1StGXR8_Z5jdHi6B-myT"
132
-
133
- class User < ApplicationRecord
134
- include OpaqueId::Model
135
- self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
136
- end
137
- ```
138
-
139
- ### Custom Alphabets
140
-
141
- #### Numeric Only
142
-
143
- ```ruby
144
- class Order < ApplicationRecord
145
- include OpaqueId::Model
146
-
147
- # Generate numeric-only IDs
148
- self.opaque_id_alphabet = "0123456789"
149
- self.opaque_id_length = 10
150
- end
151
-
152
- # Example output: "1234567890"
153
- ```
154
-
155
- #### Hexadecimal
156
-
157
- ```ruby
158
- class ApiKey < ApplicationRecord
159
- include OpaqueId::Model
160
-
161
- # Generate hexadecimal IDs
162
- self.opaque_id_alphabet = "0123456789abcdef"
163
- self.opaque_id_length = 16
164
- end
165
-
166
- # Example output: "a1b2c3d4e5f67890"
167
- ```
168
-
169
- #### URL-Safe Characters
170
-
171
- ```ruby
172
- class Article < ApplicationRecord
173
- include OpaqueId::Model
174
-
175
- # Generate URL-safe IDs
176
- self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
177
- self.opaque_id_length = 8
178
- end
179
-
180
- # Example output: "V1StGXR8"
181
- ```
182
-
183
- #### Base64-Style
184
-
185
- ```ruby
186
- class Document < ApplicationRecord
187
- include OpaqueId::Model
188
-
189
- # Generate Base64-style IDs
190
- self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
191
- self.opaque_id_length = 12
192
- end
193
-
194
- # Example output: "V1StGXR8_Z5j"
195
- ```
196
-
197
- ## Length Configuration
198
-
199
- ### Short IDs (8-12 characters)
200
-
201
- ```ruby
202
- class ShortUrl < ApplicationRecord
203
- include OpaqueId::Model
204
-
205
- # Short IDs for URLs
206
- self.opaque_id_length = 8
207
- self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
208
- end
209
-
210
- # Example output: "V1StGXR8"
211
- ```
212
-
213
- ### Medium IDs (15-21 characters)
214
-
215
- ```ruby
216
- class User < ApplicationRecord
217
- include OpaqueId::Model
218
-
219
- # Standard length for general use
220
- self.opaque_id_length = 21
221
- self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
222
- end
223
-
224
- # Example output: "V1StGXR8_Z5jdHi6B-myT"
225
- ```
226
-
227
- ### Long IDs (32+ characters)
228
-
229
- ```ruby
230
- class ApiKey < ApplicationRecord
231
- include OpaqueId::Model
232
-
233
- # Long IDs for sensitive data
234
- self.opaque_id_length = 32
235
- self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
236
- end
237
-
238
- # Example output: "V1StGXR8_Z5jdHi6B-myT1234567890"
239
- ```
240
-
241
- ## Column Name Configuration
242
-
243
- ### Custom Column Names
244
-
245
- ```ruby
246
- class User < ApplicationRecord
247
- include OpaqueId::Model
248
-
249
- # Use a different column name
250
- self.opaque_id_column = :public_id
251
- end
252
-
253
- # Now the methods use the custom column name
254
- user = User.create!(name: "John Doe")
255
- puts user.public_id
256
- # => "izkpm55j334u8x9y2a"
257
-
258
- user = User.find_by_public_id("izkpm55j334u8x9y2a")
259
- ```
260
-
261
- ### Multiple Column Names
262
-
263
- ```ruby
264
- class User < ApplicationRecord
265
- include OpaqueId::Model
266
- self.opaque_id_column = :public_id
267
- end
268
-
269
- class Order < ApplicationRecord
270
- include OpaqueId::Model
271
- self.opaque_id_column = :order_number
272
- end
273
-
274
- class Product < ApplicationRecord
275
- include OpaqueId::Model
276
- self.opaque_id_column = :sku
277
- end
278
- ```
279
-
280
- ## Validation Configuration
281
-
282
- ### Letter Start Requirement
283
-
284
- ```ruby
285
- class User < ApplicationRecord
286
- include OpaqueId::Model
287
-
288
- # Require IDs to start with a letter
289
- self.opaque_id_require_letter_start = true
290
- end
291
-
292
- # This will retry until it generates an ID starting with a letter
293
- user = User.create!(name: "John Doe")
294
- puts user.opaque_id
295
- # => "izkpm55j334u8x9y2a" (starts with 'i')
296
- ```
297
-
298
- ### Character Purging
299
-
300
- ```ruby
301
- class User < ApplicationRecord
302
- include OpaqueId::Model
303
-
304
- # Exclude confusing characters
305
- self.opaque_id_purge_chars = ['0', 'O', 'l', 'I']
306
- end
307
-
308
- # Generated IDs will not contain these characters
309
- user = User.create!(name: "John Doe")
310
- puts user.opaque_id
311
- # => "izkpm55j334u8x9y2a" (no '0', 'O', 'l', 'I')
312
- ```
313
-
314
- ## Collision Handling Configuration
315
-
316
- ### Retry Attempts
317
-
318
- ```ruby
319
- class User < ApplicationRecord
320
- include OpaqueId::Model
321
-
322
- # Increase retry attempts for high-volume applications
323
- self.opaque_id_max_retry = 10
324
- end
325
- ```
326
-
327
- ### Custom Collision Handling
328
-
329
- ```ruby
330
- class User < ApplicationRecord
331
- include OpaqueId::Model
332
-
333
- private
334
-
335
- def set_opaque_id
336
- retries = 0
337
- max_retries = opaque_id_max_retry
338
-
339
- begin
340
- self.opaque_id = generate_opaque_id
341
- rescue OpaqueId::GenerationError => e
342
- retries += 1
343
- if retries < max_retries
344
- retry
345
- else
346
- # Custom fallback logic
347
- self.opaque_id = generate_fallback_id
348
- end
349
- end
350
- end
351
-
352
- def generate_fallback_id
353
- # Fallback generation method
354
- "fallback_#{SecureRandom.hex(10)}"
355
- end
356
- end
357
- ```
358
-
359
- ## Environment-Specific Configuration
360
-
361
- ### Development Environment
362
-
363
- ```ruby
364
- # config/environments/development.rb
365
- if Rails.env.development?
366
- OpaqueId.configure do |config|
367
- # Shorter IDs for development
368
- config.default_length = 8
369
- config.default_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
370
- end
371
- end
372
- ```
373
-
374
- ### Production Environment
375
-
376
- ```ruby
377
- # config/environments/production.rb
378
- if Rails.env.production?
379
- OpaqueId.configure do |config|
380
- # Longer IDs for production
381
- config.default_length = 21
382
- config.default_alphabet = OpaqueId::STANDARD_ALPHABET
383
- end
384
- end
385
- ```
386
-
387
- ### Test Environment
388
-
389
- ```ruby
390
- # config/environments/test.rb
391
- if Rails.env.test?
392
- OpaqueId.configure do |config|
393
- # Predictable IDs for testing
394
- config.default_length = 10
395
- config.default_alphabet = "0123456789"
396
- end
397
- end
398
- ```
399
-
400
- ## Configuration Examples by Use Case
401
-
402
- ### E-commerce Application
403
-
404
- ```ruby
405
- # Orders - short, numeric IDs
406
- class Order < ApplicationRecord
407
- include OpaqueId::Model
408
-
409
- self.opaque_id_column = :order_number
410
- self.opaque_id_length = 8
411
- self.opaque_id_alphabet = "0123456789"
412
- end
413
-
414
- # Products - medium, alphanumeric IDs
415
- class Product < ApplicationRecord
416
- include OpaqueId::Model
417
-
418
- self.opaque_id_column = :sku
419
- self.opaque_id_length = 12
420
- self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
421
- end
422
-
423
- # Users - standard IDs
424
- class User < ApplicationRecord
425
- include OpaqueId::Model
426
-
427
- self.opaque_id_length = 21
428
- self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
429
- end
430
- ```
431
-
432
- ### API Development
433
-
434
- ```ruby
435
- # API Keys - long, secure IDs
436
- class ApiKey < ApplicationRecord
437
- include OpaqueId::Model
438
-
439
- self.opaque_id_length = 32
440
- self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
441
- self.opaque_id_require_letter_start = true
442
- end
443
-
444
- # API Tokens - medium, URL-safe IDs
445
- class ApiToken < ApplicationRecord
446
- include OpaqueId::Model
447
-
448
- self.opaque_id_column = :token
449
- self.opaque_id_length = 24
450
- self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
451
- end
452
- ```
453
-
454
- ### Content Management
455
-
456
- ```ruby
457
- # Articles - short, URL-friendly IDs
458
- class Article < ApplicationRecord
459
- include OpaqueId::Model
460
-
461
- self.opaque_id_column = :slug
462
- self.opaque_id_length = 8
463
- self.opaque_id_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
464
- end
465
-
466
- # Comments - standard IDs
467
- class Comment < ApplicationRecord
468
- include OpaqueId::Model
469
-
470
- self.opaque_id_length = 15
471
- self.opaque_id_alphabet = OpaqueId::ALPHANUMERIC_ALPHABET
472
- end
473
- ```
474
-
475
- ## Configuration Validation
476
-
477
- ### Validate Configuration
478
-
479
- ```ruby
480
- class User < ApplicationRecord
481
- include OpaqueId::Model
482
-
483
- validate :validate_opaque_id_configuration
484
-
485
- private
486
-
487
- def validate_opaque_id_configuration
488
- if opaque_id_length < 8
489
- errors.add(:base, "Opaque ID length must be at least 8 characters")
490
- end
491
-
492
- if opaque_id_alphabet.size < 16
493
- errors.add(:base, "Opaque ID alphabet must have at least 16 characters")
494
- end
495
-
496
- if opaque_id_max_retry < 1
497
- errors.add(:base, "Opaque ID max retry must be at least 1")
498
- end
499
- end
500
- end
501
- ```
502
-
503
- ### Configuration Testing
504
-
505
- ```ruby
506
- # test/models/user_test.rb
507
- class UserTest < ActiveSupport::TestCase
508
- test "opaque_id configuration is valid" do
509
- user = User.new(name: "Test User")
510
-
511
- assert user.valid?
512
- assert_equal 18, user.class.opaque_id_length
513
- assert_equal OpaqueId::SLUG_LIKE_ALPHABET, user.class.opaque_id_alphabet
514
- end
515
-
516
- test "opaque_id generation works with custom configuration" do
517
- user = User.new(name: "Test User")
518
- user.class.opaque_id_length = 15
519
- user.class.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
520
-
521
- user.save!
522
-
523
- assert_equal 15, user.opaque_id.length
524
- assert user.opaque_id.match?(/\A[A-Za-z0-9_-]+\z/)
525
- end
526
- end
527
- ```
528
-
529
- ## Best Practices
530
-
531
- ### 1. Choose Appropriate Length
532
-
533
- - **Short IDs (8-12)**: Public URLs, user-facing identifiers
534
- - **Medium IDs (15-21)**: General purpose, internal references
535
- - **Long IDs (32+)**: Sensitive data, API keys, tokens
536
-
537
- ### 2. Select Suitable Alphabets
538
-
539
- - **SLUG_LIKE_ALPHABET** (default): URL-safe, double-click selectable, no confusing characters
540
- - **ALPHANUMERIC_ALPHABET**: General purpose, URL-safe
541
- - **STANDARD_ALPHABET**: Fastest generation, URL-safe
542
- - **Custom alphabets**: Specific requirements (numeric, hexadecimal, etc.)
543
-
544
- ### 3. Configure Collision Handling
545
-
546
- - **Low volume**: Default retry attempts (3)
547
- - **High volume**: Increase retry attempts (5-10)
548
- - **Critical systems**: Implement custom fallback logic
549
-
550
- ### 4. Use Environment-Specific Settings
551
-
552
- - **Development**: Shorter IDs for easier testing
553
- - **Production**: Longer IDs for better security
554
- - **Test**: Predictable IDs for consistent testing
555
-
556
- ## Next Steps
557
-
558
- Now that you understand configuration options:
559
-
560
- 1. **Explore [Alphabets](alphabets.md)** for detailed alphabet information
561
- 2. **Check out [Use Cases](use-cases.md)** for real-world configuration examples
562
- 3. **Review [Performance](performance.md)** for configuration optimization tips
563
- 4. **Read [API Reference](api-reference.md)** for complete configuration documentation
564
- 5. **Learn about [Algorithms](algorithms.md)** for technical implementation details
565
- 6. **Understand [Security](security.md)** for security considerations