Class: FHIR::Definitions

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Class Method Details

.get_basetype(uri) ⇒ Object

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



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fhir_models/bootstrap/definitions.rb', line 101

def self.get_basetype(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?

  basetype = nil
  basetype = defn['baseType'] if !defn.nil?
  basetype
end

.get_codes(uri) ⇒ Object

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/fhir_models/bootstrap/definitions.rb', line 191

def self.get_codes(uri)
  return nil if uri.nil?
  load_expansions
  codes = nil
  valueset = @@expansions.select{|x|x['url']==uri}.first
  if !valueset.nil?
    codes = {}
    if !valueset['expansion'].nil? && !valueset['expansion']['contains'].nil?
      keys = valueset['expansion']['contains'].map{|x|x['system']}.uniq
      keys.each{|x| codes[x]=[]}
      valueset['expansion']['contains'].each{|x| codes[x['system']] << x['code']}
    end
    if !valueset['compose'].nil? && !valueset['compose']['include'].nil?
      included_systems = valueset['compose']['include'].map{|x|x['system']}.uniq
      included_systems.each{|x| codes[x]=[] if !codes.keys.include?(x) }
      systems = @@valuesets.select{|x|x['resourceType']=='CodeSystem' && included_systems.include?(x['url'])}
      systems.each do |x|
        x['concept'].each{|y| codes[x['url']] << y['code']}
      end
    end
  end
  codes
end

.get_complex_typesObject



33
34
35
36
37
38
# File 'lib/fhir_models/bootstrap/definitions.rb', line 33

def self.get_complex_types
  load_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)
  @@types.select{|t| (t['id'][0]==t['id'][0].upcase) && (t['id']==t['snapshot']['element'].first['path'])}
end

.get_extension_definition(extension_name) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/fhir_models/bootstrap/definitions.rb', line 92

def self.get_extension_definition(extension_name)
  return nil if extension_name.nil?
  load_extensions
  d = @@extensions.find{|x|x['xmlId']==extension_name || x['name']==extension_name || x['url']==extension_name}
  d = FHIR::StructureDefinition.new(d) if !d.nil?
  d
end

.get_primitive_typesObject



27
28
29
30
31
# File 'lib/fhir_models/bootstrap/definitions.rb', line 27

def self.get_primitive_types
  load_types
  # primitive data types start with a lowercase letter
  @@types.select{|t|t['id'][0]==t['id'][0].downcase}
end

.get_profile(uri) ⇒ Object

Get the StructureDefinition for a given profile.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fhir_models/bootstrap/definitions.rb', line 115

def self.get_profile(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?

  profile = nil
  profile = FHIR::StructureDefinition.new(defn) if !defn.nil?
  profile
end

.get_profile_class(uri) ⇒ Object

Get a dynamically generated class for a given profile.



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

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
  if !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

.get_profiles_for_resource(resource_name) ⇒ Object



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

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

.get_resource_definition(resource_name) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/fhir_models/bootstrap/definitions.rb', line 66

def self.get_resource_definition(resource_name)
  return nil if resource_name.nil?
  load_resources
  d = @@resources.find{|x|x['xmlId']==resource_name || x['name']==resource_name || x['url']==resource_name}
  d = FHIR::StructureDefinition.new(d) if !d.nil?
  d
end

.get_resource_definitionsObject



61
62
63
64
# File 'lib/fhir_models/bootstrap/definitions.rb', line 61

def self.get_resource_definitions
  load_resources
  @@resources
end

.get_search_parameters(type_name) ⇒ Object



228
229
230
231
232
# File 'lib/fhir_models/bootstrap/definitions.rb', line 228

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

.get_type_definition(type_name) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/fhir_models/bootstrap/definitions.rb', line 40

def self.get_type_definition(type_name)
  return nil if type_name.nil?
  load_types
  d = @@types.find{|x|x['xmlId']==type_name || x['name']==type_name || x['url']==type_name}
  d = FHIR::StructureDefinition.new(d) if !d.nil?
  d
end