saxy 0.1.1 → 0.1.2
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/README.md +20 -12
- data/lib/saxy/element.rb +13 -2
- data/lib/saxy/version.rb +1 -1
- data/spec/saxy/element_spec.rb +6 -0
- data/spec/saxy_spec.rb +7 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -29,13 +29,15 @@ Assume the XML file:
|
|
29
29
|
<name>Amazon</name>
|
30
30
|
<products>
|
31
31
|
<product>
|
32
|
-
<
|
33
|
-
<name>Kindle</name>
|
34
|
-
<description>The world's best-selling e-reader.</description>
|
35
|
-
<price>$109</price>
|
32
|
+
<name>Kindle - The world's best-selling e-reader.</name>
|
36
33
|
<images>
|
37
34
|
<thumb>http://amazon.com/kindle_thumb.jpg</thumb>
|
38
|
-
|
35
|
+
</images>
|
36
|
+
</product>
|
37
|
+
<product>
|
38
|
+
<name>Kindle Touch - Simple-to-use touchscreen with built-in WIFI.</name>
|
39
|
+
<images>
|
40
|
+
<thumb>http://amazon.com/kindle_touch_thumb.jpg</thumb>
|
39
41
|
</images>
|
40
42
|
</product>
|
41
43
|
</products>
|
@@ -46,15 +48,16 @@ You instantiate the parser by passing path to XML file and object-identyfing tag
|
|
46
48
|
The following will parse the XML, find product definitions (inside `<product>` and `</product>` tags), build `OpenStruct`s and yield them inside the block:
|
47
49
|
|
48
50
|
Saxy.parse("filename.xml", "product").each do |product|
|
49
|
-
puts product.
|
50
|
-
puts product.
|
51
|
-
puts product.description # => "The world's best-selling e-reader."
|
52
|
-
puts product.price # => "$109"
|
53
|
-
|
54
|
-
# nested objects are build as well
|
55
|
-
puts product.images.thumb # => "http://amazon.com/kindle_thumb.jpg"
|
51
|
+
puts product.name
|
52
|
+
puts product.images.thumb
|
56
53
|
end
|
57
54
|
|
55
|
+
# =>
|
56
|
+
Kindle - The world's best-selling e-reader.
|
57
|
+
http://amazon.com/kindle_thumb.jpg
|
58
|
+
Kindle Touch - Simple-to-use touchscreen with built-in WIFI.
|
59
|
+
http://amazon.com/kindle_touch_thumb.jpg
|
60
|
+
|
58
61
|
Saxy supports Enumerable, so you can use it's goodies to your comfort without building intermediate arrays:
|
59
62
|
|
60
63
|
Saxy.parse("filename.xml", "product").map do |object|
|
@@ -65,6 +68,11 @@ You can also grab an Enumerator for external use (e.g. lazy evaluation, etc.):
|
|
65
68
|
|
66
69
|
enumerator = Saxy.parse("filename.xml", "product").each
|
67
70
|
|
71
|
+
Multiple definitions of child objects are grouped in arrays:
|
72
|
+
|
73
|
+
webstore = Saxy.parse("filename.xml", "webstore").first
|
74
|
+
webstore.products.product.size # => 2
|
75
|
+
|
68
76
|
## Contributing
|
69
77
|
|
70
78
|
1. Fork it
|
data/lib/saxy/element.rb
CHANGED
@@ -11,7 +11,9 @@ module Saxy
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def set_attribute(name, value)
|
14
|
-
|
14
|
+
name = attribute_name(name)
|
15
|
+
attributes[name] ||= []
|
16
|
+
attributes[name] << value
|
15
17
|
end
|
16
18
|
|
17
19
|
def append_value(string)
|
@@ -22,7 +24,16 @@ module Saxy
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def as_object
|
25
|
-
attributes.any?
|
27
|
+
if attributes.any?
|
28
|
+
object = OpenStruct.new
|
29
|
+
attributes.each do |name, value|
|
30
|
+
value = value.first if value.size == 1
|
31
|
+
object.send("#{name}=", value)
|
32
|
+
end
|
33
|
+
object
|
34
|
+
else
|
35
|
+
value
|
36
|
+
end
|
26
37
|
end
|
27
38
|
|
28
39
|
def attribute_name(name)
|
data/lib/saxy/version.rb
CHANGED
data/spec/saxy/element_spec.rb
CHANGED
@@ -31,4 +31,10 @@ describe Saxy::Element do
|
|
31
31
|
element.set_attribute("FooBar", "baz")
|
32
32
|
element.as_object.foo_bar.should == "baz"
|
33
33
|
end
|
34
|
+
|
35
|
+
it "should create array if adding multiple attributtes with the same name" do
|
36
|
+
element.set_attribute("foo", "bar")
|
37
|
+
element.set_attribute("foo", "baz")
|
38
|
+
element.as_object.foo.should == ["bar", "baz"]
|
39
|
+
end
|
34
40
|
end
|
data/spec/saxy_spec.rb
CHANGED
@@ -24,6 +24,13 @@ describe Saxy do
|
|
24
24
|
products[1].images.large.should == "http://amazon.com/kindle_touch.jpg"
|
25
25
|
end
|
26
26
|
|
27
|
+
it "should group multiple definitions of child objects into arrays" do
|
28
|
+
webstore = Saxy.parse(fixture_file("webstore.xml"), "webstore").first
|
29
|
+
|
30
|
+
webstore.products.product.should be_instance_of Array
|
31
|
+
webstore.products.product.size.should == 2
|
32
|
+
end
|
33
|
+
|
27
34
|
it "should return Enumerator when calling #parse without a block", :unless => RUBY_1_8 do
|
28
35
|
Saxy.parse(fixture_file("webstore.xml"), "product").each.should be_instance_of Enumerator
|
29
36
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: saxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Micha\xC5\x82 Szajbe"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-08-
|
13
|
+
date: 2012-08-19 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|