class Record

Constants

Sections

Public Class Methods

update_or_create(data) click to toggle source
# 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

Private Class Methods

provider_queries(provider_id, effective_date) click to toggle source
# File lib/health-data-standards/models/record.rb, line 159
def self.provider_queries(provider_id, effective_date)
 {'$or' => [provider_query(provider_id, effective_date,effective_date), provider_query(provider_id, nil,effective_date), provider_query(provider_id, effective_date,nil)]}
end
provider_query(provider_id, start_before, end_after) click to toggle source
# File lib/health-data-standards/models/record.rb, line 162
def self.provider_query(provider_id, start_before, end_after)
  {'provider_performances' => {'$elemMatch' => {'provider_id' => provider_id, '$and'=>[{'$or'=>[{'start_date'=>nil},{'start_date'=>{'$lt'=>start_before}}]}, {'$or'=>[{'end_date'=>nil},{'end_date'=> {'$gt'=>end_after}}]}] } }}
end

Public Instance Methods

dedup_record!() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# 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
entries() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/health-data-standards/models/record.rb, line 84
def over_18?
  Time.at(birthdate) < Time.now.years_ago(18)
end
providers() click to toggle source
# File lib/health-data-standards/models/record.rb, line 80
def providers
  provider_performances.map {|pp| pp.provider }
end
shift_dates(date_diff) click to toggle source
# 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