Class: FHIR::Definitions

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



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

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.



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

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



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

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



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

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



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

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.



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

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.



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

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 Exception => e
      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



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

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



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

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



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

def self.get_resource_definitions
  load_resources
  @@resources
end

.get_search_parameters(typeName) ⇒ Object



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

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

.get_type_definition(type_name) ⇒ Object



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

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