Class: DaVinciUSDrugFormularyTestKit::Generator::SearchDefinitionMetadataExtractor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SearchDefinitionMetadataExtractor.



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

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

Instance Attribute Details

#ig_resourcesObject

Returns the value of attribute ig_resources.



6
7
8
# File 'lib/davinci_us_drug_formulary_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/davinci_us_drug_formulary_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/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 6

def profile_elements
  @profile_elements
end

#resourceObject

Returns the value of attribute resource.



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

def resource
  @resource
end

Instance Method Details

#basic_status_search?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 156

def basic_status_search?
  resource == 'Basic' && name == 'status'
end

#basic_status_valuesObject



160
161
162
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 160

def basic_status_values
  ['draft', 'active', 'retired', 'unknown']
end

#chainObject



144
145
146
147
148
149
150
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 144

def chain
  return nil if param.chain.blank?

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

#chain_expectationsObject



140
141
142
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 140

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

#chain_extensionsObject



136
137
138
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 136

def chain_extensions
  param_hash['_chain']
end

#comparator_expectation(extension) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 87

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

#comparator_expectation_extensionsObject



79
80
81
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 79

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

#comparatorsObject



95
96
97
98
99
100
101
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 95

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)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 120

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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 67

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



55
56
57
58
59
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 55

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



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 38

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.split('|')
    end
end

#multiple_or_expectationObject



152
153
154
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 152

def multiple_or_expectation
  param_hash['multipleOr']
end

#paramObject



30
31
32
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 30

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

#param_hashObject



34
35
36
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 34

def param_hash
  param.source_hash
end

#pathsObject



51
52
53
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 51

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

#profile_elementObject



61
62
63
64
65
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 61

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

#search_definitionObject



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

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

#support_expectation(extension) ⇒ Object



83
84
85
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 83

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

#targetObject



113
114
115
116
117
118
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 113

def target
  # The search param definition for FormularyItem.subject doesn't have a target for some reason
  return 'MedicationKnowledge' if param.url == 'http://hl7.org/fhir/us/davinci-drug-formulary/SearchParameter/Basic-subject'

  param.target.first
end

#typeObject



103
104
105
106
107
108
109
110
111
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 103

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



182
183
184
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 182

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

#valuesObject



164
165
166
167
168
169
170
171
172
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 164

def values
  return basic_status_values if basic_status_search?

  value_extractor.values_from_slicing(profile_element, type).presence ||
    value_extractor.values_from_required_binding(profile_element).presence ||
    value_extractor.values_from_value_set_binding(profile_element).presence ||
    (paths).presence ||
    []
end

#values_from_resource_metadata(paths) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/davinci_us_drug_formulary_test_kit/generator/search_definition_metadata_extractor.rb', line 174

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