Class: FHIR::Boot::Template
- Inherits:
-
Object
- Object
- FHIR::Boot::Template
- Extended by:
- Deprecate
- Includes:
- Hashable
- Defined in:
- lib/fhir_models/bootstrap/template.rb
Instance Attribute Summary collapse
-
#constants ⇒ Object
Returns the value of attribute constants.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#hierarchy ⇒ Object
Returns the value of attribute hierarchy.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#name ⇒ Object
Returns the value of attribute name.
-
#templates ⇒ Object
Returns the value of attribute templates.
-
#top_level ⇒ Object
Returns the value of attribute top_level.
Instance Method Summary collapse
- #indent(level = 0, offset = 0) ⇒ Object
-
#initialize(name = ['Template'], top_level = false) ⇒ Template
constructor
A new instance of Template.
- #metadata ⇒ Object
- #to_s(offset = 0) ⇒ Object
Methods included from Deprecate
Methods included from Hashable
Constructor Details
#initialize(name = ['Template'], top_level = false) ⇒ Template
Returns a new instance of Template.
15 16 17 18 19 20 21 22 23 |
# File 'lib/fhir_models/bootstrap/template.rb', line 15 def initialize(name = ['Template'], top_level = false) @name = name @hierarchy = [] @kind = nil @constants = {} @fields = [] @templates = [] @top_level = top_level end |
Instance Attribute Details
#constants ⇒ Object
Returns the value of attribute constants.
10 11 12 |
# File 'lib/fhir_models/bootstrap/template.rb', line 10 def constants @constants end |
#fields ⇒ Object
Returns the value of attribute fields.
11 12 13 |
# File 'lib/fhir_models/bootstrap/template.rb', line 11 def fields @fields end |
#hierarchy ⇒ Object
Returns the value of attribute hierarchy.
8 9 10 |
# File 'lib/fhir_models/bootstrap/template.rb', line 8 def hierarchy @hierarchy end |
#kind ⇒ Object
Returns the value of attribute kind.
9 10 11 |
# File 'lib/fhir_models/bootstrap/template.rb', line 9 def kind @kind end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/fhir_models/bootstrap/template.rb', line 7 def name @name end |
#templates ⇒ Object
Returns the value of attribute templates.
12 13 14 |
# File 'lib/fhir_models/bootstrap/template.rb', line 12 def templates @templates end |
#top_level ⇒ Object
Returns the value of attribute top_level.
13 14 15 |
# File 'lib/fhir_models/bootstrap/template.rb', line 13 def top_level @top_level end |
Instance Method Details
#indent(level = 0, offset = 0) ⇒ Object
131 132 133 |
# File 'lib/fhir_models/bootstrap/template.rb', line 131 def indent(level = 0, offset = 0) ' ' * offset + ' ' * level end |
#metadata ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/fhir_models/bootstrap/template.rb', line 25 def = {} @fields.each do |field| if .keys.include?(field.name) # this field has already been declared, so we're dealing # with a `slice` which we'll track with an array. x = field.serialize x.delete('name') if [field.name].is_a?(Array) [field.name] << x else [field.name] = [[field.name], x] end else [field.name] = field.serialize [field.name].delete('name') end end end |
#to_s(offset = 0) ⇒ Object
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 |
# File 'lib/fhir_models/bootstrap/template.rb', line 47 def to_s(offset = 0) # create an array of Strings, one per line s = [] # TODO: insert copyright statement # always declare the FHIR module s << 'module FHIR' if @top_level @name.each_with_index do |name, index| space = indent(index + 1, offset) type = 'module' type = 'class' if index == @name.length - 1 classdef = "#{space}#{type} #{name}" classdef += ' < FHIR::Model' if type == 'class' s << classdef end # include modules space = indent(@name.length + 1, offset) s << "#{space}include FHIR::Hashable" unless @name.empty? s << "#{space}include FHIR::Json" unless @name.empty? s << "#{space}include FHIR::Xml" unless @name.empty? s << '' # add mandatory METADATA constant @constants['METADATA'] = unless .empty? # add constants @constants.each do |constant, value| if value.is_a?(Hash) s << "#{space}#{constant.upcase} = {" value.each do |k, v| s << "#{space} '#{k}' => #{v}," # Replace wildcard string with Infinity constant s[-1].gsub!('"max"=>"*"', '"max"=>Float::INFINITY') s[-1].tr!('"', '\'') end s[-1] = s[-1][0..-2] # remove the trailing comma s << "#{space}}" else s << "#{space}#{constant.upcase} = #{value.to_s.tr!('"', '\'')}" end end s << '' # add internal nested classes @templates.each do |template| s << template.to_s(space.length - 2) s << '' end # calculate the longest field name for whitespace layout max_name_size = (@fields.map { |field| field.local_name || field.name }.map(&:length).max || 0) + 1 # declare attributes @fields.each do |field| s << "#{space}attr_accessor :" local_name = field.local_name || field.name s[-1] << format("%-#{max_name_size}s", local_name) # add comment after field declaration s[-1] << "# #{field.min}-#{field.max} " s[-1] << '[ ' if field.max.to_i > 1 || field.max == '*' s[-1] << field.type if field.type == 'Reference' s[-1] << "(#{field.type_profiles.map { |p| p.split('/').last }.join('|')})" end s[-1] << ' ]' if field.max.to_i > 1 || field.max == '*' end if @top_level && @kind == 'resource' s << '' s << "#{space}def resourceType" s << "#{space} '#{@name.first}'" s << "#{space}end" end # close all the class and module declarations (0..@name.length - 1).reverse_each do |index| space = indent(index + 1, offset) s << "#{space}end" end s << 'end' if @top_level s.join("\n") end |