Class: FHIR::Generator::ClassGenerator
- Inherits:
-
Object
- Object
- FHIR::Generator::ClassGenerator
- Defined in:
- lib/fhir_models/generator/class_generator.rb
Constant Summary collapse
- KNOWN_MISSING_EXPANSIONS =
['bcp47', 'bcp13.txt', 'mimetypes', 'LL379-9'].freeze
Instance Attribute Summary collapse
-
#ig_resources ⇒ Object
Returns the value of attribute ig_resources.
-
#output_folder ⇒ Object
Returns the value of attribute output_folder.
-
#structure_defs ⇒ Object
Returns the value of attribute structure_defs.
Instance Method Summary collapse
-
#cap_first(string) ⇒ Object
This method is necessary for fields such as ‘codeFilter`.
- #generate ⇒ Object
- #generate_class(hierarchy, structure_def, top_level: false) ⇒ Object
-
#initialize(structure_defs, ig_resources, output_folder) ⇒ ClassGenerator
constructor
A new instance of ClassGenerator.
Constructor Details
#initialize(structure_defs, ig_resources, output_folder) ⇒ ClassGenerator
Returns a new instance of ClassGenerator.
9 10 11 12 13 14 |
# File 'lib/fhir_models/generator/class_generator.rb', line 9 def initialize(structure_defs, ig_resources, output_folder) self.structure_defs = structure_defs self.ig_resources = ig_resources self.output_folder = output_folder @templates = [] end |
Instance Attribute Details
#ig_resources ⇒ Object
Returns the value of attribute ig_resources.
7 8 9 |
# File 'lib/fhir_models/generator/class_generator.rb', line 7 def ig_resources @ig_resources end |
#output_folder ⇒ Object
Returns the value of attribute output_folder.
7 8 9 |
# File 'lib/fhir_models/generator/class_generator.rb', line 7 def output_folder @output_folder end |
#structure_defs ⇒ Object
Returns the value of attribute structure_defs.
7 8 9 |
# File 'lib/fhir_models/generator/class_generator.rb', line 7 def structure_defs @structure_defs end |
Instance Method Details
#cap_first(string) ⇒ Object
This method is necessary for fields such as ‘codeFilter`. “codeFilter”.capitalize -> “Codefilter”, but “CodeFilter” is desired output.
31 32 33 34 35 |
# File 'lib/fhir_models/generator/class_generator.rb', line 31 def cap_first(string) t = String.new(string) t[0] = t[0].upcase t end |
#generate ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/fhir_models/generator/class_generator.rb', line 16 def generate structure_defs.each do |structure_def| @templates.clear type_name = structure_def['id'] template = generate_class([type_name], structure_def, top_level: true) params = ig_resources.get_search_parameters(type_name).map { |p| p['code'] } template.constants['SEARCH_PARAMS'] = params.sort unless params.nil? filename = File.join(output_folder, "#{type_name}.rb") file = File.open(filename, 'w:UTF-8') file.write(template.to_s) file.close end end |
#generate_class(hierarchy, structure_def, top_level: false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 131 132 133 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/fhir_models/generator/class_generator.rb', line 37 def generate_class(hierarchy, structure_def, top_level: false) type_name = structure_def['id'] path_type = structure_def['type'] || type_name template = Template.new([type_name], top_level) template.hierarchy = hierarchy template.kind = structure_def['kind'] template.fhir_version = ig_resources..shortName return template if structure_def['snapshot'].nil? || structure_def['snapshot']['element'].nil? multiple_data_types = {} # examine the snapshot.elements... move the Element and BackboneElements, # and their children, into separate StructureDefiniton hashes and process as # child templates. child_templates = [] structure_def['snapshot']['element'].each do |element| # skip the first element next if element['path'] == path_type next unless element['type'] unique_types = element['type'].map { |t| t['code'] }.uniq child_templates << element['path'] if unique_types.include?('Element') || unique_types.include?('BackboneElement') end # now build the child templates... child_templates.each do |child_name| child_fixed_name = cap_first(child_name.gsub("#{type_name}.", '')) next if child_fixed_name.include?('.') child_def = { 'id' => child_fixed_name, 'snapshot' => { 'element' => [] } } # Copy the element definitions for the child structure structure_def['snapshot']['element'].each do |element| child_def['snapshot']['element'] << element.clone if element['path'].start_with?("#{child_name}.") end # Remove the child elements child_paths = child_def['snapshot']['element'].map { |e| e['path'] } # child_paths = child_paths.drop(1) structure_def['snapshot']['element'].keep_if do |element| !child_paths.include?(element['path']) end # Rename the child paths child_def['snapshot']['element'].each do |element| element['path'] = element['path'].gsub(child_name, child_fixed_name) end # add the child child_hierarchy = hierarchy + [child_fixed_name] child_klass = generate_class(child_hierarchy, child_def) template.templates << child_klass @templates << child_klass end # Process the remaining attributes (none of which are Elements or BackboneElements) structure_def['snapshot']['element'].each do |element| # skip the first element next if element['path'] == path_type field_base_name = element['path'].gsub("#{path_type}.", '') # If the element has a type, treat it as a datatype or resource # If not, treat it as a reference to an already declared internal class if !element['type'].nil? # profiles contains a list of profiles if the datatype is Reference or Extension profiles = [] element['type'].select { |t| t['code'] == 'Reference' || t['code'] == 'Extension' }.each do |data_type| profiles << data_type['targetProfile'] end profiles.reject!(&:nil?) profiles.flatten! # Calculate fields that have multiple data types if element['type'].length > 1 fieldname = field_base_name.gsub('[x]', '') unique_types = element['type'].map { |t| t['code'] }.uniq multiple_data_types[fieldname] = unique_types if unique_types.length > 1 end # generate a field for each valid datatype... this is for things like Resource.attribute[x] element['type'].each do |type| extension = type['extension'] data_type = type['code'] || 'string' if element['path'].end_with?('.id') data_type = element['base']['path'] == 'Resource.id' ? 'id' : 'string' elsif data_type == 'http://hl7.org/fhirpath/System.String' && extension data_type = extension.first['valueUrl'] end fieldname = field_base_name.gsub('[x]', cap_first(data_type)) field = FHIR::Field.new(fieldname) field.path = element['path'].gsub(path_type, type_name) field.type = data_type field.type = 'Extension' if field.path.end_with?('extension') field.type_profiles = profiles if ['Reference', 'Extension'].include?(data_type) field.min = element['min'] field.max = element['max'] field.max = field.max.to_i field.max = '*' if element['max'] == '*' if ['code', 'Coding', 'CodeableConcept'].include?(data_type) && element['binding'] field.binding = element['binding'] field.binding['uri'] = field.binding['valueSet'] field.binding.delete('valueSet') field.binding.delete('description') field.binding.delete('extension') # set the actual code list binding_uri = field.binding['uri'] # Strip off the |4.0.0 or |4.0.1 or |2014-03-26 or similar from the ends of URLs binding_uri&.gsub!(/\|[A-Za-z0-9.-]*/, '') codes = ig_resources.get_codes(binding_uri) field.valid_codes = codes unless codes.nil? if field.valid_codes.empty? && binding_uri && !binding_uri.end_with?(*KNOWN_MISSING_EXPANSIONS) FHIR.logger.warn " MISSING EXPANSION -- #{field.path} #{field.min}..#{field.max}: #{binding_uri} (#{field.binding['strength']})" @missing_expansions = true @missing_required_expansion ||= (field.binding['strength'] == 'required') end elsif ['Element', 'BackboneElement'].include?(data_type) # This is a nested structure or class field.type = "#{hierarchy.join('::')}::#{cap_first(field.name)}" end template.fields << field end else # If there is no data type, treat the type as a reference to an already declared internal class field = FHIR::Field.new(field_base_name) field.path = element['path'].gsub(path_type, type_name) field.type = element['contentReference'] field.type = field.type[1..] if field.type[0] == '#' if hierarchy.last == field.type # reference to self field.type = hierarchy.join('::').to_s else # reference to contained template klass = @templates.select { |x| x.hierarchy.last == field.type }.first field.type = if !klass.nil? # the template/child class was declared somewhere else in this class hierarchy klass.hierarchy.join('::') else # the template/child is a direct ancester (it isn't in @templates yet because it is being defined now) field.type.split('.').map { |x| cap_first(x) }.join('::') end end field.min = element['min'] field.max = element['max'] field.max = field.max.to_i field.max = '*' if element['max'] == '*' template.fields << field end end template.constants['MULTIPLE_TYPES'] = multiple_data_types unless multiple_data_types.empty? template end |