Sha256: 2e6f3537ba2b32e971621c307ee26e2776b88bfb61556b31bc673d1fb7443317

Contents?: true

Size: 1.92 KB

Versions: 11

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'json'

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

    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, &block)
      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('.')
      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:, &block)
                        else
                          find_a_value_at(child, remaining_path, 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

11 entries across 11 versions & 1 rubygems

Version Path
carin_for_blue_button_test_kit-0.14.0 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.13.3 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.13.2 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.13.1 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.13.0 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.12.1 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.12.0 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.11.2 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.11.1 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.11.0 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb
carin_for_blue_button_test_kit-0.10.0 lib/carin_for_blue_button_test_kit/fhir_resource_navigation.rb