Sha256: 5d7c529368302eac48d674dc5ebbea19ab26de71d6be7c77ff2cf7a98f7f27de

Contents?: true

Size: 1.88 KB

Versions: 13

Compression:

Stored size: 1.88 KB

Contents

module USCoreTestKit
  module FHIRResourceNavigation
    DAR_EXTENSION_URL = 'http://hl7.org/fhir/StructureDefinition/data-absent-reason'.freeze

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

      paths = path.split('.')
      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

    def find_a_value_at(element, path, include_dar: false)
      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 { |el| yield(el) } if block_given?

        return elements.first
      end

      path_segments = path.split('.')
      segment = path_segments.shift.delete_suffix('[x]').to_sym

      no_elements_present =
        elements.none? do |element|
        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|
        child = get_next_value(element, segment)
        element_found =
          if block_given?
            find_a_value_at(child, remaining_path, include_dar: include_dar) { |value_found| yield(value_found) }
          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

    def get_next_value(element, property)
      element.send(property)
    rescue NoMethodError
      nil
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
us_core_test_kit-0.4.6 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.4.5 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.4.4 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.4.3 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.4.2 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.4.1 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.4.0 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.3.2 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.3.1 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.3.0 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.2.5 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.2.4 lib/us_core_test_kit/fhir_resource_navigation.rb
us_core_test_kit-0.2.3 lib/us_core_test_kit/fhir_resource_navigation.rb