Class: Struct
- Inherits:
-
Object
- Object
- Struct
- Defined in:
- lib/structformatter.rb
Constant Summary collapse
- @@printclass =
true
Class Method Summary collapse
Instance Method Summary collapse
- #render_xml(element_name, element) ⇒ Object
- #to(format) ⇒ Object
- #to_json(*a) ⇒ Object
- #to_s(sep = " ") ⇒ Object
- #to_s_header(sep = " ") ⇒ Object
- #to_xml ⇒ Object
Class Method Details
.printclass=(pc) ⇒ Object
91 92 93 |
# File 'lib/structformatter.rb', line 91 def Struct::printclass=(pc) @@printclass = pc end |
Instance Method Details
#render_xml(element_name, element) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/structformatter.rb', line 94 def render_xml(element_name, element) str = "" if element.class == Date str = "<#{element_name}>#{element.strftime("%Y-%m-%d")}</#{element_name}>" elsif element.class == Time or element.class == DateTime str = "<#{element_name}>#{element.strftime("%Y-%m-%dT%H:%M:%SZ")}</#{element_name}>" elsif element.kind_of? Struct str = element.to_xml elsif element.kind_of? Hash or element.kind_of? Array str = "<#{element_name}>"+element.to_xml+"</#{element_name}>" else str = "<#{element_name}>#{element.to_s.xml_escape}</#{element_name}>" end end |
#to(format) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/structformatter.rb', line 157 def to(format) case format when 'xml' self.to_xml when 'json' self.to_json when 'string' self.to_s else raise "invalid format: #{format}, use one of xml, json, or string" end end |
#to_json(*a) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/structformatter.rb', line 149 def to_json(*a) hash = (@@printclass) ? { 'class' => self.class } : {} self.members.sort.each do |member| hash[member] = self[member] end hash.to_json(*a) end |
#to_s(sep = " ") ⇒ Object
170 171 172 |
# File 'lib/structformatter.rb', line 170 def to_s(sep = " ") self.members.map{ |x| self[x].to_s }.join(sep) end |
#to_s_header(sep = " ") ⇒ Object
174 175 176 |
# File 'lib/structformatter.rb', line 174 def to_s_header(sep = " ") self.members.map{ |x| x.to_s }.join(sep) end |
#to_xml ⇒ Object
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 |
# File 'lib/structformatter.rb', line 108 def to_xml children = [] str = "<#{self.class}" self.members.each do |member| if self[member].class == Array or self[member].class == Hash or self[member].kind_of? Struct children << member elsif self[member].class == Date str += " #{member}='#{self[member].strftime("%Y-%m-%d")}'" elsif self[member].class == Time str += " #{member}='#{self[member].strftime("%Y-%m-%dT%H:%M:%SZ")}'" else str += " #{member}='#{self[member].to_s.xml_escape}'" end end if children.length == 0 str += ' />' else str += '>' children.each do |member| if self[member].class == Array str += "<#{member}s>" self[member].each do |item| str += render_xml(member,item) end str += "</#{member}s>" elsif self[member].class == Hash str += "<#{member}s>" self[member].each do |key,value| str += "<HashElement><key>#{key}</key><value>" str += render_xml(member,value) str += "</value></HashElement>" end str += "</#{member}s>" elsif self[member].kind_of? Struct str += self[member].to_xml end end str += "</#{self.class}>" end end |