Class: FHIR::STU3::Definitions

Inherits:
Object
  • Object
show all
Extended by:
Deprecate
Defined in:
lib/fhir_stu3_models/bootstrap/definitions.rb

Constant Summary collapse

@@defns =
File.expand_path '../definitions', File.dirname(File.absolute_path(__FILE__))
@@types =
nil
@@resources =
nil
@@profiles =
nil
@@extensions =
nil
@@expansions =
nil
@@valuesets =
nil
@@search_params =
nil
@@cache =
{}

Class Method Summary collapse

Methods included from Deprecate

deprecate

Class Method Details

.basetype(uri) ⇒ Object

Get the basetype (String) for a given profile or extension.



114
115
116
117
118
119
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 114

def self.basetype(uri)
  return nil if uri.nil?
  defn = profiles.detect { |x| x['url'] == uri } || extensions.detect { |x| x['url'] == uri }
  return nil if defn.nil?
  defn['baseType']
end

.complex_typesObject



38
39
40
41
42
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 38

def self.complex_types
  # complex data types start with an uppercase letter
  # and we'll filter out profiles on types (for example, Age is a profile on Quantity)
  @complex_types ||= types.select { |t| t['id'].start_with?(*('A'..'Z').to_a) && (t['id'] == t['snapshot']['element'].first['path']) }
end

.expansionsObject


ValueSet Code Expansions



178
179
180
181
182
183
184
185
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 178

def self.expansions
  @@expansions ||= begin
    # load the expansions
    filename = File.join(@@defns, 'valuesets', 'expansions.json')
    raw = File.open(filename, 'r:UTF-8', &:read)
    JSON.parse(raw)['entry'].map { |e| e['resource'] }
  end
end

.extension_definition(extension_name) ⇒ Object



105
106
107
108
109
110
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 105

def self.extension_definition(extension_name)
  return nil if extension_name.nil?
  extension = extensions.find { |x| x['xmlId'] == extension_name || x['name'] == extension_name || x['url'] == extension_name }
  return nil if extension.nil?
  FHIR::STU3::StructureDefinition.new(extension)
end

.get_codes(uri) ⇒ Object

Get codes (Array of Strings) for a given expansion.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 198

def self.get_codes(uri)
  return nil if uri.nil?
  return @@cache[uri] if @@cache[uri]
  valueset = expansions.find { |x| x['url'] == uri } || valuesets.find { |x| x['url'] == uri && x['resourceType'] == 'ValueSet' }
  unless valueset.nil?
    @@cache[uri] = {}
    if !valueset['expansion'].nil? && !valueset['expansion']['contains'].nil?
      keys = valueset['expansion']['contains'].map { |x| x['system'] }.uniq
      keys.each { |x| @@cache[uri][x] = [] }
      valueset['expansion']['contains'].each { |x| @@cache[uri][x['system']] << x['code'] }
    end
    if !valueset['compose'].nil? && !valueset['compose']['include'].nil?
      # for each system, if codes are included add those, otherwise lookup the codesystem in the list
      included_systems = []
      valueset['compose']['include'].each do |code_group|
        system_url = code_group['system']
        @@cache[uri][system_url] ||= []
        code_group['concept']&.each { |y| @@cache[uri][system_url] << y['code'] }
        included_systems << system_url
      end
      included_systems.each { |x| @@cache[uri][x] ||= [] }
      systems = valuesets.select { |x| x['resourceType'] == 'CodeSystem' && included_systems.include?(x['url']) }
      systems.each do |included_system|
        included_system['concept']&.each { |y| @@cache[uri][included_system['url']] << y['code'] }
      end
    end
    @@cache[uri].each { |_system, codes| codes.uniq! }
  end
  @@cache[uri]
end

.get_display(uri, code) ⇒ Object

Get the “display” (human-readable title) for a given code in a code system (uri) If one can’t be found, return nil



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 231

def self.get_display(uri, code)
  return nil if uri.nil? || code.nil?
  valuesets_and_expansions = expansions.select { |ex| ex['compose']['include'].detect { |i| i['system'] == uri } }
  valuesets_and_expansions += valuesets.select { |vs| vs['url'] == uri }
  code_hash = nil
  valuesets_and_expansions.each do |valueset|
    if valueset['expansion'] && valueset['expansion']['contains']
      # This currently only matches 'expansions', not 'valuesets'
      code_hash = valueset['expansion']['contains'].detect { |contained| contained['system'] == uri && contained['code'] == code }
    elsif valueset['compose'] && valueset['compose']['include']
      # This seems to only match 'valuesets'
      valueset['compose']['include'].each do |code_system|
        code_hash = code_system['concept'].detect { |con| con['code'] == code } if code_system['concept']
        break if code_hash
      end
    elsif valueset['concept']
      # This currently only matches 'valuesets', not 'expansions'
      code_hash = valueset['concept'].detect { |vs| vs['code'] == code }
    end
    break if code_hash
  end
  code_hash['display'] if code_hash
end

.get_profile_class(uri) ⇒ Object

Get a dynamically generated class for a given profile.



138
139
140
141
142
143
144
145
146
147
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/fhir_stu3_models/bootstrap/definitions.rb', line 138

def self.get_profile_class(uri)
  return nil if uri.nil?
  load_profiles
  load_extensions

  defn = @@profiles.select { |x| x['url'] == uri }.first
  defn = @@extensions.select { |x| x['url'] == uri }.first if defn.nil?

  klass = nil
  unless defn.nil?
    generator = FHIR::STU3::Boot::Generator.new(false)
    type = defn['baseType']
    id = defn['id'].gsub(/-|_/, '').capitalize
    defn['id'] = type # override profile id with baseType name for generator
    template = generator.generate_class([type], defn)
    f = Tempfile.new(["profile-#{id}", '.rb'])
    f.write("module FHIR\n")
    f.write("module Profile\n")
    f.write("module #{id}\n")
    f.write(template.to_s)
    3.times { f.write("\nend") }
    f.close
    begin
      # load the profiled class
      load f
      # set the return class type
      klass = Object.const_get("FHIR::STU3::Profile::#{id}::#{type}")
    rescue
      FHIR::STU3.logger.error "Failed to generate class for profile #{uri}"
    end
    # unlink the file so it can be garbage collected
    f.unlink
  end
  klass
end

.primitive_typesObject



32
33
34
35
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 32

def self.primitive_types
  # primitive data types start with a lowercase letter
  @primitive_types ||= types.select { |t| t['id'].start_with?(*('a'..'z').to_a) }
end

.profile(uri) ⇒ Object

Get the StructureDefinition for a given profile.



123
124
125
126
127
128
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 123

def self.profile(uri)
  return nil if uri.nil?
  defn = profiles.detect { |x| x['url'] == uri } || extensions.detect { |x| x['url'] == uri }
  return nil if defn.nil?
  FHIR::STU3::StructureDefinition.new(defn)
end

.profiles_for_resource(resource_name) ⇒ Object



131
132
133
134
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 131

def self.profiles_for_resource(resource_name)
  return nil if resource_name.nil?
  profiles.select { |x| x['baseType'] == resource_name }.map { |x| FHIR::STU3::StructureDefinition.new(x) }
end

.resource_definition(resource_name) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 74

def self.resource_definition(resource_name)
  return nil if resource_name.nil?
  return @@cache[resource_name] if @@cache[resource_name]
  definition = resources.find { |x| x['xmlId'] == resource_name || x['name'] == resource_name || x['url'] == resource_name }
  @@cache[resource_name] = FHIR::STU3::StructureDefinition.new(definition) if definition
  @@cache[resource_name]
end

.resource_definitionsObject



69
70
71
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 69

def self.resource_definitions
  resources.select { |r| r['kind'] == 'resource' }
end

.search_parameters(type_name) ⇒ Object



270
271
272
273
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 270

def self.search_parameters(type_name)
  return nil if type_name.nil?
  search_params.select { |p| p['base'].include?(type_name) && p['xpath'] && !p['xpath'].include?('extension') }.map { |p| p['code'] }
end

.type_definition(type_name) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 45

def self.type_definition(type_name)
  return nil if type_name.nil?
  return @@cache[type_name] if @@cache[type_name]
  definition = types.find { |x| x['xmlId'] == type_name || x['name'] == type_name || x['url'] == type_name }
  @@cache[type_name] = FHIR::STU3::StructureDefinition.new(definition) if definition
  @@cache[type_name]
end

.valuesetsObject



188
189
190
191
192
193
194
195
# File 'lib/fhir_stu3_models/bootstrap/definitions.rb', line 188

def self.valuesets
  @@valuesets ||= begin
    # load the valuesets
    filename = File.join(@@defns, 'valuesets', 'valuesets.json')
    raw = File.open(filename, 'r:UTF-8', &:read)
    JSON.parse(raw)['entry'].map { |e| e['resource'] }
  end
end