Module: Familia::Features::ExternalIdentifier::ModelClassMethods

Defined in:
lib/familia/features/external_identifier.rb

Overview

ExternalIdentifier::ModelClassMethods

Instance Method Summary collapse

Instance Method Details

#extid?(guess) ⇒ Boolean

Check if a string matches the extid format for the Horreum class. The specific class is important b/c each one can have its own custom prefix, like ext_.

The validator accepts a reasonable range of ID lengths (20-32 characters) to accommodate potential changes to the entropy or encoding while maintaining security. The current implementation generates exactly 25 base36 characters from 16 bytes (128 bits), but this flexibility allows for future adjustments without breaking validation.

Parameters:

  • guess (String)

    The string to check

Returns:

  • (Boolean)

    true if the guess matches the extid format, false otherwise



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/familia/features/external_identifier.rb', line 191

def extid?(guess)
  return false if guess.to_s.empty?

  options = feature_options(:external_identifier)
  format = options[:format] || 'ext_%{id}'

  # Extract prefix and suffix from format
  return false unless format.include?('%{id}')
  prefix, suffix = format.split('%{id}', 2)

  # Build regex pattern to match the extid format
  # Accept 20-32 base36 characters to allow for entropy/encoding variations
  # Current generation: 16 bytes -> base36 -> 25 chars (rjust with '0')
  pattern = /\A#{Regexp.escape(prefix)}[0-9a-z]{20,32}#{Regexp.escape(suffix)}\z/i

  !!(guess =~ pattern)
end

#find_by_extid(extid) ⇒ Object?

Find an object by its external identifier

Parameters:

  • extid (String)

    The external identifier to search for

Returns:

  • (Object, nil)

    The object if found, nil otherwise



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/familia/features/external_identifier.rb', line 159

def find_by_extid(extid)
  return nil if extid.to_s.empty?

  if Familia.debug?
    reference = caller(1..1).first
    Familia.trace :FIND_BY_EXTID, nil, extid, reference
  end

  # Look up the primary ID from the external ID mapping
  primary_id = extid_lookup[extid]
  return nil if primary_id.nil?

  # Find the object by its primary ID
  find_by_id(primary_id)
rescue Familia::NotFound
  # If the object was deleted but mapping wasn't cleaned up
  # we could autoclean here, as long as we log it.
  # extid_lookup.remove_field(extid)
  nil
end