Class: Assistant::TechnicalSupport::CaseEventStream::EvidenceGraph::RecordReader

Inherits:
Object
  • Object
show all
Defined in:
app/services/assistant/technical_support/case_event_stream/evidence_graph/record_reader.rb

Overview

Safely reads both Active Record objects and lightweight replay test records.

Instance Method Summary collapse

Instance Method Details

#array_for(record, attribute) ⇒ Object



11
12
13
# File 'app/services/assistant/technical_support/case_event_stream/evidence_graph/record_reader.rb', line 11

def array_for(record, attribute)
  Array(value_for(record, attribute))
end

#first_present(*values) ⇒ Object



27
28
29
# File 'app/services/assistant/technical_support/case_event_stream/evidence_graph/record_reader.rb', line 27

def first_present(*values)
  values.find(&:present?)
end

#raw_attribute(record, attribute) ⇒ Object



15
16
17
18
19
# File 'app/services/assistant/technical_support/case_event_stream/evidence_graph/record_reader.rb', line 15

def raw_attribute(record, attribute)
  return unless record

  record.respond_to?(:read_attribute) ? record.read_attribute(attribute) : value_for(record, attribute)
end

#unique_records(records) ⇒ Object



5
6
7
8
9
# File 'app/services/assistant/technical_support/case_event_stream/evidence_graph/record_reader.rb', line 5

def unique_records(records)
  Array(records).compact.uniq do |record|
    [record.class.name, value_for(record, :id) || record.object_id]
  end
end

#value_for(record, attribute) ⇒ Object



21
22
23
24
25
# File 'app/services/assistant/technical_support/case_event_stream/evidence_graph/record_reader.rb', line 21

def value_for(record, attribute)
  record&.public_send(attribute) if record.respond_to?(attribute)
rescue ActiveRecord::RecordNotFound
  nil
end