Module: Familia::Features::ExternalIdentifiers::ClassMethods

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

Overview

ExternalIdentifiers::ClassMethods

Instance Method Summary collapse

Instance Method Details

#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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/familia/features/external_identifiers.rb', line 48

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, Familia.dbclient, 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
  extid_lookup.del(extid)
  nil
end

#generate_extid(objid = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/familia/features/external_identifiers.rb', line 30

def generate_extid(objid = nil)
  unless features_enabled.include?(:object_identifiers)
    raise Familia::Problem,
          'ExternalIdentifiers requires ObjectIdentifiers feature'
  end
  return nil if objid.to_s.empty?

  objid_hex = objid.to_s.delete('-')
  external_part = Familia.shorten_to_external_id(objid_hex, base: 36)
  prefix = feature_options(:external_identifiers)[:prefix] || 'ext'
  "#{prefix}_#{external_part}"
end