Class: Struct
- Inherits:
-
Object
show all
- Defined in:
- lib/util/structformatter.rb
Constant Summary
collapse
- @@printclass =
true
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.printclass=(pc) ⇒ Object
77
78
79
|
# File 'lib/util/structformatter.rb', line 77
def Struct::printclass=(pc)
@@printclass = pc
end
|
Instance Method Details
#render_xml(element_name, element) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/util/structformatter.rb', line 80
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.to_xml(element_name)
else
str = "<#{element_name}>#{element}</#{element_name}>"
end
end
|
#to(format) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/util/structformatter.rb', line 143
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
135
136
137
138
139
140
141
|
# File 'lib/util/structformatter.rb', line 135
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
156
157
158
|
# File 'lib/util/structformatter.rb', line 156
def to_s(sep = " ")
self.members.map{ |x| self[x].to_s }.join(sep)
end
|
160
161
162
|
# File 'lib/util/structformatter.rb', line 160
def (sep = " ")
self.members.map{ |x| x.to_s }.join(sep)
end
|
#to_xml ⇒ Object
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/util/structformatter.rb', line 94
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]}'"
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
|