Class: Record

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Mongoid::Attributes::Dynamic, Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/health-data-standards/models/record.rb

Constant Summary collapse

Sections =
[:allergies, :care_goals, :conditions, :encounters, :immunizations, :medical_equipment,
:medications, :procedures, :results, :communications, :family_history, :social_history, :vital_signs, :support, :advance_directives,
:insurance_providers, :functional_statuses, :care_experiences, :assessments, :adverse_events, :participations]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update_or_create(data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/health-data-standards/models/record.rb', line 69

def self.update_or_create(data)
  existing = Record.where(medical_record_number: data.medical_record_number).first
  if existing
    existing.update_attributes!(data.attributes.except('_id'))
    existing
  else
    data.save!
    data
  end
end

Instance Method Details

#dedup_record!Object



140
141
142
# File 'lib/health-data-standards/models/record.rb', line 140

def dedup_record!
  Record::Sections.each {|section| self.dedup_section!(section)}
end

#dedup_section!(section) ⇒ Object



137
138
139
# File 'lib/health-data-standards/models/record.rb', line 137

def dedup_section!(section)
  [:encounters, :procedures, :results].include?(section) ? dedup_section_merging_codes_and_values!(section) : dedup_section_ignoring_content!(section)
end

#dedup_section_ignoring_content!(section) ⇒ Object

Remove duplicate entries from a section based on cda_identifier or id. This method may lose information because it does not compare entries based on clinical content



111
112
113
114
115
116
117
118
119
# File 'lib/health-data-standards/models/record.rb', line 111

def dedup_section_ignoring_content!(section)
  unique_entries = self.send(section).uniq do |entry|
    entry.references.each do |ref|
      ref.resolve_referenced_id
    end
    entry.identifier
  end
  self.send("#{section}=", unique_entries)
end

#dedup_section_merging_codes_and_values!(section) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/health-data-standards/models/record.rb', line 120

def dedup_section_merging_codes_and_values!(section)
  unique_entries = {}
  self.send(section).each do |entry|
    entry.references.each do |ref|
      ref.resolve_referenced_id
    end
    if unique_entries[entry.identifier]
      unique_entries[entry.identifier].codes = unique_entries[entry.identifier].codes.deep_merge(entry.codes){ |key, old, new| Array.wrap(old) + Array.wrap(new) }
      unique_entries[entry.identifier].values.concat(entry.values)
    else
      unique_entries[entry.identifier] = entry
    end

  end
  self.send("#{section}=", unique_entries.values)
end

#entriesObject



100
101
102
103
104
# File 'lib/health-data-standards/models/record.rb', line 100

def entries
  Sections.map do |section|
    self.send(section)
  end.flatten
end

#entries_for_oid(oid) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/health-data-standards/models/record.rb', line 88

def entries_for_oid(oid)
  matching_entries_by_section = Sections.map do |section|
    section_entries = self.send(section)
    if section_entries.present?
      section_entries.find_all { |entry| (entry.respond_to? :oid) ? entry.oid == oid : false}
    else
      []
    end
  end
  matching_entries_by_section.flatten
end

#over_18?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/health-data-standards/models/record.rb', line 84

def over_18?
  Time.at(birthdate) < Time.now.years_ago(18)
end

#providersObject



80
81
82
# File 'lib/health-data-standards/models/record.rb', line 80

def providers
  provider_performances.map {|pp| pp.provider }
end

#shift_dates(date_diff) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/health-data-standards/models/record.rb', line 144

def shift_dates(date_diff)
  self.birthdate = (self.birthdate.nil?) ? nil : self.birthdate + date_diff
  self.deathdate = (self.deathdate.nil?) ? nil : self.deathdate + date_diff
  self.provider_performances.each {|pp| pp.shift_dates(date_diff)}
  Sections.each do |sec|
    (self.send sec || []).each do |ent|
      ent.shift_dates(date_diff)
    end

  end

end