Module: Upkeep::Dependencies

Defined in:
lib/upkeep/dependencies.rb

Defined Under Namespace

Classes: ActiveRecordAttribute, ActiveRecordCollection, ActiveRecordQuery, Base, CookieValue, CurrentAttribute, Identity, RequestValue, Restored, SessionValue, Unknown, WardenUser

Class Method Summary collapse

Class Method Details

.canonical_identity(value) ⇒ Object



441
442
443
444
445
446
447
448
# File 'lib/upkeep/dependencies.rb', line 441

def canonical_identity(value)
  case value
  when nil, true, false, Numeric, String, Symbol
    value
  else
    model_identity(value) || private_fingerprint(value)
  end
end

.from_h(snapshot) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/upkeep/dependencies.rb', line 380

def from_h(snapshot)
  snapshot = symbolize_keys(snapshot)
  source = snapshot.fetch(:source)
  key = symbolize_keys(snapshot.fetch(:key))
   = symbolize_keys(snapshot.fetch(:metadata))

  case source.to_sym
  when :active_record_attribute
    ActiveRecordAttribute.new(
      table: key.fetch(:table),
      id: key.fetch(:id),
      attribute: key.fetch(:attribute),
      model: [:model]
    )
  when :active_record_collection, :active_record_query
    dependency_class = source.to_sym == :active_record_query ? ActiveRecordQuery : ActiveRecordCollection
    dependency_class.new(
      primary_table: .fetch(:primary_table),
      table_columns: .fetch(:table_columns),
      coverage: .fetch(:coverage),
      sql: .fetch(:sql),
      predicates: .fetch(:predicates)
    )
  else
    if snapshot.fetch(:visibility).to_sym == :identity_bound
      Identity.new(
        source: source,
        key: key.fetch(:key),
        value: key.fetch(:value),
        metadata: 
      )
    else
      Restored.new(
        source: source,
        key: key,
        metadata: ,
        visibility: snapshot.fetch(:visibility),
        precision: snapshot.fetch(:precision)
      )
    end
  end
end

.identity_absent_for?(dependency, name) ⇒ Boolean

Returns:

  • (Boolean)


459
460
461
462
463
464
465
# File 'lib/upkeep/dependencies.rb', line 459

def identity_absent_for?(dependency, name)
  absent_by_name = (dependency, :identity_absent_by_name) || {}
  absent_by_name = absent_by_name.transform_keys(&:to_s) if absent_by_name.respond_to?(:transform_keys)
  return absent_by_name.fetch(name.to_s) if absent_by_name.key?(name.to_s)

  !partitioning_identity?(dependency)
end

.metadata_value(dependency, key) ⇒ Object



471
472
473
474
475
476
477
478
479
# File 'lib/upkeep/dependencies.rb', line 471

def (dependency, key)
  return unless dependency.respond_to?(:metadata)

  if dependency..key?(key)
    dependency..fetch(key)
  elsif dependency..key?(key.to_s)
    dependency..fetch(key.to_s)
  end
end

.model_identity(value) ⇒ Object



423
424
425
426
427
428
429
# File 'lib/upkeep/dependencies.rb', line 423

def model_identity(value)
  return nil unless value

  if value.respond_to?(:id) && value.class.respond_to?(:name)
    { model: value.class.name, id: value.id }
  end
end

.model_metadata(value) ⇒ Object



431
432
433
434
435
436
437
438
439
# File 'lib/upkeep/dependencies.rb', line 431

def (value)
  return {} unless value

  {
    model: value.class.name,
    table: value.class.respond_to?(:table_name) ? value.class.table_name : nil,
    id: value.respond_to?(:id) ? value.id : nil
  }.compact
end

.nil_identity?(dependency) ⇒ Boolean

Returns:

  • (Boolean)


467
468
469
# File 'lib/upkeep/dependencies.rb', line 467

def nil_identity?(dependency)
  dependency.respond_to?(:nil_identity?) && dependency.nil_identity?
end

.partitioning_identity?(dependency) ⇒ Boolean

Returns:

  • (Boolean)


450
451
452
453
454
455
456
457
# File 'lib/upkeep/dependencies.rb', line 450

def partitioning_identity?(dependency)
  return false unless dependency.identity?

  flag = (dependency, :partitioning_identity)
  return flag if [true, false].include?(flag)

  !nil_identity?(dependency)
end

.private_fingerprint(value) ⇒ Object



481
482
483
# File 'lib/upkeep/dependencies.rb', line 481

def private_fingerprint(value)
  Digest::SHA256.hexdigest(JSON.generate(private_fingerprint_payload(value)))[0, 16]
end

.private_fingerprint_payload(value) ⇒ Object



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/upkeep/dependencies.rb', line 485

def private_fingerprint_payload(value)
  case value
  when nil, true, false, Numeric, String
    [value.class.name, value]
  when Symbol
    ["Symbol", value.to_s]
  when Array
    ["Array", value.map { |item| private_fingerprint_payload(item) }]
  when Hash
    entries = value.keys.sort_by { |key| JSON.generate(private_fingerprint_payload(key)) }.map do |key|
      [private_fingerprint_payload(key), private_fingerprint_payload(value.fetch(key))]
    end
    ["Hash", entries]
  else
    identity = model_identity(value)
    identity ? ["Model", identity] : ["Object", value.class.name, value.inspect]
  end
end

.symbolize_keys(value) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/upkeep/dependencies.rb', line 504

def symbolize_keys(value)
  case value
  when Hash
    value.each_with_object({}) do |(key, nested_value), result|
      normalized_key = key.respond_to?(:to_sym) ? key.to_sym : key
      result[normalized_key] = symbolize_keys(nested_value)
    end
  when Array
    value.map { |nested_value| symbolize_keys(nested_value) }
  else
    value
  end
end