Module: CancerPathologyDataSharingTestKit::FHIRResourceNavigation

Included in:
MustSupportTest
Defined in:
lib/cancer_pathology_data_sharing_test_kit/fhir_resource_navigation.rb

Constant Summary collapse

DAR_EXTENSION_URL =
'http://hl7.org/fhir/StructureDefinition/data-absent-reason'.freeze
PRIMITIVE_DATA_TYPES =
FHIR::PRIMITIVES.keys

Instance Method Summary collapse

Instance Method Details

#find_a_value_at(element, path, include_dar: false, &block) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cancer_pathology_data_sharing_test_kit/fhir_resource_navigation.rb', line 22

def find_a_value_at(element, path, include_dar: false, &block) # rubocop:disable Metrics/CyclomaticComplexity
  return nil if element.nil?

  elements = Array.wrap(element)
  if path.empty?
    unless include_dar
      elements = elements.reject do |el|
        el.respond_to?(:extension) && el.extension.any? { |ext| ext.url == DAR_EXTENSION_URL }
      end
    end

    return elements.find(&block) if block_given?

    return elements.first
  end

  path_segments = path.split(/(?<!hl7)\./)

  segment = path_segments
    .shift
    .delete_suffix('[x]')
    .gsub(/^class$/, 'local_class')
    .gsub(/^method$/, 'local_method')
    .gsub('[x]:', ':')
    .to_sym
  no_elements_present =
    elements.none? do |element| # rubocop:disable Lint/ShadowingOuterLocalVariable
      child = get_next_value(element, segment)
      child.present? || child == false
    end
  return nil if no_elements_present

  remaining_path = path_segments.join('.')
  elements.each do |element| # rubocop:disable Lint/ShadowingOuterLocalVariable
    child = get_next_value(element, segment)
    element_found =
      if block_given?
        find_a_value_at(child, remaining_path, include_dar: include_dar, &block)
      else
        find_a_value_at(child, remaining_path, include_dar: include_dar)
      end
    return element_found if element_found.present? || element_found == false
  end

  nil
end

#find_slice_via_discriminator(element, property) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



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
141
142
143
144
145
146
# File 'lib/cancer_pathology_data_sharing_test_kit/fhir_resource_navigation.rb', line 95

def find_slice_via_discriminator(element, property) # rubocop:disable Metrics/CyclomaticComplexity
  element_name = property.to_s.split(':')[0].gsub(/^class$/, 'local_class').gsub(/^method$/, 'local_method')
  slice_name = property.to_s.split(':')[1].gsub(/^class$/, 'local_class').gsub(/^method$/, 'local_method')
  if .present?
    slice_by_name = .must_supports[:slices].find { |slice| slice[:slice_name] == slice_name }
    discriminator = slice_by_name[:discriminator]
    slices = Array.wrap(element.send(element_name))
    slices.find do |slice|
      case discriminator[:type]
      when 'patternCodeableConcept'
        slice_value = discriminator[:path].present? ? slice.send(discriminator[:path].to_s)&.coding : slice.coding
        slice_value&.any? { |coding| coding.code == discriminator[:code] && coding.system == discriminator[:system] }
      when 'patternCoding'
        slice_value = discriminator[:path].present? ? slice.send(discriminator[:path]) : slice
        slice_value&.code == discriminator[:code] && slice_value&.system == discriminator[:system]
      when 'patternIdentifier'
        slice.identifier.system == discriminator[:system]
      when 'value'
        values = discriminator[:values].map { |value| value.merge(path: value[:path].split('.')) }
        verify_slice_by_values(slice, values)
      when 'type'
        case discriminator[:code]
        when 'Date'
          begin
            Date.parse(slice)
          rescue ArgumentError # rubocop:disable Metrics/BlockNesting
            false
          end
        when 'DateTime'
          begin
            DateTime.parse(slice)
          rescue ArgumentError # rubocop:disable Metrics/BlockNesting
            false
          end
        when 'String'
          slice.is_a? String
        else
          if slice.is_a? FHIR::Bundle::Entry # rubocop:disable Metrics/BlockNesting
            slice.resource.resourceType == discriminator[:code]
          else
            slice.is_a? FHIR.const_get(discriminator[:code])
          end
        end
      when 'requiredBinding'
        discriminator[:path].present? ? slice.send(discriminator[:path].to_s).coding : slice.coding
        slice_value { |coding| discriminator[:values].include?(coding.code) }
      end
    end
  else # rubocop:disable Style/EmptyElse
    # TODO: Error handling for if this file doesn't have access to metadata for some reason (begin/rescue with StandardError?)
  end
end

#get_next_value(element, property) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cancer_pathology_data_sharing_test_kit/fhir_resource_navigation.rb', line 69

def get_next_value(element, property)
  extension_url = property[/(?<=where\(url=').*(?='\))/]
  if extension_url.present?
    element.url == extension_url ? element : nil
  elsif property.to_s.include?(':') && !property.to_s.include?('url')
    find_slice_via_discriminator(element, property)

  else
    value = element.send(property)
    primitive_value = get_primitive_type_value(element, property, value)
    primitive_value.present? ? primitive_value : value
  end
rescue NoMethodError
  nil
end

#get_primitive_type_value(element, property, value) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/cancer_pathology_data_sharing_test_kit/fhir_resource_navigation.rb', line 85

def get_primitive_type_value(element, property, value)
  source_value = element.source_hash["_#{property}"]

  return nil unless source_value.present?

  primitive_value = CancerPathologyDataSharingTestKit::PrimitiveType.new(source_value)
  primitive_value.value = value
  primitive_value
end

#resolve_path(elements, path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cancer_pathology_data_sharing_test_kit/fhir_resource_navigation.rb', line 8

def resolve_path(elements, path)
  elements = Array.wrap(elements)
  return elements if path.blank?

  paths = path.split(/(?<!hl7)\./)
  segment = paths.first
  remaining_path = paths.drop(1).join('.')

  elements.flat_map do |element|
    child = get_next_value(element, segment)
    resolve_path(child, remaining_path)
  end.compact
end

#verify_slice_by_values(element, value_definitions) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cancer_pathology_data_sharing_test_kit/fhir_resource_navigation.rb', line 148

def verify_slice_by_values(element, value_definitions) # rubocop:disable Metrics/CyclomaticComplexity
  path_prefixes = value_definitions.map { |value_definition| value_definition[:path].first }.uniq
  path_prefixes.all? do |path_prefix|
    value_definitions_for_path =
      value_definitions
        .select { |value_definition| value_definition[:path].first == path_prefix }
        .each { |value_definition| value_definition[:path].shift }
    find_a_value_at(element, path_prefix) do |el_found|
      child_element_value_definitions, current_element_value_definitions =
        value_definitions_for_path.partition { |value_definition| value_definition[:path].present? }

      current_element_values_match =
        current_element_value_definitions
          .all? { |value_definition| value_definition[:value] == el_found }

      child_element_values_match =
        if child_element_value_definitions.present?
          verify_slice_by_values(el_found, child_element_value_definitions)
        else
          true
        end
      current_element_values_match && child_element_values_match
    end
  end
end