opaque_id 1.2.0 โ†’ 1.4.0

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.
@@ -0,0 +1,259 @@
1
+ ---
2
+ layout: default
3
+ title: Getting Started
4
+ nav_order: 2
5
+ description: "Quick setup and basic usage guide for OpaqueId"
6
+ permalink: /getting-started/
7
+ ---
8
+
9
+ # Getting Started
10
+
11
+ This guide will help you get up and running with OpaqueId in your Rails application. We'll cover installation, basic setup, and common usage patterns.
12
+
13
+ - TOC
14
+ {:toc}
15
+
16
+ ## Prerequisites
17
+
18
+ Before you begin, ensure you have:
19
+
20
+ - **Ruby 3.2+** - OpaqueId requires modern Ruby features
21
+ - **Rails 8.0+** - Built for the latest Rails version
22
+ - **ActiveRecord 8.0+** - For model integration
23
+
24
+ ## Installation
25
+
26
+ ### Step 1: Add to Gemfile
27
+
28
+ Add OpaqueId to your application's Gemfile:
29
+
30
+ ```ruby
31
+ gem 'opaque_id'
32
+ ```
33
+
34
+ Then run:
35
+
36
+ ```bash
37
+ bundle install
38
+ ```
39
+
40
+ ### Step 2: Generate Migration and Update Model
41
+
42
+ Use the Rails generator to set up OpaqueId for a specific model:
43
+
44
+ ```bash
45
+ rails generate opaque_id:install User
46
+ ```
47
+
48
+ This command will:
49
+
50
+ - Create a migration to add the `opaque_id` column
51
+ - Add a unique index on the `opaque_id` column
52
+ - Include the `OpaqueId::Model` concern in your model
53
+
54
+ ### Step 3: Run Migration
55
+
56
+ Execute the migration:
57
+
58
+ ```bash
59
+ rails db:migrate
60
+ ```
61
+
62
+ ### Step 4: Verify Setup
63
+
64
+ Check that your model has been updated correctly:
65
+
66
+ ```ruby
67
+ # app/models/user.rb
68
+ class User < ApplicationRecord
69
+ include OpaqueId::Model
70
+ end
71
+ ```
72
+
73
+ ## Basic Usage
74
+
75
+ ### Creating Records
76
+
77
+ Once set up, OpaqueId automatically generates opaque IDs when you create new records:
78
+
79
+ ```ruby
80
+ # Create a new user
81
+ user = User.create!(name: "John Doe", email: "john@example.com")
82
+
83
+ # The opaque_id is automatically generated
84
+ puts user.opaque_id
85
+ # => "V1StGXR8_Z5jdHi6B-myT"
86
+ ```
87
+
88
+ ### Finding Records
89
+
90
+ Use the provided finder methods to locate records by their opaque ID:
91
+
92
+ ```ruby
93
+ # Find by opaque_id (returns nil if not found)
94
+ user = User.find_by_opaque_id("V1StGXR8_Z5jdHi6B-myT")
95
+
96
+ # Find by opaque_id (raises exception if not found)
97
+ user = User.find_by_opaque_id!("V1StGXR8_Z5jdHi6B-myT")
98
+ ```
99
+
100
+ ### Standalone ID Generation
101
+
102
+ You can also generate opaque IDs without ActiveRecord:
103
+
104
+ ```ruby
105
+ # Generate a default opaque ID (21 characters, alphanumeric)
106
+ id = OpaqueId.generate
107
+ # => "V1StGXR8_Z5jdHi6B-myT"
108
+
109
+ # Generate with custom length
110
+ id = OpaqueId.generate(size: 10)
111
+ # => "V1StGXR8_Z5"
112
+
113
+ # Generate with custom alphabet
114
+ id = OpaqueId.generate(alphabet: OpaqueId::STANDARD_ALPHABET)
115
+ # => "V1StGXR8_Z5jdHi6B-myT"
116
+ ```
117
+
118
+ ## Common Patterns
119
+
120
+ ### API Endpoints
121
+
122
+ Use opaque IDs in your API endpoints for better security:
123
+
124
+ ```ruby
125
+ # routes.rb
126
+ resources :users, param: :opaque_id
127
+
128
+ # controller
129
+ class UsersController < ApplicationController
130
+ def show
131
+ @user = User.find_by_opaque_id!(params[:id])
132
+ end
133
+ end
134
+ ```
135
+
136
+ ### URL Generation
137
+
138
+ Generate URLs using opaque IDs:
139
+
140
+ ```ruby
141
+ # Instead of exposing internal IDs
142
+ user_path(user) # => /users/123
143
+
144
+ # Use opaque IDs
145
+ user_path(user.opaque_id) # => /users/V1StGXR8_Z5jdHi6B-myT
146
+ ```
147
+
148
+ ### Custom Column Names
149
+
150
+ If you prefer a different column name, specify it during generation:
151
+
152
+ ```bash
153
+ rails generate opaque_id:install User --column-name=public_id
154
+ ```
155
+
156
+ This will:
157
+
158
+ - Create a `public_id` column instead of `opaque_id`
159
+ - Add the configuration to your model automatically
160
+
161
+ ## Configuration Options
162
+
163
+ OpaqueId is highly configurable. Here are the most common options:
164
+
165
+ ### Model-Level Configuration
166
+
167
+ ```ruby
168
+ class User < ApplicationRecord
169
+ include OpaqueId::Model
170
+
171
+ # Custom column name
172
+ self.opaque_id_column = :public_id
173
+
174
+ # Custom length (default: 21)
175
+ self.opaque_id_length = 15
176
+
177
+ # Custom alphabet (default: ALPHANUMERIC_ALPHABET)
178
+ self.opaque_id_alphabet = OpaqueId::STANDARD_ALPHABET
179
+
180
+ # Require letter start (default: false)
181
+ self.opaque_id_require_letter_start = true
182
+
183
+ # Max retry attempts for collision handling (default: 3)
184
+ self.opaque_id_max_retry = 5
185
+ end
186
+ ```
187
+
188
+ ### Global Configuration
189
+
190
+ You can also configure OpaqueId globally in an initializer:
191
+
192
+ ```ruby
193
+ # config/initializers/opaque_id.rb
194
+ OpaqueId.configure do |config|
195
+ config.default_length = 15
196
+ config.default_alphabet = OpaqueId::STANDARD_ALPHABET
197
+ end
198
+ ```
199
+
200
+ ## Built-in Alphabets
201
+
202
+ OpaqueId comes with two pre-configured alphabets:
203
+
204
+ ### ALPHANUMERIC_ALPHABET (Default)
205
+
206
+ - **Characters**: `A-Z`, `a-z`, `0-9` (62 characters)
207
+ - **Use case**: General purpose, URL-safe
208
+ - **Example**: `V1StGXR8_Z5jdHi6B-myT`
209
+
210
+ ### STANDARD_ALPHABET
211
+
212
+ - **Characters**: `A-Z`, `a-z`, `0-9`, `-`, `_` (64 characters)
213
+ - **Use case**: When you need the fastest generation
214
+ - **Example**: `V1StGXR8_Z5jdHi6B-myT`
215
+
216
+ ## Error Handling
217
+
218
+ OpaqueId provides comprehensive error handling:
219
+
220
+ ```ruby
221
+ begin
222
+ user = User.create!(name: "John Doe")
223
+ rescue OpaqueId::GenerationError => e
224
+ # Handle ID generation failure (very rare)
225
+ Rails.logger.error "Failed to generate opaque ID: #{e.message}"
226
+ end
227
+ ```
228
+
229
+ ## Next Steps
230
+
231
+ Now that you have OpaqueId set up, you might want to explore:
232
+
233
+ - [Installation](installation.md) - Detailed installation guide
234
+ - [Usage](usage.md) - Comprehensive usage examples
235
+ - [Configuration](configuration.md) - Detailed configuration guide
236
+ - [Alphabets](alphabets.md) - Built-in alphabets and custom options
237
+ - [API Reference](api-reference.md) - Complete API documentation
238
+ - [Use Cases](use-cases.md) - Real-world examples and applications
239
+ - [Performance](performance.md) - Benchmarks and optimization tips
240
+ - [Security](security.md) - Security considerations and best practices
241
+
242
+ ## Troubleshooting
243
+
244
+ ### Common Issues
245
+
246
+ **Migration fails**: Ensure you're using Rails 8.0+ and have the correct database permissions.
247
+
248
+ **Generator not found**: Make sure you've added `gem 'opaque_id'` to your Gemfile and run `bundle install`.
249
+
250
+ **ID generation fails**: This is extremely rare, but if it happens, check your database constraints and ensure the column allows the configured length.
251
+
252
+ ### Getting Help
253
+
254
+ If you encounter issues:
255
+
256
+ 1. Check the [API Reference](api-reference.md) for detailed method documentation
257
+ 2. Review the [Usage](usage.md) guide for common patterns
258
+ 3. Check the [Use Cases](use-cases.md) for real-world examples
259
+ 4. Open an issue on [GitHub](https://github.com/nyaggah/opaque_id/issues) with details about your setup and the error
data/docs/index.md ADDED
@@ -0,0 +1,135 @@
1
+ ---
2
+ layout: default
3
+ title: Home
4
+ nav_order: 1
5
+ description: "Generate cryptographically secure, collision-free opaque IDs for ActiveRecord models"
6
+ permalink: /
7
+ ---
8
+
9
+ # OpaqueId
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/opaque_id.svg?icon=si%3Arubygems)](https://badge.fury.io/rb/opaque_id)
12
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
13
+ [![Gem Downloads](https://img.shields.io/badge/gem%20downloads-opaque_id-blue)](https://rubygems.org/gems/opaque_id)
14
+
15
+ A Ruby gem for generating cryptographically secure, collision-free opaque IDs for ActiveRecord models. OpaqueId provides a drop-in replacement for `nanoid.rb` using Ruby's built-in `SecureRandom` methods with optimized algorithms for unbiased distribution.
16
+
17
+ - TOC
18
+ {:toc}
19
+
20
+ ## Features
21
+
22
+ - **๐Ÿ” Cryptographically Secure**: Uses Ruby's `SecureRandom` for secure ID generation
23
+ - **โšก High Performance**: Optimized algorithms with fast paths for 64-character alphabets
24
+ - **๐ŸŽฏ Collision-Free**: Built-in collision detection with configurable retry attempts
25
+ - **๐Ÿ”ง Highly Configurable**: Customizable alphabet, length, column name, and validation rules
26
+ - **๐Ÿš€ Rails Integration**: Seamless ActiveRecord integration with automatic ID generation
27
+ - **๐Ÿ“ฆ Rails Generator**: One-command setup with `rails generate opaque_id:install`
28
+ - **๐Ÿงช Well Tested**: Comprehensive test suite with statistical uniformity tests
29
+ - **๐Ÿ“š Rails 8.0+ Compatible**: Built for modern Rails applications
30
+
31
+ ## Quick Start
32
+
33
+ ### 1. Add to your Gemfile
34
+
35
+ ```ruby
36
+ gem 'opaque_id'
37
+ ```
38
+
39
+ ### 2. Generate migration and update model
40
+
41
+ ```bash
42
+ rails generate opaque_id:install User
43
+ ```
44
+
45
+ ### 3. Run migration
46
+
47
+ ```bash
48
+ rails db:migrate
49
+ ```
50
+
51
+ ### 4. Use in your models
52
+
53
+ ```ruby
54
+ class User < ApplicationRecord
55
+ include OpaqueId::Model
56
+ end
57
+
58
+ # Create a user - opaque_id is automatically generated
59
+ user = User.create!(name: "John Doe")
60
+ puts user.opaque_id # => "V1StGXR8_Z5jdHi6B-myT"
61
+
62
+ # Find by opaque_id
63
+ user = User.find_by_opaque_id("V1StGXR8_Z5jdHi6B-myT")
64
+ ```
65
+
66
+ ## Why OpaqueId?
67
+
68
+ OpaqueId replaces the need for `nanoid.rb` by providing:
69
+
70
+ - **Native Ruby implementation** using `SecureRandom`
71
+ - **Better performance** with optimized algorithms
72
+ - **Rails 8.0+ compatibility** out of the box
73
+ - **ActiveRecord integration** with automatic ID generation
74
+ - **Collision handling** with configurable retry attempts
75
+ - **Flexible configuration** for different use cases
76
+
77
+ ## Installation
78
+
79
+ ### Requirements
80
+
81
+ - Ruby 3.2+
82
+ - Rails 8.0+
83
+ - ActiveRecord 8.0+
84
+
85
+ ### Using Bundler (Recommended)
86
+
87
+ Add this line to your application's Gemfile:
88
+
89
+ ```ruby
90
+ gem 'opaque_id'
91
+ ```
92
+
93
+ And then execute:
94
+
95
+ ```bash
96
+ bundle install
97
+ ```
98
+
99
+ ### Manual Installation
100
+
101
+ ```bash
102
+ gem install opaque_id
103
+ ```
104
+
105
+ ## Getting Started
106
+
107
+ Ready to get started? Check out our [Getting Started Guide](getting-started.md) for a comprehensive walkthrough.
108
+
109
+ ## Documentation
110
+
111
+ - [Getting Started](getting-started.md) - Quick setup and basic usage
112
+ - [Installation](installation.md) - Detailed installation guide
113
+ - [Usage](usage.md) - Comprehensive usage examples
114
+ - [Configuration](configuration.md) - All configuration options
115
+ - [Alphabets](alphabets.md) - Built-in alphabets and custom options
116
+ - [Algorithms](algorithms.md) - Technical algorithm details
117
+ - [Performance](performance.md) - Benchmarks and optimization tips
118
+ - [Security](security.md) - Security considerations and best practices
119
+ - [Use Cases](use-cases.md) - Real-world examples and applications
120
+ - [API Reference](api-reference.md) - Complete API documentation
121
+ - [Development](development.md) - Contributing and development setup
122
+
123
+ ## License
124
+
125
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
126
+
127
+ ## Contributing
128
+
129
+ This project follows an "open source, closed contribution" model. We welcome bug reports, feature requests, and documentation improvements through GitHub Issues.
130
+
131
+ ## Acknowledgements
132
+
133
+ - [nanoid.rb](https://github.com/radeno/nanoid.rb) - Original inspiration and reference implementation
134
+ - [NanoID](https://github.com/ai/nanoid) - The original JavaScript implementation
135
+ - [PlanetScale Article](https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api) by Mike Coutermarsh - Excellent explanation of opaque ID benefits