Class: QME::MapReduce::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/qme/map/map_reduce_builder.rb

Overview

Builds Map and Reduce functions for a particular measure

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, measure_def, params) ⇒ Builder

Create a new Builder

Parameters:

  • measure_def (Hash)

    a JSON hash of the measure, field values may contain Erb directives to inject the values of supplied parameters into the map function

  • params (Hash)

    a hash of parameter names (String or Symbol) and their values



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/qme/map/map_reduce_builder.rb', line 47

def initialize(db, measure_def, params)
  @id = measure_def['id']
  @params = {}
  @db = db
  
  # normalize parameters hash to accept either symbol or string keys
  params.each do |name, value|
    @params[name.to_s] = value
  end
  @measure_def = measure_def
  @measure_def['parameters'] ||= {}
  @measure_def['parameters'].each do |parameter, value|
    if !@params.has_key?(parameter)
      raise "No value supplied for measure parameter: #{parameter}"
    end
  end
  # if the map function is specified then replace any erb templates with their values
  # taken from the supplied params
  # always true for actual measures, not always true for unit tests
  if (@measure_def['map_fn'])
    template = ERB.new(@measure_def['map_fn'])
    context = Context.new(@db, @params)
    @measure_def['map_fn'] = template.result(context.get_binding)
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/qme/map/map_reduce_builder.rb', line 9

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/qme/map/map_reduce_builder.rb', line 9

def params
  @params
end

Instance Method Details

#finalize_functionString

Get the reduce function for the measure, this is a simple wrapper for the reduce utility function specified in map-reduce-utils.js

Returns:

  • (String)

    the reduce function



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/qme/map/map_reduce_builder.rb', line 83

def finalize_function
  reduce = 
  "function (key, value) { 
    var patient = value;
    patient.measure_id = \"#{@measure_def['id']}\";\n"
  if @params['test_id'] && @params['test_id'].class==BSON::ObjectId
    reduce += "  patient.test_id = new ObjectId(\"#{@params['test_id']}\");\n"
  end
  if @measure_def['sub_id']
    reduce += "  patient.sub_id = \"#{@measure_def['sub_id']}\";\n"
  end
    
  reduce += "patient.effective_date = #{@params['effective_date']};
             if (patient.provider_performances) {
               var tmp = [];
               for(var i=0; i<patient.provider_performances.length; i++) {
                 var value = patient.provider_performances[i];
                 if ((value['start_date'] <= #{@params['effective_date']} || value['start_date'] == null) && (value['end_date'] >= #{@params['effective_date']} || value['end_date'] == null))
                 tmp.push(value);
               }
               if (tmp.length == 0) tmp = null;
               patient.provider_performances = tmp;
             }
             return patient;}"
  
  reduce
end

#map_functionString

Get the map function for the measure

Returns:

  • (String)

    the map function



75
76
77
# File 'lib/qme/map/map_reduce_builder.rb', line 75

def map_function
  @measure_def['map_fn']
end