hash_to_xml 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/hash_to_xml.rb +43 -8
- metadata +10 -5
data/lib/hash_to_xml.rb
CHANGED
@@ -2,17 +2,52 @@
|
|
2
2
|
require 'rubygems'
|
3
3
|
require 'nokogiri'
|
4
4
|
class Hash
|
5
|
+
|
5
6
|
def to_xml
|
7
|
+
#pp "self : #{self}"
|
6
8
|
@to_xml_doc = ::Nokogiri::XML( '<root />' )
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
|
10
|
+
def recurse( parent, collection )
|
11
|
+
#pp '-' * 80
|
12
|
+
#pp "parent : #{parent.to_xml}"
|
13
|
+
case collection
|
14
|
+
|
15
|
+
when Hash
|
16
|
+
collection.each_pair { | key, value |
|
17
|
+
#pp "key : #{key}"
|
18
|
+
#pp "value : #{value}"
|
19
|
+
node = Nokogiri::XML::Node.new key, @to_xml_doc
|
20
|
+
case value
|
21
|
+
when Hash, Array
|
22
|
+
node = recurse( node, value )
|
23
|
+
else
|
24
|
+
node.content = value
|
25
|
+
end # case value
|
26
|
+
parent << node
|
27
|
+
}
|
28
|
+
|
29
|
+
when Array
|
30
|
+
node = parent
|
31
|
+
collection.each { | value |
|
32
|
+
#pp "value : #{value}"
|
33
|
+
case value
|
34
|
+
when Hash, Array
|
35
|
+
node = recurse( node, value )
|
36
|
+
else
|
37
|
+
node.content = value
|
38
|
+
end # case value
|
39
|
+
}
|
40
|
+
|
41
|
+
else
|
42
|
+
fail "Unhandeled class: #{collection.class}"
|
43
|
+
end # case collection
|
44
|
+
|
13
45
|
return parent
|
14
|
-
|
46
|
+
|
47
|
+
end # def recurse
|
48
|
+
|
15
49
|
recurse( @to_xml_doc.root, self )
|
16
50
|
return @to_xml_doc.to_xml
|
17
|
-
end
|
51
|
+
end # def to_xml
|
52
|
+
|
18
53
|
end # hash
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_to_xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: Add method to_xml on ruby Hash class. The method returns a xml string.
|
26
31
|
email: comlock@gmail.com
|
27
32
|
executables: []
|
@@ -51,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
56
|
requirements:
|
52
57
|
- nokogiri
|
53
58
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.8.
|
59
|
+
rubygems_version: 1.8.18
|
55
60
|
signing_key:
|
56
61
|
specification_version: 3
|
57
62
|
summary: hash.to_xml
|