Class: HQMF2JS::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/generator/converter.rb

Class Method Summary collapse

Class Method Details

.generate_map_reduce(hqmf_contents, codes = nil) ⇒ Object



3
4
5
6
7
8
9
10
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
# File 'lib/generator/converter.rb', line 3

def self.generate_map_reduce(hqmf_contents, codes=nil)
  # First compile the CoffeeScript that enables our converted HQMF JavaScript
  hqmf_utils = HQMF2JS::Generator::JS.library_functions

  if !codes
    # Parse the code systems that are mapped to the OIDs we support
    codes_file_path = File.expand_path("../../../test/fixtures/codes/codes.xml", __FILE__)
    codes = HQMF2JS::Generator::CodesToJson.from_xml(codes_file_path)
  end
  codes_json = HQMF2JS::Generator::CodesToJson.hash_to_js(codes)

  # Convert the HQMF document included as a fixture into JavaScript
  converter = HQMF2JS::Generator::JS.new(hqmf_contents)
  data_criteria_code = converter.js_for_data_criteria
  population_criteria_code = HQMF::PopulationCriteria::ALL_POPULATION_CODES.collect do |code|
    converter.js_for(code, nil, true)
  end
  converted_hqmf = [
    data_criteria_code,
    population_criteria_code.join("\n")
  ].join("\n")
  
  # Pretty stock map/reduce functions that call out to our converted HQMF code stored in the functions variable
  map = "function map(patient) {
  var ipp = hqmfjs.IPP(patient);
  if (hqmf.SpecificsManager.validate(ipp)) {
emit('ipp', 1);
if (hqmf.SpecificsManager.validate(hqmfjs.DENEX(patient), ipp)) {
  emit('denex', 1);    
} else {
  var denom = hqmfjs.DENOM(patient);
  if (hqmf.SpecificsManager.validate(denom, ipp)) {
    if (hqmf.SpecificsManager.validate(hqmfjs.NUMER(patient), denom, ipp)) {
      emit('denom', 1);
      emit('numer', 1);
    } else if (hqmf.SpecificsManager.validate(hqmfjs.DENEXCEP(patient), denom, ipp)) {
      emit('excep', 1);
    } else {
      emit('denom', 1);
      emit('antinum', 1);
    }
  }
}
  }
};"
  reduce = "function reduce(bucket, counts) {
  var sum = 0;
  while(counts.hasNext()){
sum += counts.next();
  }
  return sum;
};"
  functions = "#{hqmf_utils}\nvar OidDictionary = #{codes_json};\n#{converted_hqmf}"

  return { :map => map, :reduce => reduce, :functions => functions }
end