Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,12 @@ jobs:
- name: Run the default task
run: bundle exec rake

docs:
if: github.ref == 'refs/heads/main'
rubyfmt:
runs-on: ubuntu-latest
steps:
- uses: Homebrew/actions/setup-homebrew@master
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Generate HTML documentation
run: bundle exec yard doc --output-dir ./docs
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./docs

deploy:
needs: docs
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
- name: Install rubyfmt
run: brew install rubyfmt
- name: Check formatting
run: rubyfmt --check .
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@

# rspec failure tracking
.rspec_status
.aider*
36 changes: 22 additions & 14 deletions lib/cool_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
# The CoolId module provides functionality for generating and managing unique identifiers.
module CoolId
# Error raised when CoolId is not configured for a model.
class NotConfiguredError < StandardError; end
class NotConfiguredError < StandardError
end

# Error raised when the maximum number of retries is exceeded while generating a unique ID.
class MaxRetriesExceededError < StandardError; end
class MaxRetriesExceededError < StandardError
end

# Default separator used in generated IDs.
DEFAULT_SEPARATOR = "_"
Expand All @@ -33,16 +35,16 @@ class MaxRetriesExceededError < StandardError; end
Id = Struct.new(:key, :prefix, :id, :model_class, :id_field)

class << self
# @!attribute [rw] separator
# @return [String] The separator used in generated IDs.
# @!attribute [rw] alphabet
# @return [String] The alphabet used for generating IDs.
# @!attribute [rw] length
# @return [Integer] The length of the generated ID (excluding prefix and separator).
# @!attribute [rw] max_retries
# @return [Integer] The maximum number of retries when generating a unique ID.
# @!attribute [rw] id_field
# @return [Symbol, nil] The default field to use for storing the ID in models.
# @!attribute [rw] separator
# @return [String] The separator used in generated IDs.
# @!attribute [rw] alphabet
# @return [String] The alphabet used for generating IDs.
# @!attribute [rw] length
# @return [Integer] The length of the generated ID (excluding prefix and separator).
# @!attribute [rw] max_retries
# @return [Integer] The maximum number of retries when generating a unique ID.
# @!attribute [rw] id_field
# @return [Symbol, nil] The default field to use for storing the ID in models.
attr_accessor :separator, :alphabet, :length, :max_retries, :id_field

# Configures the CoolId module.
Expand Down Expand Up @@ -195,7 +197,10 @@ def validate_prefix(value)
# @raise [ArgumentError] If the alphabet includes the separator.
def validate_alphabet(value)
return nil if value.nil?
raise ArgumentError, "Alphabet cannot include the separator '#{CoolId.separator}'" if value.include?(CoolId.separator)
if value.include?(CoolId.separator)
raise ArgumentError, "Alphabet cannot include the separator '#{CoolId.separator}'"
end

value
end
end
Expand Down Expand Up @@ -273,7 +278,10 @@ def set_cool_id
def ensure_cool_id_configured
if self.class.cool_id_setup_required && self.class.cool_id_config.nil?
suggested_prefix = self.class.name.downcase[0..2]
raise NotConfiguredError, "CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_enforce_cool_id' to opt out.\n\ne.g.\n\nclass #{self.class} < ApplicationRecord\n cool_id prefix: \"#{suggested_prefix}\"\nend"
raise(
NotConfiguredError,
"CoolId not configured for #{self.class}. Use 'cool_id' to configure or 'skip_enforce_cool_id' to opt out.\n\ne.g.\n\nclass #{self.class} < ApplicationRecord\n cool_id prefix: \"#{suggested_prefix}\"\nend"
)
end
end
end
Expand Down
Loading