Class: USCoreTestKit::Generator::ValueExactor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ig_resources, resource, profile_elements) ⇒ ValueExactor



6
7
8
9
10
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 6

def initialize(ig_resources, resource, profile_elements)
  self.ig_resources = ig_resources
  self.resource = resource
  self.profile_elements = profile_elements
end

Instance Attribute Details

#ig_resourcesObject

Returns the value of attribute ig_resources.



4
5
6
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 4

def ig_resources
  @ig_resources
end

#profile_elementsObject

Returns the value of attribute profile_elements.



4
5
6
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 4

def profile_elements
  @profile_elements
end

#resourceObject

Returns the value of attribute resource.



4
5
6
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 4

def resource
  @resource
end

Instance Method Details

#bound_systems(the_element) ⇒ Object



70
71
72
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 70

def bound_systems(the_element)
  bound_systems_from_valueset(value_set(the_element))
end

#bound_systems_from_valueset(value_set) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 74

def bound_systems_from_valueset(value_set)
  value_set&.compose&.include&.map do |include_element|
    if include_element.concept.present?
      include_element
    elsif include_element.system.present? && include_element.filter&.empty?
      # Cannot process intensional value set with filters
      ig_resources.code_system_by_url(include_element.system)
    elsif include_element.valueSet.present?
      include_element.valueSet.map do |vs|
        a_value_set = ig_resources.value_set_by_url(vs)
        bound_systems_from_valueset(a_value_set)
      end
    end
  end&.flatten&.compact
end

#codes_from_system_code_pair(codings) ⇒ Object



94
95
96
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 94

def codes_from_system_code_pair(codings)
  codings.present? ? codings.map { |coding| coding[:code] }.compact.uniq : []
end

#codes_from_value_set_binding(the_element) ⇒ Object



90
91
92
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 90

def codes_from_value_set_binding(the_element)
  codes_from_system_code_pair(codings_from_value_set_binding(the_element))
end

#codings_from_bound_systems(bound_systems) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 112

def codings_from_bound_systems(bound_systems)
  return [] unless bound_systems.present?

  bound_systems.flat_map do |bound_system|
    case bound_system
    when FHIR::ValueSet::Compose::Include
      bound_system.concept.map { |concept| { system: bound_system.system, code: concept.code } }
    when FHIR::CodeSystem
      bound_system.concept.map { |concept| { system: bound_system.url, code: concept.code } }
    else
      []
    end
  end.uniq
end

#codings_from_value_set_binding(the_element) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 98

def codings_from_value_set_binding(the_element)
  return [] if the_element.nil?

  bound_systems = bound_systems(the_element)

  return codings_from_bound_systems(bound_systems) if bound_systems.present?

  expansion_contains = value_set_expansion_contains(the_element)

  return [] if expansion_contains.blank?

  expansion_contains.map { |contains| { system: contains.system, code: contains.code } }.compact.uniq
end

#fhir_metadata(current_path) ⇒ Object



131
132
133
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 131

def (current_path)
  FHIR.const_get(resource)::[current_path]
end

#value_set(the_element) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 46

def value_set(the_element)
  binding = value_set_binding(the_element)
  target_valueset = binding&.valueSet

  additional_binding_ext = binding&.extension&.find do |ext|
    ext.url == 'http://hl7.org/fhir/tools/StructureDefinition/additional-binding' &&
    ext.extension.any? { |sub_ext| sub_ext.url == 'purpose' && sub_ext.valueCode == 'minimum' }
  end

  min_valueset_ext = binding&.extension&.find do |ext|
    ext.url == 'http://hl7.org/fhir/StructureDefinition/elementdefinition-minValueSet'
  end

  if additional_binding_ext.present?
    min_valueset = additional_binding_ext.extension.find { |ext| ext.url == 'valueSet' }

    target_valueset = min_valueset.valueCanonical if min_valueset.present?
  elsif min_valueset_ext.present?
    target_valueset = min_valueset_ext.valueCanonical
  end

  ig_resources.value_set_by_url(target_valueset)
end

#value_set_binding(the_element) ⇒ Object



42
43
44
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 42

def value_set_binding(the_element)
  the_element&.binding
end

#value_set_expansion_contains(element) ⇒ Object



127
128
129
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 127

def value_set_expansion_contains(element)
  value_set(element)&.expansion&.contains
end

#values_from_fixed_codes(profile_element, type) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 12

def values_from_fixed_codes(profile_element, type)
  return [] unless type == 'CodeableConcept'

  elements = profile_elements.select do |element|
    element.path == "#{profile_element.path}.coding.code" && element.fixedCode.present?
  end

  elements.map(&:fixedCode)
end

#values_from_pattern_codeable_concept(profile_element, type) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 32

def values_from_pattern_codeable_concept(profile_element, type)
  return [] unless type == 'CodeableConcept'

  elements = profile_elements.select do |element|
    element.path == profile_element.path && element.patternCodeableConcept.present? && element.min.positive?
  end

  elements.map { |element| element.patternCodeableConcept.coding.first.code }
end

#values_from_pattern_coding(profile_element, type) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 22

def values_from_pattern_coding(profile_element, type)
  return [] unless type == 'CodeableConcept'

  elements = profile_elements.select do |element|
    element.path == "#{profile_element.path}.coding" && element.patternCoding.present?
  end

  elements.map { |element| element.patternCoding.code }
end

#values_from_resource_metadata(paths) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/us_core_test_kit/generator/value_extractor.rb', line 135

def (paths)
  values = []

  paths.each do |current_path|
     = (current_path)

    next unless &.dig('valid_codes').present?

    values += ['valid_codes'].flat_map do |system, codes|
      codes.map { |code| { system:, code: } }
    end
  end

  values
end