Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#render_xml(element_name, element) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/util/structformatter.rb', line 40

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



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/util/structformatter.rb', line 61

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



52
53
54
55
56
57
58
59
60
# File 'lib/util/structformatter.rb', line 52

def to_xml
	str = "<hash>"
	self.each do |key,value|
		str += "<element><key>#{key}</key>"
		str += render_xml("value",value)
		str += "</element>"
	end
	str += "</hash>"
end