Module: Familia::Features::ExternalIdentifier::ModelClassMethods
- Defined in:
- lib/familia/features/external_identifier.rb
Overview
ExternalIdentifier::ModelClassMethods
Instance Method Summary collapse
-
#extid?(guess) ⇒ Boolean
Check if a string matches the extid format for the Horreum class.
-
#find_by_extid(extid) ⇒ Object?
Find an object by its external identifier.
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.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/familia/features/external_identifier.rb', line 225 def extid?(guess) return false if guess.to_s.empty? = (:external_identifier) format = [:format] || DEFAULT_FORMAT # The feature declaration validates that the template has exactly one # canonical placeholder, so this split mirrors minting exactly. prefix, suffix = format.split(CANONICAL_PLACEHOLDER, 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/ !!(guess =~ pattern) end |
#find_by_extid(extid) ⇒ Object?
Find an object by its external identifier
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/familia/features/external_identifier.rb', line 193 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 |