Module: HealthDataStandards::Export::ViewHelper

Included in:
RenderingContext
Defined in:
lib/health-data-standards/export/view_helper.rb

Instance Method Summary collapse

Instance Method Details

#clean_hash_code_system(codes) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/health-data-standards/export/view_helper.rb', line 183

def clean_hash_code_system(codes)
  if codes['title']
    return codes['code'] + " (#{codes['title']})"
  else
    return codes['code']
  end
end

#clean_hash_id(codes, hashkey) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/health-data-standards/export/view_helper.rb', line 191

def clean_hash_id(codes, hashkey)
  value = codes[hashkey]
  if value.nil?
    return 'none'
  elsif value.is_a? Hash
    hash_result = convert_field_to_hash(hashkey, value)
    if hash_result.is_a? Hash
      return hash_result.map {|key, value| "#{key}: #{value}"}.join(' ')
    else
      return hash_result
    end
  elsif value.is_a? Array
    return value.join(', ')
  else
    return convert_field_to_hash(hashkey, value)
  end
end

#code_display(entry, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/health-data-standards/export/view_helper.rb', line 4

def code_display(entry, options={})
  options['tag_name'] ||= 'code'
  options['attribute'] ||= :codes
  options['exclude_null_flavor'] ||= false
  # allowing wild card matching of any code system for generic templates
  # valueset filtering should filter out a decent code
  # TODO: Can we add nil? logic to the function?  Make it so that we don't have to include the preferred_code_sets['*'] if we want all?
  pcs = if options['preferred_code_sets'] && options['preferred_code_sets'].index("*")
    # all of the code_systems that we know about
    HealthDataStandards::Util::CodeSystemHelper::CODE_SYSTEMS.values | HealthDataStandards::Util::CodeSystemHelper::CODE_SYSTEM_ALIASES.keys
  else
    options['preferred_code_sets']
  end
  create_code_string(entry, entry.preferred_code(pcs, options['attribute'], options['value_set_map']), options)
end

#convert_field_to_hash(field, codes) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/health-data-standards/export/view_helper.rb', line 147

def convert_field_to_hash(field, codes)
  if codes.is_a? Array
    return codes.collect{ |code| convert_field_to_hash(field, convert_field_to_hash(field, code))}.join("<br>")
  end

  if (codes.is_a? Hash)
    clean_hash = {}

    if codes['codeSystem']
      clean_hash[codes['codeSystem']] = clean_hash_code_system(codes)
    elsif codes['_id']
      codes.keys.reject {|key| ['_id'].include? key}.each do |hashkey|
        clean_hash[hashkey.titleize] = clean_hash_id(codes, hashkey)
      end
    elsif codes['scalar']
      return "#{codes['scalar']} #{codes['units']}"
    else
      return codes.map do |hashcode_set, hashcodes| 
        if hashcodes.is_a? Hash
          "#{hashcode_set}: #{convert_field_to_hash(hashcode_set, hashcodes)}"
        else
          "#{hashcode_set}: #{(hashcodes.respond_to? :join) ? hashcodes.join(', ') : hashcodes.to_s}"
        end
      end.join(' ')
    end
      
    clean_hash
  else
    if codes && (field.match(/Time$/) || field.match(/\_time$/) || field.match(/Date$/)) 
      Entry.time_to_s(codes)
    else
      codes.to_s
    end
  end
end

#create_code_display_string(entry, preferred_code, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/health-data-standards/export/view_helper.rb', line 37

def create_code_display_string(entry, preferred_code, options={})
  code_string = nil
  if entry['negationInd'] == true
    code_system_oid = HealthDataStandards::Util::CodeSystemHelper.oid_for_code_system(preferred_code['code_set'])
    code_string = "<#{options['tag_name']} code=\"#{preferred_code['code']}\" codeSystem=\"#{code_system_oid}\"/>"
  elsif preferred_code
    code_system_oid = HealthDataStandards::Util::CodeSystemHelper.oid_for_code_system(preferred_code['code_set'])
    code_string = "<#{options['tag_name']} code=\"#{preferred_code['code']}\" codeSystem=\"#{code_system_oid}\" #{options['extra_content']}>"
  else
    code_string = "<#{options['tag_name']} "
    code_string += "nullFlavor=\"UNK\" " unless options["exclude_null_flavor"]
    code_string += "#{options['extra_content']}>"
  end
  code_string
end

#create_code_string(entry, preferred_code, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/health-data-standards/export/view_helper.rb', line 20

def create_code_string(entry, preferred_code, options={})
  
  code_string = create_code_display_string(entry, preferred_code, options)
  
  unless entry['negationInd'] == true
    code_string += "<originalText>#{ERB::Util.html_escape entry.description}</originalText>" if entry.respond_to?(:description)

    code_string += create_laterality_code_string(entry, options) if options["laterality"]
    
    code_string += create_translations_code_string(entry, options) if options["attribute"] == :codes && entry.respond_to?(:translation_codes)
    
    code_string += "</#{options['tag_name']}>"
  end

  code_string
end

#create_laterality_code_string(entry, options = {}) ⇒ Object



53
54
55
56
57
# File 'lib/health-data-standards/export/view_helper.rb', line 53

def create_laterality_code_string(entry, options={})
  code_string = "\n<!-- QDM Attribute: Laterality -->\n<qualifier>\n<name code='182353008' codeSystem='2.16.840.1.113883.6.96' displayName='Side' />\n"
  code_string += "<value xsi:type='CD' code='#{options['laterality']['code']}' displayName='#{options['laterality']['title']}' sdtc:valueSet='#{oid_for_code(entry.laterality,field_oids['LATERALITY'])}'
                 codeSystem='#{HealthDataStandards::Util::CodeSystemHelper.oid_for_code_system(options['laterality']['code_system'])}'/>\n</qualifier>\n"
end

#create_translations_code_string(entry, options = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/health-data-standards/export/view_helper.rb', line 59

def create_translations_code_string(entry, options={})
  code_string = ''
  entry.translation_codes(options['preferred_code_sets'], options['value_set_map']).each do |translation|
    code_string += "<translation code=\"#{translation['code']}\" codeSystem=\"#{HealthDataStandards::Util::CodeSystemHelper.oid_for_code_system(translation['code_set'])}\"/>\n"
  end
  code_string
end

#dose_quantity(codes, dose) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/health-data-standards/export/view_helper.rb', line 95

def dose_quantity(codes, dose)
  # this previously paid attention to precoordinated medications like RxNorm and CVX
  # and would ignore medication dosage if the code was one of those. However, that does
  # not seem accurate. From conversation with Dave Czulada on 8/8:
  # "When a medication code is "precoordinated" one should not be able to specify a UOM
  # that is other than capsule/dose/tablet/unit. For example, you wouldn't say 20 MG of
  # RxNorm 1000048 (Doxepin Hydrochloride 10 MG Oral Capsule).  You would say 2 capsules
  # of RxNorm 1000048 or 2 units of RxNorm 1000048."
  #
  # In order to enable a user to say "2 units" or "2 capsules", we need to allow the
  # dosage to be present even if an RxNorm or CVX code is used. It is the user's job
  # to confirm that they aren't contradicting the coded value.
  if dose[:scalar].present?
    return "value='#{dose[:scalar]}' unit='#{dose[:units]}'"
  elsif dose[:value].present?
    return "value='#{dose[:value]}' unit='#{dose[:unit]}'"
  else
    return "value='1' unit='1'"
  end
end

#fulfillment_quantity(codes, fulfillmentHistory, dose) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/health-data-standards/export/view_helper.rb', line 78

def fulfillment_quantity(codes, fulfillmentHistory, dose)
  if (codes["RxNorm"].present?)
    doses = (fulfillmentHistory.quantity_dispensed['value'].to_f / dose['value'].to_f ).to_i
    return "value='#{doses}' unit='1'"
  else
    return "value='#{fulfillmentHistory.quantity_dispensed['value']}' unit='#{fulfillmentHistory.quantity_dispensed['unit']}'"
  end
end

#identifier_for(obj) ⇒ Object



143
144
145
# File 'lib/health-data-standards/export/view_helper.rb', line 143

def identifier_for(obj)
  Digest::MD5.hexdigest(obj.to_s).upcase
end

#is_bool?(str) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/health-data-standards/export/view_helper.rb', line 139

def is_bool?(str)
  return ["true","false"].include? (str || "").downcase
end

#is_num?(str) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'lib/health-data-standards/export/view_helper.rb', line 131

def is_num?(str)
  Float(str || "")
rescue ArgumentError
  false
else
  true
end

#status_code_for(entry) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/health-data-standards/export/view_helper.rb', line 67

def status_code_for(entry)
  case entry.status.to_s.downcase
  when 'active'
    '55561003'
  when 'inactive'
    '73425007'
  when 'resolved'
    '413322009'
  end
end

#time_if_not_nil(*args) ⇒ Object



127
128
129
# File 'lib/health-data-standards/export/view_helper.rb', line 127

def time_if_not_nil(*args)
  args.compact.map {|t| Time.at(t).utc}.first
end

#ucum_for_dose_quantity(unit) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/health-data-standards/export/view_helper.rb', line 116

def ucum_for_dose_quantity(unit)
  case unit
  when 'capsule(s)'
    '{Capsule}'
  when 'tablet(s)'
    '{tbl}'
  else
    unit
  end
end

#value_or_null_flavor(time) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/health-data-standards/export/view_helper.rb', line 87

def value_or_null_flavor(time)
  if time 
    return "value='#{Time.at(time).utc.to_formatted_s(:number)}'"
  else 
   return "nullFlavor='UNK'"
 end
end