Class: HQMF1::Document

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/hqmf-parser/1.0/document.rb

Overview

Class representing an HQMF document

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#attr_val, #check_nil_conjunction_on_child, #clean_json, #clean_json_recursive

Methods included from HQMF::Conversion::Utilities

#build_hash, #check_equality, #json_array, #openstruct_to_json

Constructor Details

#initialize(hqmf_contents) ⇒ Document

Create a new HQMF1::Document instance by parsing the supplied contents

Parameters:

  • hqmf_contents (String)

    the contents of an HQMF v1.0 document



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hqmf-parser/1.0/document.rb', line 11

def initialize(hqmf_contents)
  
  @doc = Document.parse(hqmf_contents)
  occurrence_counters = {}
  @data_criteria = @doc.xpath('//cda:section[cda:code/@code="57025-9"]/cda:entry').collect do |entry|
    DataCriteria.new(entry, occurrence_counters)
  end

  @supplemental= @doc.xpath('//cda:section[cda:code/@code="69670-8"]/cda:entry').collect do |entry|
    DataCriteria.new(entry, occurrence_counters)
  end

  @data_criteria.concat @supplemental
  backfill_derived_code_lists
  
  @attributes = @doc.xpath('//cda:subjectOf/cda:measureAttribute').collect do |attr|
    Attribute.new(attr)
  end
  @population_criteria = @doc.xpath('//cda:section[cda:code/@code="57026-7"]/cda:entry').collect do |attr|
    PopulationCriteria.new(attr, self)
  end
  observations = @doc.xpath('//cda:section[cda:code/@code="57027-5"]/cda:entry').collect do |attr|
    Observation.new(attr, self)
  end
  @population_criteria.concat(observations)

  @stratification = @doc.xpath('//cda:section[cda:code/@code="69669-0"]/cda:entry').collect do |attr|
    PopulationCriteria.new(attr, self)
  end
  
  if (@stratification and !@stratification.empty?)
    initial_populations = @population_criteria.select {|pc| pc.code.starts_with? 'IPP'}
    initial_populations.each do |population|
      
      @stratification.each do |stratification|
        new_population = HQMF1::PopulationCriteria.new(population.entry, population.doc)
        new_population.hqmf_id = new_population.id
        new_population.stratification_id = stratification.id
        new_population.id = "#{new_population.id}_#{stratification.id}"
        ids = stratification.preconditions.map(&:id)
        new_population.preconditions.delete_if {|precondition| ids.include? precondition.id}
        new_population.preconditions.concat(stratification.preconditions)
        new_population.preconditions.rotate!(-1*stratification.preconditions.size)
        @population_criteria << new_population
      end
      
    end
    
  end
  
  @hqmf_set_id = @doc.at_xpath('//cda:setId/@root').value.upcase
  @hqmf_id = @doc.at_xpath('//cda:id/@root').value.upcase
  @hqmf_version_number = @doc.at_xpath('//cda:versionNumber/@value').value.to_i
  
end

Instance Attribute Details

#hqmf_idObject (readonly)

Returns the value of attribute hqmf_id.



7
8
9
# File 'lib/hqmf-parser/1.0/document.rb', line 7

def hqmf_id
  @hqmf_id
end

#hqmf_set_idObject (readonly)

Returns the value of attribute hqmf_set_id.



7
8
9
# File 'lib/hqmf-parser/1.0/document.rb', line 7

def hqmf_set_id
  @hqmf_set_id
end

#hqmf_version_numberObject (readonly)

Returns the value of attribute hqmf_version_number.



7
8
9
# File 'lib/hqmf-parser/1.0/document.rb', line 7

def hqmf_version_number
  @hqmf_version_number
end

Class Method Details

.parse(hqmf_contents) ⇒ Nokogiri::XML::Document

Parse an XML document from the supplied contents

Returns:

  • (Nokogiri::XML::Document)


138
139
140
141
142
# File 'lib/hqmf-parser/1.0/document.rb', line 138

def self.parse(hqmf_contents)
  doc = Nokogiri::XML(hqmf_contents)
  doc.root.add_namespace_definition('cda', 'urn:hl7-org:v3')
  doc
end

Instance Method Details

#all_attributesArray

Get all the attributes defined by the measure

Returns:

  • (Array)

    an array of HQMF1::Attribute



81
82
83
# File 'lib/hqmf-parser/1.0/document.rb', line 81

def all_attributes
  @attributes
end

#all_data_criteriaArray

Get all the data criteria defined by the measure

Returns:

  • (Array)

    an array of HQMF1::DataCriteria describing the data elements used by the measure



125
126
127
# File 'lib/hqmf-parser/1.0/document.rb', line 125

def all_data_criteria
  @data_criteria
end

#all_population_criteriaArray

Get all the population criteria defined by the measure

Returns:

  • (Array)

    an array of HQMF1::PopulationCriteria



101
102
103
# File 'lib/hqmf-parser/1.0/document.rb', line 101

def all_population_criteria
  @population_criteria
end

#attribute(id) ⇒ HQMF1::Attribute

Get a specific attribute by id.

Parameters:

  • id (String)

    the attribute identifier

Returns:

  • (HQMF1::Attribute)

    the matching attribute, raises an Exception if not found



88
89
90
# File 'lib/hqmf-parser/1.0/document.rb', line 88

def attribute(id)
  find(@attributes, :id, id)
end

#attribute_for_code(code) ⇒ HQMF1::Attribute

Get a specific attribute by code.

Parameters:

  • code (String)

    the attribute code

Returns:

  • (HQMF1::Attribute)

    the matching attribute, raises an Exception if not found



95
96
97
# File 'lib/hqmf-parser/1.0/document.rb', line 95

def attribute_for_code(code)
  find(@attributes, :code, code)
end

#backfill_derived_code_listsObject

if the data criteria is derived from another criteria, then we want to grab the properties from the derived criteria this is typically the case with Occurrence A, Occurrence B type data criteria



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/hqmf-parser/1.0/document.rb', line 146

def backfill_derived_code_lists
  data_criteria_by_id = {}
  @data_criteria.each {|criteria| data_criteria_by_id[criteria.id] = criteria}
  @data_criteria.each do |criteria|
    if (criteria.derived_from)
      derived_from = data_criteria_by_id[criteria.derived_from]
      criteria.definition = derived_from.definition
      criteria.status = derived_from.status
      criteria.code_list_id = derived_from.code_list_id
    end
  end
end

#data_criteria(id) ⇒ HQMF1::DataCriteria

Get a specific data criteria by id.

Parameters:

  • id (String)

    the data criteria identifier

Returns:



132
133
134
# File 'lib/hqmf-parser/1.0/document.rb', line 132

def data_criteria(id)
  val = find(@data_criteria, :id, id) || raise("unknown data criteria #{id}")
end

#descriptionString

Get the description of the measure

Returns:

  • (String)

    the description



75
76
77
# File 'lib/hqmf-parser/1.0/document.rb', line 75

def description
  @doc.at_xpath('cda:QualityMeasureDocument/cda:text').inner_text
end

#population_criteria(id) ⇒ HQMF1::PopulationCriteria

Get a specific population criteria by id.

Parameters:

  • id (String)

    the population identifier

Returns:



112
113
114
# File 'lib/hqmf-parser/1.0/document.rb', line 112

def population_criteria(id)
  find(@population_criteria, :id, id)
end

#population_criteria_for_code(code) ⇒ HQMF1::PopulationCriteria

Get a specific population criteria by code.

Parameters:

  • code (String)

    the population criteria code

Returns:



119
120
121
# File 'lib/hqmf-parser/1.0/document.rb', line 119

def population_criteria_for_code(code)
  find(@population_criteria, :code, code)
end

#stratificationObject



105
106
107
# File 'lib/hqmf-parser/1.0/document.rb', line 105

def stratification
  @stratification
end

#titleString

Get the title of the measure

Returns:



69
70
71
# File 'lib/hqmf-parser/1.0/document.rb', line 69

def title
  @doc.at_xpath('cda:QualityMeasureDocument/cda:title').inner_text
end

#to_jsonObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/hqmf-parser/1.0/document.rb', line 159

def to_json
  json = build_hash(self, [:title, :description, :hqmf_id, :hqmf_set_id, :hqmf_version_number])
  
  json[:data_criteria] = {}
  @data_criteria.each do |criteria|
    criteria_json = criteria.to_json
    # check if the key already exists... if it does redefine the key
    if (json[:data_criteria][criteria_json.keys.first])
      criteria_json = {"#{criteria_json.keys.first}_#{HQMF::Counter.instance.next}" => criteria_json.values.first}
    end
    json[:data_criteria].merge! criteria_json
  end
  
  json[:metadata] = {}
  json[:attributes] = {}
  @attributes.each do |attribute|
    if (attribute.id)
      json[:attributes].merge! attribute.to_json
    else
      json[:metadata].merge! attribute.to_json
    end
      
  end

  json[:logic] = {}
  counters = {}
  @population_criteria.each do |population|
    population_json = population.to_json
    key = population_json.keys.first
    if json[:logic][key]
      counters[key] ||= 0
      counters[key] += 1
      population_json["#{key}_#{counters[key]}"] = population_json[key]
      population_json.delete(key)
    end
    json[:logic].merge! population_json
  end
  
  clean_json_recursive(json)
  json
end