Class: DaVinciPASTestKit::Generator::MustSupportMetadataExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile_elements, profile, resource, ig_resources) ⇒ MustSupportMetadataExtractor

Returns a new instance of MustSupportMetadataExtractor.



9
10
11
12
13
14
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 9

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

Instance Attribute Details

#ig_resourcesObject

Returns the value of attribute ig_resources.



7
8
9
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 7

def ig_resources
  @ig_resources
end

#profileObject

Returns the value of attribute profile.



7
8
9
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 7

def profile
  @profile
end

#profile_elementsObject

Returns the value of attribute profile_elements.



7
8
9
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 7

def profile_elements
  @profile_elements
end

#resourceObject

Returns the value of attribute resource.



7
8
9
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 7

def resource
  @resource
end

Instance Method Details

#all_must_support_elementsObject



26
27
28
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 26

def all_must_support_elements
  profile_elements.select(&:mustSupport)
end

#blood_pressure?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 300

def blood_pressure?
  ['observation-bp', 'USCoreBloodPressureProfile'].include?(profile.name)
end

#discriminators(slice) ⇒ Object



54
55
56
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 54

def discriminators(slice)
  slice.slicing.discriminator
end

#get_type_must_support_metadata(current_metadata, current_element) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 215

def (, current_element)
  current_element.type.map do |type|
    next unless type_must_support_extension?(type.extension)

     =
      {
        path: "#{[:path].delete_suffix('[x]')}#{type.code.upcase_first}",
        original_path: [:path]
      }
    [:type] = [type.code] if save_type_code?(type)
    handle_type_must_support_target_profiles(type, ) if type.code == 'Reference'

    
  end.compact
end

#handle_choice_type_in_sliced_element(current_metadata, must_support_elements_metadata) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 246

def handle_choice_type_in_sliced_element(, )
   = .find do ||
    [:original_path].present? &&
      [:path].include?([:original_path])
  end

  return unless .present?

  [:original_path] = [:path]
  [:path] =
    [:path].sub([:original_path], [:path])
end

#handle_fixed_values(metadata, element) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 190

def handle_fixed_values(, element)
  if element.fixedUri.present?
    [:fixed_value] = element.fixedUri
  elsif element.patternCodeableConcept.present?
    [:fixed_value] = element.patternCodeableConcept.coding.first.code
    [:path] += '.coding.code'
  elsif element.fixedCode.present?
    [:fixed_value] = element.fixedCode
  elsif element.patternIdentifier.present?
    [:fixed_value] = element.patternIdentifier.system
    [:path] += '.system'
  end
end

#handle_type_must_support_target_profiles(type, metadata) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 231

def handle_type_must_support_target_profiles(type, )
  index = 0
  target_profiles = []

  type.source_hash['_targetProfile']&.each do |hash|
    if hash.present?
      element = FHIR::Element.new(hash)
      target_profiles << type.targetProfile[index] if type_must_support_extension?(element.extension)
    end
    index += 1
  end

  [:target_profiles] = target_profiles if target_profiles.present?
end

#must_support_elementsObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 259

def must_support_elements
  plain_must_support_elements.each_with_object([]) do |current_element, |
    {
      path: current_element.path.gsub("#{resource}.", '')
    }.tap do ||
       = (, current_element)

      if .any?
        .concat()
      else
        handle_choice_type_in_sliced_element(, )

        supported_types = current_element.type.select { |type| save_type_code?(type) }.map(&:code)
        [:types] = supported_types if supported_types.present?

        if current_element.type.first&.code == 'Reference'
          handle_type_must_support_target_profiles(current_element.type.first,
                                                   )
        end

        handle_fixed_values(, current_element)

        .delete_if do ||
          [:path] == [:path] && [:fixed_value].blank?
        end

         << 
      end
    end
  end.uniq
end

#must_support_extension_elementsObject



30
31
32
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 30

def must_support_extension_elements
  all_must_support_elements.select { |element| element.path.end_with? 'extension' }
end

#must_support_extensionsObject



34
35
36
37
38
39
40
41
42
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 34

def must_support_extensions
  must_support_extension_elements.map do |element|
    {
      id: element.id,
      path: element.path.gsub("#{resource}.", ''),
      url: element.type.first.profile.first
    }
  end
end

#must_support_pattern_slice_elementsObject



58
59
60
61
62
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 58

def must_support_pattern_slice_elements
  must_support_slice_elements.select do |element|
    discriminators(sliced_element(element)).first.type == 'pattern' unless sliced_element(element).slicing.nil?
  end
end

#must_support_slice_elementsObject



44
45
46
47
48
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 44

def must_support_slice_elements
  all_must_support_elements.select do |element|
    !element.path.end_with?('extension') && element.sliceName.present?
  end
end

#must_support_slicesObject



182
183
184
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 182

def must_support_slices
  pattern_slices + type_slices + value_slices
end

#must_support_type_slice_elementsObject



121
122
123
124
125
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 121

def must_support_type_slice_elements
  must_support_slice_elements.select do |element|
    discriminators(sliced_element(element)).first.type == 'type' unless sliced_element(element).slicing.nil?
  end
end

#must_support_value_slice_elementsObject



152
153
154
155
156
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 152

def must_support_value_slice_elements
  must_support_slice_elements.select do |element|
    discriminators(sliced_element(element)).first.type == 'value' unless sliced_element(element).slicing.nil?
  end
end

#must_supportsObject



16
17
18
19
20
21
22
23
24
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 16

def must_supports
  @must_supports = {
    extensions: must_support_extensions,
    slices: must_support_slices,
    elements: must_support_elements
  }

  @must_supports
end

#pattern_slicesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 64

def pattern_slices
  must_support_pattern_slice_elements.map do |current_element|
    {
      name: current_element.id,
      path: current_element.path.gsub("#{resource}.", '')
    }.tap do ||
      discriminator = discriminators(sliced_element(current_element)).first
      discriminator_path = discriminator.path
      discriminator_path = '' if discriminator_path == '$this'
      pattern_element =
        if discriminator_path.present?
          profile_elements.find { |element| element.id == "#{current_element.id}.#{discriminator_path}" }
        else
          current_element
        end

      [:discriminator] =
        if pattern_element.patternCodeableConcept.present?
          {
            type: 'patternCodeableConcept',
            path: discriminator_path,
            code: pattern_element.patternCodeableConcept.coding.first.code,
            system: pattern_element.patternCodeableConcept.coding.first.system
          }
        elsif pattern_element.patternCoding.present?
          {
            type: 'patternCoding',
            path: discriminator_path,
            code: pattern_element.patternCoding.code,
            system: pattern_element.patternCoding.system
          }
        elsif pattern_element.patternIdentifier.present?
          {
            type: 'patternIdentifier',
            path: discriminator_path,
            system: pattern_element.patternIdentifier.system
          }
        elsif pattern_element.binding&.strength == 'required' &&
              pattern_element.binding&.valueSet.present?

          value_extractor = ValueExactor.new(ig_resources, resource)

          values = value_extractor.values_from_value_set_binding(pattern_element).presence ||
                   value_extractor.([[:path]]).presence || []

          {
            type: 'requiredBinding',
            path: discriminator_path,
            values:
          }
        else
          raise StandardError, 'Unsupported discriminator pattern type'
        end
    end
  end
end

#plain_must_support_elementsObject



186
187
188
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 186

def plain_must_support_elements
  all_must_support_elements - must_support_extension_elements - must_support_slice_elements
end

#remove_device_carrierObject

ONC and US Core 4.0.0 both clarified that health IT developers that always provide HL7 FHIR “observation” values are not required to demonstrate Health IT Module support for “dataAbsentReason” elements. Remove MS check for dataAbsentReason and component.dataAbsentReason from vital sign profiles and observation lab profile. Smoking status profile does not have MS on dataAbsentReason. It is safe to use profile.type == ‘Observation’



310
311
312
313
314
315
316
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 310

def remove_device_carrier
  return unless profile.type == 'Device'

  @must_supports[:elements].delete_if do |element|
    ['udiCarrier.carrierAIDC', 'udiCarrier.carrierHRF'].include?(element[:path])
  end
end

#remove_document_reference_attachment_data_urlObject



318
319
320
321
322
323
324
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 318

def remove_document_reference_attachment_data_url
  return unless profile.type == 'DocumentReference'

  @must_supports[:elements].delete_if do |element|
    ['content.attachment.data', 'content.attachment.url'].include?(element[:path])
  end
end

#save_type_code?(type) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 211

def save_type_code?(type)
  type.code == 'Reference'
end

#sliced_element(slice) ⇒ Object



50
51
52
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 50

def sliced_element(slice)
  profile_elements.find { |element| element.id == slice.path }
end

#type_must_support_extension?(extensions) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 204

def type_must_support_extension?(extensions)
  extensions&.any? do |extension|
    extension.url == 'http://hl7.org/fhir/StructureDefinition/elementdefinition-type-must-support' &&
      extension.valueBoolean
  end
end

#type_slicesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 127

def type_slices
  must_support_type_slice_elements.map do |current_element|
    discriminator = discriminators(sliced_element(current_element)).first
    type_path = discriminator.path
    type_path = '' if type_path == '$this'
    type_element =
      if type_path.present?
        profile_elements.find { |element| element.id == "#{current_element.id}.#{type_path}" }
      else
        current_element
      end

    type_code = type_element.type.first.code

    {
      name: current_element.id,
      path: current_element.path.gsub("#{resource}.", ''),
      discriminator: {
        type: 'type',
        code: type_code.upcase_first
      }
    }
  end
end

#value_slicesObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 158

def value_slices
  must_support_value_slice_elements.map do |current_element|
    {
      name: current_element.id,
      path: current_element.path.gsub("#{resource}.", ''),
      discriminator: {
        type: 'value'
      }
    }.tap do ||
      [:discriminator][:values] = discriminators(sliced_element(current_element)).map do |discriminator|
        fixed_element = profile_elements.find do |element|
          element.id.starts_with?(current_element.id) &&
            element.path == "#{current_element.path}.#{discriminator.path}"
        end

        {
          path: discriminator.path,
          value: fixed_element&.fixedUri || fixed_element&.fixedCode
        }
      end
    end
  end
end

#vital_sign?Boolean

SPECIAL CASE ####

Returns:

  • (Boolean)


293
294
295
296
297
298
# File 'lib/davinci_pas_test_kit/generator/must_support_metadata_extractor.rb', line 293

def vital_sign?
  [
    'http://hl7.org/fhir/StructureDefinition/vitalsigns',
    'http://hl7.org/fhir/us/core/StructureDefinition/us-core-vital-signs'
  ].include?(profile.baseDefinition)
end