Module: ThingWithCodes

Included in:
CodedResultValue, Entry, Transfer
Defined in:
lib/health-data-standards/models/thing_with_codes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_codes_to_s(codes) ⇒ Object



14
15
16
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 14

def self.convert_codes_to_s(codes)
  codes.map {|code_set, codes| "#{code_set}: #{codes.join(', ')}"}.join(' ')
end

.included(receiver) ⇒ Object



2
3
4
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 2

def self.included(receiver)
  receiver.field :codes, type: Hash, default: {}
end

Instance Method Details

#add_code(code, code_system) ⇒ Object

Add a code into the Entry

Parameters:

  • code (String)

    the code to add

  • code_system (String)

    the code system that the code belongs to



80
81
82
83
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 80

def add_code(code, code_system)
  self.codes[code_system] ||= []
  self.codes[code_system] << code
end

#codes_in_code_set(code_set) ⇒ all codes that are in the code set

Returns all codes that are in the code set.

Parameters:

  • code_set (Array)

    an Array of Hashes that describe the values for code sets The hash has a key of “set” for the code system name and “values” for the actual code list

Returns:

  • (all codes that are in the code set)

    all codes that are in the code set



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 63

def codes_in_code_set(code_set)
  matching = {}
  codes.keys.each do |code_system|
    matching_codes = []
    matching[code_system] = matching_codes
    all_codes_in_system = code_set.find_all {|set| set['set'] == code_system}
    all_codes_in_system.each do |codes_in_system|
      matching_codes.concat codes_in_system['values'] & codes[code_system]
    end
  end
  matching
end

#codes_to_sObject



10
11
12
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 10

def codes_to_s
  ThingWithCodes.convert_codes_to_s(codes)
end

#preferred_code(preferred_code_sets, codes_attribute = :codes, value_set_map = nil) ⇒ Object

Will return a single code and code set if one exists in the code sets that are passed in. Returns a hash with a key of code and code_set if found, nil otherwise



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 20

def preferred_code(preferred_code_sets, codes_attribute=:codes, value_set_map=nil)
  codes_value = send(codes_attribute)
  preferred_code_sets = value_set_map ? (preferred_code_sets & value_set_map.collect{|cs| cs["set"]}) : preferred_code_sets
  matching_code_sets = preferred_code_sets & codes_value.keys 
  if matching_code_sets.present?
    if value_set_map
      matching_code_sets.each do |matching_code_set|
        matching_codes = codes_value[matching_code_set] & value_set_map.collect{|cs| cs["set"] == matching_code_set ? cs["values"] : []}.flatten.compact
        if matching_codes.present?
          return {'code' => matching_codes.first, 'code_set' => matching_code_set}
        end
      end
      # we did not find a matching preferred code... we cannot write this out to QRDA
      return nil
    else
      code_set = matching_code_sets.first
      {'code' => codes_value[code_set].first, 'code_set' => code_set}
    end
  else
    nil
  end
end

#single_code_value?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 6

def single_code_value?
  codes.size == 1 && codes.first[1].size == 1
end

#translation_codes(preferred_code_sets, value_set_map = nil) ⇒ Object

Will return an Array of code and code_set hashes for all codes for this entry except for the preferred_code. It is intended that these codes would be used in the translation elements as childern of a CDA code element



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/health-data-standards/models/thing_with_codes.rb', line 46

def translation_codes(preferred_code_sets,value_set_map=nil)
  tx_codes = []
  matching_codes = value_set_map ? codes_in_code_set(value_set_map) : codes
  matching_codes.each_pair do |code_set, code_list|
    code_list.each do |code|
      tx_codes << {'code' => code, 'code_set' => code_set}
    end
  end

  tx_codes - [preferred_code(preferred_code_sets, :codes, value_set_map)]
end