Class: FHIR::Generator::IGResources

Inherits:
Object
  • Object
show all
Defined in:
lib/fhir_models/generator/ig_resources.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ig_metadataObject

Returns the value of attribute ig_metadata.



4
5
6
# File 'lib/fhir_models/generator/ig_resources.rb', line 4

def 
  @ig_metadata
end

Instance Method Details

#add(resource, break_bundle: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fhir_models/generator/ig_resources.rb', line 6

def add(resource, break_bundle: false)
  return if resource.nil?

  if break_bundle && resource['resourceType'] == 'Bundle'
    resource['entry']&.each do |entry|
      entry_resource = entry['resource']
      next if entry_resource.nil?

      if entry_resource['resourceType'] == 'ValueSet'
        merge_value_set(entry_resource)
      else
        resources_by_type[resource['resourceType']] << entry_resource
      end
    end
  else
    resources_by_type[resource['resourceType']] << resource
  end
end

#all_codes_from_concept(concept) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/fhir_models/generator/ig_resources.rb', line 151

def all_codes_from_concept(concept)
  collected_codes = []
  concept&.each do |c|
    collected_codes << c['code']
    collected_codes.concat(all_codes_from_concept(c['concept'])) unless c['concept'].nil?
  end
  collected_codes
end

#codes_from_compose(backbone_element) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fhir_models/generator/ig_resources.rb', line 132

def codes_from_compose(backbone_element)
  collected_codes = []
  system_url = backbone_element['system']

  if !system_url.nil?
    if !backbone_element['concept'].nil?
      backbone_element['concept'].each { |coding| collected_codes << coding['code'] }
    elsif backbone_element['filter'].nil?
      # i.e. the include/exclude element has 'system', 'copyright' and/or 'version'
      cs = get_code_systems(system_url)
      collected_codes.concat(all_codes_from_concept(cs['concept'])) unless cs.nil?
    end
  elsif !backbone_element['valueSet'].nil?
    # Handle include/exclude value set
  end

  collected_codes
end

#complex_typesObject



29
30
31
# File 'lib/fhir_models/generator/ig_resources.rb', line 29

def complex_types
  resources_by_type['StructureDefinition'].select { |sd| sd['kind'] == 'complex-type' && sd['derivation'] != 'constraint' }
end

#extension_definitionsObject



37
38
39
# File 'lib/fhir_models/generator/ig_resources.rb', line 37

def extension_definitions
  resources_by_type['StructureDefinition'].select { |sd| sd['type'] == 'Extension' && sd['derivation'] == 'constraint' }
end

#get_code_systems(url = nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/fhir_models/generator/ig_resources.rb', line 45

def get_code_systems(url = nil)
  if url.nil?
    resources_by_type['CodeSystem']
  else
    resources_by_type['CodeSystem'].find { |cs| cs['url'] == url }
  end
end

#get_codes(url) ⇒ Object



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
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fhir_models/generator/ig_resources.rb', line 90

def get_codes(url)
  return nil if url.nil?
  return transformed_expansion[url] if transformed_expansion[url]

  value_set = get_value_sets(url)
  unless value_set.nil?
    transformed_expansion[url] = {}
    # if the expansion is completed already, use it...
    # except for http://hl7.org/fhir/ValueSet/c80-doc-typecodes, because that expansion is missing codes
    if value_set.dig('expansion', 'contains') && url != 'http://hl7.org/fhir/ValueSet/c80-doc-typecodes'
      transformed_expansion[url] = value_set['expansion']['contains']
                                   .group_by { |coding| coding['system'] }
                                   .transform_values { |v| v.map { |coding| coding['code'] } }
    elsif !value_set['compose'].nil?
      # the expansion is not available, so we have to include values
      # and possibly partially expand the Valueset by including extra CodeSystems
      # So, for each system, if codes are included add them...
      value_set['compose']['include']&.each do |include_element|
        system_url = include_element['system']
        next if system_url.nil?

        included_codes = codes_from_compose(include_element)
        transformed_expansion[url][system_url] = included_codes unless included_codes.empty?
      end

      value_set['compose']['exclude']&.each do |exclude_element|
        system_url = exclude_element['system']
        next if system_url.nil?

        excluded_codes = codes_from_compose(exclude_element)

        if transformed_expansion[url].key?(system_url)
          transformed_expansion[url][system_url] = transformed_expansion[url][system_url] - excluded_codes
          transformed_expansion[url].delete(system_url) if transformed_expansion[url][system_url].empty?
        end
      end
    end
    transformed_expansion[url].each { |_system, codes| codes.uniq! }
  end
  transformed_expansion[url]
end

#get_search_parameters(type_name = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/fhir_models/generator/ig_resources.rb', line 61

def get_search_parameters(type_name = nil)
  if type_name.nil?
    resources_by_type['SearchParameter']
  else
    resources_by_type['SearchParameter'].select do |p|
      p['base']&.include?(type_name) && p['expression'] && !p['expression'].include?('extension')
    end
  end
end

#get_value_sets(url = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/fhir_models/generator/ig_resources.rb', line 53

def get_value_sets(url = nil)
  if url.nil?
    resources_by_type['ValueSet']
  else
    resources_by_type['ValueSet'].find { |vs| vs['url'] == url }
  end
end

#merge_value_set(from_value_set) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fhir_models/generator/ig_resources.rb', line 75

def merge_value_set(from_value_set)
  url = from_value_set['url']
  to_value_set = get_value_sets(url)

  if to_value_set.nil?
    resources_by_type['ValueSet'] << from_value_set
  elsif to_value_set['expansion'].nil? && !from_value_set['expansion'].nil?
    to_value_set['expansion'] = from_value_set['expansion']
  elsif to_value_set['compose'].nil? && !from_value_set['compose'].nil?
    to_value_set['compose'] = from_value_set['compose']
  else
    puts "Cannot merge ValueSet #{url}"
  end
end

#primitive_typesObject



25
26
27
# File 'lib/fhir_models/generator/ig_resources.rb', line 25

def primitive_types
  resources_by_type['StructureDefinition'].select { |sd| sd['kind'] == 'primitive-type' }
end

#profilesObject



41
42
43
# File 'lib/fhir_models/generator/ig_resources.rb', line 41

def profiles
  resources_by_type['StructureDefinition'].select { |sd| sd['kind'] == 'resource' && sd['derivation'] == 'constraint' }
end

#resource_definitionsObject



33
34
35
# File 'lib/fhir_models/generator/ig_resources.rb', line 33

def resource_definitions
  resources_by_type['StructureDefinition'].select { |sd| sd['kind'] == 'resource' && sd['derivation'] != 'constraint' }
end

#resources_by_typeObject

private



161
162
163
# File 'lib/fhir_models/generator/ig_resources.rb', line 161

def resources_by_type
  @resources_by_type ||= Hash.new { |hash, key| hash[key] = [] }
end

#transformed_expansionObject



71
72
73
# File 'lib/fhir_models/generator/ig_resources.rb', line 71

def transformed_expansion
  @transformed_expansion ||= {}
end