Module: HealthDataStandards::Export::ViewHelper

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

Instance Method Summary collapse

Instance Method Details

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



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
# 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
  code_string = nil
  preferred_code = entry.preferred_code(options['preferred_code_sets'], options['attribute'], options['value_set_map'])
  if 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
  
  
  
  if options["attribute"] == :codes && entry.respond_to?(:translation_codes)
    code_string += "<originalText>#{ERB::Util.html_escape entry.description}</originalText>" if entry.respond_to?(:description)
    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
  end
  
  code_string += "</#{options['tag_name']}>"
  code_string
end

#convert_field_to_hash(field, codes) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/health-data-standards/export/view_helper.rb', line 88

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']
      if codes['title']
        clean_hash[codes['codeSystem']] = codes['code'] + " (#{codes['title']})"
      else
        clean_hash[codes['codeSystem']] = codes['code']
      end
    elsif codes['_id']
      codes.keys.reject {|key| ['_id'].include? key}.each do |hashkey|
        value = codes[hashkey]
        if value.nil?
          clean_hash[hashkey.titleize] = 'none'
        elsif value.is_a? Hash
          hash_result = convert_field_to_hash(hashkey, value)
          if hash_result.is_a? Hash
            clean_hash[hashkey.titleize] = hash_result.map {|key, value| "#{key}: #{value}"}.join(' ')
          else
            clean_hash[hashkey.titleize] = hash_result
          end
        elsif value.is_a? Array
          clean_hash[hashkey.titleize] = value.join(', ')
        else
          clean_hash[hashkey.titleize] = convert_field_to_hash(hashkey, value)
        end
      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

#dose_quantity(codes, dose) ⇒ Object



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

def dose_quantity(codes, dose)
  if (codes["RxNorm"].present?)
    return "value='1'"
  else
    return "value=#{dose['value']} unit=#{dose['unit']}" 
  end
end

#fulfillment_quantity(codes, fulfillmentHistory, dose) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/health-data-standards/export/view_helper.rb', line 43

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}'"
  else
    return "value='#{fulfillmentHistory.quantity_dispensed['value']}' unit='#{fulfillmentHistory.quantity_dispensed['unit']}'"
  end
end

#identifier_for(obj) ⇒ Object



84
85
86
# File 'lib/health-data-standards/export/view_helper.rb', line 84

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

#is_bool?(str) ⇒ Boolean



80
81
82
# File 'lib/health-data-standards/export/view_helper.rb', line 80

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

#is_num?(str) ⇒ Boolean



72
73
74
75
76
77
78
# File 'lib/health-data-standards/export/view_helper.rb', line 72

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

#status_code_for(entry) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/health-data-standards/export/view_helper.rb', line 32

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



68
69
70
# File 'lib/health-data-standards/export/view_helper.rb', line 68

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

#value_or_null_flavor(time) ⇒ Object



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

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