Module: Familia::Features::ObjectIdentifier::ModelClassMethods

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

Instance Method Summary collapse

Instance Method Details

#find_by_objid(objid) ⇒ Object?

Find an object by its object identifier

Parameters:

  • objid (String)

    The object identifier to search for

Returns:

  • (Object, nil)

    The object if found, nil otherwise



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/familia/features/object_identifier.rb', line 258

def find_by_objid(objid)
  return nil if objid.to_s.empty?

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

  # Look up the primary ID from the external ID mapping
  primary_id = objid_lookup[objid]

  # If there is no mapping for this instance's objid, perhaps
  # the object dbkey is already using the objid.
  primary_id = objid if primary_id.nil?

  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.
  # objid_lookup.remove_field(objid)
  nil
end

#generate_object_identifierString

Generate a new object identifier using the configured strategy

Returns:

  • (String)

    A new unique identifier



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/familia/features/object_identifier.rb', line 230

def generate_object_identifier
  options = feature_options(:object_identifier)
  generator = options[:generator] || DEFAULT_GENERATOR

  case generator
  when :uuid_v7
    SecureRandom.uuid_v7
  when :uuid_v4
    SecureRandom.uuid_v4
  when :hex
    Familia.generate_id(16)
  when Proc
    generator.call
  else
    unless generator.respond_to?(:call)
      raise Familia::Problem, "Invalid object identifier generator: #{generator.inspect}"
    end

    generator.call

  end
end