18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/hashutils.rb', line 18
def to_xml(args = {:name => nil, :pretty => false})
tab = (args[:tab].nil?)? "" : args[:tab]
build_attrs = Proc.new do |k, v|
attributes = (self.respond_to?"xml_attributes" and !self.xml_attributes.nil?) ? self.xml_attributes : []
out = ""
out += " " + k.to_s + "=\"" + v.to_s + "\"" if attributes.select{|x| x == k}.length > 0
out
end
build_body = Proc.new do |k, v|
attributes = (self.respond_to?"xml_attributes" and !self.xml_attributes.nil?) ? self.xml_attributes : []
out = ""
if attributes.select{|x| x == k}.length == 0
new_args = args.merge({:tab => tab, :name => k})
out += v.to_xml(new_args) if v.respond_to?"to_xml"
out += ((args[:pretty])? tab : "") + @@lt + k.to_s + @@gt + v.to_s + @@clt + k.to_s + @@gt unless v.respond_to?"to_xml"
out += @@new_line if args[:pretty]
end
out
end
out = ""
if !args[:name].nil?
out += ((args[:pretty])? tab : "") + @@lt + args[:name].to_s
self.each_pair {|k, v| out += build_attrs.call(k, v, args)} if self.respond_to?"each_pair"
out += @@gt
end
orig_tab = tab
if !args[:name].nil? and args[:pretty]
out += @@new_line
tab += @@default_tab
end
self.each_pair {|k, v| out += build_body.call(k, v)} if self.respond_to?"each_pair"
self.each {|v| out += build_body.call((@xml_name.nil?) ? "item" : @xml_name, v)} if !self.respond_to?"each_pair"
out += ((args[:pretty])? orig_tab : "") + @@clt + args[:name].to_s + @@gt if !args[:name].nil?
out
end
|