Class: CoolId::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cool_id.rb

Overview

Configuration class for CoolId generation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, model_class:, length: nil, alphabet: nil, max_retries: nil, id_field: nil) ⇒ Config

Initializes a new Config instance.

Parameters:

  • prefix (String)

    The prefix for generated IDs.

  • model_class (Class)

    The ActiveRecord model class.

  • length (Integer, nil) (defaults to: nil)

    The length of the generated ID (excluding prefix and separator).

  • alphabet (String, nil) (defaults to: nil)

    The alphabet to use for generating IDs.

  • max_retries (Integer, nil) (defaults to: nil)

    The maximum number of retries when generating a unique ID.

  • id_field (Symbol, nil) (defaults to: nil)

    The field to use for storing the ID in the model.



173
174
175
176
177
178
179
180
# File 'lib/cool_id.rb', line 173

def initialize(prefix:, model_class:, length: nil, alphabet: nil, max_retries: nil, id_field: nil)
  @prefix = validate_prefix(prefix)
  @length = length
  @alphabet = validate_alphabet(alphabet)
  @max_retries = max_retries
  @model_class = model_class
  @id_field = id_field
end

Instance Attribute Details

#alphabetString? (readonly)

Returns The alphabet to use for generating IDs.

Returns:

  • (String, nil)

    The alphabet to use for generating IDs.



155
156
157
# File 'lib/cool_id.rb', line 155

def alphabet
  @alphabet
end

#id_fieldSymbol? (readonly)

Returns The field to use for storing the ID in the model.

Returns:

  • (Symbol, nil)

    The field to use for storing the ID in the model.



164
165
166
# File 'lib/cool_id.rb', line 164

def id_field
  @id_field
end

#lengthInteger? (readonly)

Returns The length of the generated ID (excluding prefix and separator).

Returns:

  • (Integer, nil)

    The length of the generated ID (excluding prefix and separator).



152
153
154
# File 'lib/cool_id.rb', line 152

def length
  @length
end

#max_retriesInteger? (readonly)

Returns The maximum number of retries when generating a unique ID.

Returns:

  • (Integer, nil)

    The maximum number of retries when generating a unique ID.



158
159
160
# File 'lib/cool_id.rb', line 158

def max_retries
  @max_retries
end

#model_classClass (readonly)

Returns The ActiveRecord model class associated with this configuration.

Returns:

  • (Class)

    The ActiveRecord model class associated with this configuration.



161
162
163
# File 'lib/cool_id.rb', line 161

def model_class
  @model_class
end

#prefixString (readonly)

Returns The prefix for generated IDs.

Returns:

  • (String)

    The prefix for generated IDs.



149
150
151
# File 'lib/cool_id.rb', line 149

def prefix
  @prefix
end