Class: FHIR::Boot::Template
- Inherits:
-
Object
- Object
- FHIR::Boot::Template
- Includes:
- Hashable
- Defined in:
- lib/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
- #get_metadata ⇒ Object
- #indent(level = 0, offset) ⇒ Object
-
#initialize(name = ['Template'], top_level = false) ⇒ Template
constructor
A new instance of Template.
- #to_s(offset = 0) ⇒ Object
Methods included from Hashable
Constructor Details
#initialize(name = ['Template'], top_level = false) ⇒ Template
Returns a new instance of Template.
14 15 16 17 18 19 20 21 22 |
# File 'lib/bootstrap/template.rb', line 14 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.
9 10 11 |
# File 'lib/bootstrap/template.rb', line 9 def constants @constants end |
#fields ⇒ Object
Returns the value of attribute fields.
10 11 12 |
# File 'lib/bootstrap/template.rb', line 10 def fields @fields end |
#hierarchy ⇒ Object
Returns the value of attribute hierarchy.
7 8 9 |
# File 'lib/bootstrap/template.rb', line 7 def hierarchy @hierarchy end |
#kind ⇒ Object
Returns the value of attribute kind.
8 9 10 |
# File 'lib/bootstrap/template.rb', line 8 def kind @kind end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/bootstrap/template.rb', line 6 def name @name end |
#templates ⇒ Object
Returns the value of attribute templates.
11 12 13 |
# File 'lib/bootstrap/template.rb', line 11 def templates @templates end |
#top_level ⇒ Object
Returns the value of attribute top_level.
12 13 14 |
# File 'lib/bootstrap/template.rb', line 12 def top_level @top_level end |
Instance Method Details
#get_metadata ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bootstrap/template.rb', line 24 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 |
#indent(level = 0, offset) ⇒ Object
135 136 137 |
# File 'lib/bootstrap/template.rb', line 135 def indent(level=0,offset) ' '*(offset) + ' '*(level) end |
#to_s(offset = 0) ⇒ Object
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 |
# File 'lib/bootstrap/template.rb', line 45 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" if(@name.length > 0) s << "#{space}include FHIR::Json" if(@name.length > 0) s << "#{space}include FHIR::Xml" if(@name.length > 0) s << '' # add mandatory METADATA constant = @constants['METADATA'] = if !.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].gsub!('"','\'') end s[-1] = s[-1][0..-2] # remove the trailing comma s << "#{space}}" else s << "#{space}#{constant.upcase} = #{value}" 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 = 0 @fields.each do |f| name = f.local_name || f.name max_name_size=name.length if(name.length > max_name_size) end max_name_size += 1 # declare attributes @fields.each do |field| s << "#{space}attr_accessor :" local_name = field.local_name || field.name s[-1] << ("%-#{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 |