Class: FHIR::Definitions

Inherits:
Object
  • Object
show all
Extended by:
Deprecate
Defined in:
lib/fhir_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.



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

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



37
38
39
40
41
# File 'lib/fhir_models/bootstrap/definitions.rb', line 37

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



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

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



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

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::StructureDefinition.new(extension)
end

.get_codes(uri) ⇒ Object

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



197
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
228
229
230
# File 'lib/fhir_models/bootstrap/definitions.rb', line 197

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 the expansion is completed already, use it...
    # except for http://hl7.org/fhir/ValueSet/c80-doc-typecodes, because that expansion is missing codes
    if !valueset['expansion'].nil? && !valueset['expansion']['contains'].nil? && uri != 'http://hl7.org/fhir/ValueSet/c80-doc-typecodes'
      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'] }
    elsif !valueset['compose'].nil? && !valueset['compose']['include'].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...
      valueset['compose']['include'].each do |code_group|
        system_url = code_group['system']
        @@cache[uri][system_url] ||= []
        if !code_group['concept'].nil?
          code_group['concept'].each { |y| @@cache[uri][system_url] << y['code'] } if code_group['concept']
        elsif code_group.size == 1
          # i.e. the only key is 'system', so you import the entire thing
          systems = valuesets.select { |x| x['resourceType'] == 'CodeSystem' && x['url'] == system_url }
          systems.each do |included_system|
            included_system['concept'].each { |y| @@cache[uri][system_url] << y['code'] } if included_system['concept']
          end
        end
      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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/fhir_models/bootstrap/definitions.rb', line 234

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.



137
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
# File 'lib/fhir_models/bootstrap/definitions.rb', line 137

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::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::Profile::#{id}::#{type}")
    rescue
      FHIR.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



31
32
33
34
# File 'lib/fhir_models/bootstrap/definitions.rb', line 31

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.



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

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::StructureDefinition.new(defn)
end

.profiles_for_resource(resource_name) ⇒ Object



130
131
132
133
# File 'lib/fhir_models/bootstrap/definitions.rb', line 130

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

.resource_definition(resource_name) ⇒ Object



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

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::StructureDefinition.new(definition) if definition
  @@cache[resource_name]
end

.resource_definitionsObject



68
69
70
# File 'lib/fhir_models/bootstrap/definitions.rb', line 68

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

.search_parameters(type_name) ⇒ Object



273
274
275
276
# File 'lib/fhir_models/bootstrap/definitions.rb', line 273

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



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

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::StructureDefinition.new(definition) if definition
  @@cache[type_name]
end

.valuesetsObject



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

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