Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/util/structformatter.rb

Instance Method Summary collapse

Instance Method Details

#render_xml(element_name, element) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/util/structformatter.rb', line 6

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 or element.kind_of? Hash or element.kind_of? Array
		str = element.to_xml
	else
		str = "<#{element_name}>#{element}</#{element_name}>"
	end
end

#to(format) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/util/structformatter.rb', line 25

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_xmlObject



18
19
20
21
22
23
24
# File 'lib/util/structformatter.rb', line 18

def to_xml
	str = "<array>"
	self.each do |item|
		str += render_xml("element",item)
	end
	str += "</array>"
end