Class: USCoreTestKit::Generator::SearchDefinitionMetadataExtractor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ig_resources, profile_elements, group_metadata) ⇒ SearchDefinitionMetadataExtractor

Returns a new instance of SearchDefinitionMetadataExtractor.



8
9
10
11
12
13
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 8

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

Instance Attribute Details

#group_metadataObject

Returns the value of attribute group_metadata.



6
7
8
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 6

def 
  @group_metadata
end

#ig_resourcesObject

Returns the value of attribute ig_resources.



6
7
8
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 6

def ig_resources
  @ig_resources
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 6

def name
  @name
end

#profile_elementsObject

Returns the value of attribute profile_elements.



6
7
8
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 6

def profile_elements
  @profile_elements
end

Instance Method Details

#chainObject



158
159
160
161
162
163
164
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 158

def chain
  return nil if param.chain.blank?

  param.chain
    .zip(chain_expectations)
    .map { |chain, expectation| { chain: chain, expectation: expectation } }
end

#chain_expectationsObject



154
155
156
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 154

def chain_expectations
  chain_extensions.map { |extension| support_expectation(extension) }
end

#chain_extensionsObject



150
151
152
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 150

def chain_extensions
  param_hash['_chain']
end

#comparator_expectation(extension) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 108

def comparator_expectation(extension)
  if extension.nil?
    'MAY'
  else
    support_expectation(extension)
  end
end

#comparator_expectation_extensionsObject



100
101
102
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 100

def comparator_expectation_extensions
  @comparator_expectation_extensions ||= param_hash['_comparator'] || []
end

#comparatorsObject



116
117
118
119
120
121
122
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 116

def comparators
  {}.tap do |comparators|
    param.comparator&.each_with_index do |comparator, index|
      comparators[comparator.to_sym] = comparator_expectation(comparator_expectation_extensions[index])
    end
  end
end

#contains_multiple?Boolean

Returns:

  • (Boolean)


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

def contains_multiple?
  if profile_element.present?
    if profile_element.id.start_with?('Extension') && extension_definition.present?
      # Find the extension instance in a US Core profile
      target_element = profile_elements.find do |element|
        element.type.any? { |type| type.code == "Extension" && type.profile.include?(extension_definition.url) }
      end
      target_element&.max == '*'
    else
      profile_element.max == '*'
    end
  else
    false
  end
end

#extension_definitionObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 88

def extension_definition
   @extension_definition ||=
      begin
        ext_definition = nil
        extensions&.each do ||
          ext_definition = ig_resources.profile_by_url([:url])
          break if ext_definition.present?
        end
        ext_definition
      end
end

#extensionsObject



74
75
76
77
78
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 74

def extensions
  @extensions ||= full_paths.select { |a_path| a_path.include?('extension.where') }
                            .map { |a_path| { url: a_path[/(?<=extension.where\(url=').*(?='\))/] }}
                            .presence
end

#full_pathsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 41

def full_paths
  @full_paths ||=
    begin
      path = param.expression.gsub(/.where\(resolve\((.*)/, '').gsub(/url = '/, 'url=\'')
      path = path[1..-2] if path.start_with?('(') && path.end_with?(')')
      path.scan(/[. ]as[( ]([^)]*)[)]?/).flatten.map do |as_type|
        path.gsub!(/[. ]as[( ](#{as_type}[^)]*)[)]?/, as_type.upcase_first) if as_type.present?
      end

      path.gsub!('Resource.', "#{resource}.") if path.start_with?('Resource.')

      full_paths = path.split('|')

      # There is a bug in US Core 5 asserted-date search parameter. See FHIR-40573
      if param.respond_to?(:version) && param.version == '5.0.1' && name == 'asserted-date'
        remove_additional_extension_from_asserted_date(full_paths)
      end

      full_paths
    end
end

#multiple_or_expectationObject



166
167
168
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 166

def multiple_or_expectation
  param_hash['_multipleOr'] ? param_hash['_multipleOr']['extension'].first['valueCode'] : 'MAY'
end

#paramObject



33
34
35
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 33

def param
  @param ||= ig_resources.search_param_by_resource_and_name(resource, name)
end

#param_hashObject



37
38
39
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 37

def param_hash
  param.source_hash
end

#pathsObject



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

def paths
  @paths ||= full_paths.map { |a_path| a_path.gsub("#{resource}.", '') }
end

#profile_elementObject



80
81
82
83
84
85
86
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 80

def profile_element
  @profile_element ||=
    (
      profile_elements.find { |element| full_paths.include?(element.id) } ||
      extension_definition&.differential&.element&.find { |element| element.id == 'Extension.value[x]'}
    )
end

#remove_additional_extension_from_asserted_date(full_paths) ⇒ Object



63
64
65
66
67
68
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 63

def remove_additional_extension_from_asserted_date(full_paths)
  full_paths.each do |full_path|
    next unless full_path.include?('http://hl7.org/fhir/StructureDefinition/condition-assertedDate')
    full_path.gsub!(/\).extension./, ').')
  end
end

#resourceObject



29
30
31
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 29

def resource
  [:resource]
end

#search_definitionObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 15

def search_definition
  @search_definition ||=
    {
      paths: paths,
      full_paths: full_paths,
      comparators: comparators,
      values: values,
      type: type,
      contains_multiple: contains_multiple?,
      multiple_or: multiple_or_expectation,
      chain: chain
    }.compact
end

#support_expectation(extension) ⇒ Object



104
105
106
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 104

def support_expectation(extension)
  extension['extension'].first['valueCode']
end

#typeObject



124
125
126
127
128
129
130
131
132
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 124

def type
  if profile_element.present?
    profile_element.type.first.code
  else
    # search is a variable type, eg. Condition.onsetDateTime - element
    # in profile def is Condition.onset[x]
    param.type
  end
end

#value_extractorObject



224
225
226
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 224

def value_extractor
  @value_extractor ||= ValueExactor.new(ig_resources, resource, profile_elements)
end

#valuesObject



170
171
172
173
174
175
176
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 170

def values
  values_from_must_supports(profile_element).presence ||
    value_extractor.values_from_fixed_codes(profile_element, type).presence ||
    value_extractor.codes_from_value_set_binding(profile_element).presence ||
    (paths).presence ||
    []
end

#values_from_must_support_elements(short_path) ⇒ Object



210
211
212
213
214
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 210

def values_from_must_support_elements(short_path)
  [:must_supports][:elements]
    .select { |element| element[:path] == "#{short_path}.coding.code" }
    .map { |element| element[:fixed_value] }
end

#values_from_must_support_slices(profile_element, short_path, mandatory_slice_only) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 189

def values_from_must_support_slices(profile_element, short_path, mandatory_slice_only)
  [:must_supports][:slices]
    .select { |slice| [short_path, "#{short_path}.coding"].include?(slice[:path]) }
    .map do |slice|
      slice_element = profile_elements.find { |element| slice[:slice_id] == element.id }
      next if (profile_element.min > 0 && slice_element.min == 0 && mandatory_slice_only)

      case slice[:discriminator][:type]
      when 'patternCoding', 'patternCodeableConcept'
        slice[:discriminator][:code]
      when 'requiredBinding'
        value_extractor.codes_from_system_code_pair(slice[:discriminator][:values])
      when 'value'
        slice[:discriminator][:values]
          .select { |value| value[:path] == 'coding.code' }
          .map { |value| value[:value] }
      end
    end
    .compact.flatten
end

#values_from_must_supports(profile_element) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 178

def values_from_must_supports(profile_element)
  return if profile_element.nil?

  short_path = profile_element.path.split('.', 2)[1]

  values_from_must_support_slices(profile_element, short_path, true).presence ||
  values_from_must_support_slices(profile_element, short_path, false).presence ||
  values_from_must_support_elements(short_path).presence ||
  []
end

#values_from_resource_metadata(paths) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/us_core_test_kit/generator/search_definition_metadata_extractor.rb', line 216

def (paths)
  if multiple_or_expectation == 'SHALL' || paths.any? { |path| path.downcase.include?('status') }
    value_extractor.codes_from_system_code_pair(value_extractor.(paths))
  else
    []
  end
end