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.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/familia/features/external_identifier.rb', line 211 def extid?(guess) return false if guess.to_s.empty? = (:external_identifier) format = [: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
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/familia/features/external_identifier.rb', line 179 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 |