xmlparsable 1.5.2 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +84 -74
- data/lib/xmlparsable/elements/record.rb +1 -1
- data/lib/xmlparsable.rb +1 -1
- data/spec/examples/accessors.example +19 -5
- metadata +1 -1
data/README.md
CHANGED
@@ -4,80 +4,90 @@ XmlParsable is a simple means to declare how an XML grammar maps to Ruby objects
|
|
4
4
|
perform validation of the input, and it is only able to describe fairly simple structures. It
|
5
5
|
relies on LibXML::XML::SaxParser.
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
7
|
+
## Quick Example
|
8
|
+
|
9
|
+
For more details, see the RSpec examples in `spec/examples`.
|
10
|
+
|
11
|
+
### Declare XML grammar
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require "xmlparsable"
|
15
|
+
|
16
|
+
# Needed only for using the "collection" declaration or
|
17
|
+
# implement your own String#pluralize, String#singularize
|
18
|
+
require "active_support/core_ext"
|
19
|
+
|
20
|
+
class AddressBook
|
21
|
+
include XmlParsable
|
22
|
+
|
23
|
+
class Contact
|
24
|
+
include XmlParsable
|
25
|
+
element :givenname
|
26
|
+
element :familyname
|
27
|
+
element :phone
|
28
|
+
element :email
|
29
|
+
element :spouse, Contact
|
30
|
+
collection :children, Contact
|
31
|
+
element :birthdate, DateElement
|
32
|
+
element :updated, TimeElement
|
33
|
+
end
|
34
|
+
|
35
|
+
collection :contact, Contact
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Parse input XML from string, IO, or File
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
book = AddressBook.parse(<<-XML)
|
43
|
+
<contacts>
|
44
|
+
<contact>
|
45
|
+
<familyname>Kim</familyname>
|
46
|
+
<givenname>Jong-il</givenname>
|
47
|
+
<birthdate>1942-02-16</birthdate>
|
48
|
+
<updated>2010-01-01T12:45:30</updated>
|
49
|
+
<children>
|
50
|
+
<child><givenname>Sul-song</givenname></child>
|
51
|
+
<child><givenname>Jong-chul</givenname></child>
|
52
|
+
<child><givenname>Jong-un</givenname></child>
|
53
|
+
<child>
|
54
|
+
<givenname>Jong-nam</givenname>
|
55
|
+
<spouse>
|
56
|
+
<givenname>Jong-hui</givenname>
|
57
|
+
<familyname>Shin</familyname>
|
58
|
+
</spouse>
|
59
|
+
</child>
|
60
|
+
</children>
|
61
|
+
</contact>
|
62
|
+
|
63
|
+
<contact>
|
64
|
+
<givenname>Vladimir</givenname>
|
65
|
+
<familyname>Putin</familyname>
|
66
|
+
<email>vlad@gmail.com</email>
|
67
|
+
<spouse>
|
68
|
+
<givenname>Lyudmila</givenname>
|
69
|
+
<familyname>Putina</familyname>
|
70
|
+
</spouse>
|
71
|
+
</contact>
|
72
|
+
</contacts>
|
73
|
+
XML
|
74
|
+
```
|
75
|
+
|
76
|
+
### Iterate parsed data
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
kim, vlad = book.contacts
|
80
|
+
kim.givenname #=> "Kim"
|
81
|
+
kim.birthdate #=> #<Date: 4860813/2,0,2299161>
|
82
|
+
kim.updated #=> Fri Jan 01 12:45:30 2010
|
83
|
+
|
84
|
+
sulsong, jongchul, jongun, jongnam = kim.children
|
85
|
+
jongnam.spouse.givenname #=> "Jong-hui"
|
86
|
+
jongnam.spouse.familyname #=> "Shin"
|
87
|
+
|
88
|
+
vlad.email #=> "vlad@gmail.com"
|
89
|
+
vlad.spouse #=> #<AddressBook::Contact @givenname="Lyudmila" @familyname="Putina">
|
90
|
+
```
|
81
91
|
|
82
92
|
## Data Types
|
83
93
|
|
@@ -5,7 +5,7 @@ module XmlParsable
|
|
5
5
|
|
6
6
|
def initialize(name, attributes, parent, target, parsers, arguments)
|
7
7
|
@name, @attributes, @parent, @target, @parsers, @elements =
|
8
|
-
name, attributes, parent, target, parsers, Hash.new
|
8
|
+
name, attributes, parent, target, parsers, Hash.new{|h,k| h.fetch(k.to_sym, nil) }
|
9
9
|
end
|
10
10
|
|
11
11
|
def open(name, attributes)
|
data/lib/xmlparsable.rb
CHANGED
@@ -43,7 +43,7 @@ module XmlParsable
|
|
43
43
|
attr_name = arguments.fetch(:accessor, elem_name)
|
44
44
|
|
45
45
|
if attr_name and not method_defined?(attr_name)
|
46
|
-
define_method(attr_name) { instance_variable_get(:@xmlelems)[elem_name] }
|
46
|
+
define_method(attr_name) { instance_variable_get(:@xmlelems)[elem_name.to_sym] }
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -8,6 +8,7 @@ describe XmlParsable, "accessors" do
|
|
8
8
|
<size>500</size>
|
9
9
|
<wise>none</wise>
|
10
10
|
<secret>Ovaltine</secret>
|
11
|
+
<a-z>alphabutt</a-z>
|
11
12
|
</root>
|
12
13
|
XML
|
13
14
|
end
|
@@ -22,6 +23,7 @@ describe XmlParsable, "accessors" do
|
|
22
23
|
include XmlParsable
|
23
24
|
element :size
|
24
25
|
element :wise
|
26
|
+
element "a-z"
|
25
27
|
end
|
26
28
|
|
27
29
|
include XmlParsable
|
@@ -36,14 +38,23 @@ describe XmlParsable, "accessors" do
|
|
36
38
|
|
37
39
|
it "stores elements in `xmlelems`" do
|
38
40
|
xml = parser.parse(input)
|
39
|
-
xml.root.xmlelems[:size]
|
40
|
-
xml.root.xmlelems[:wise]
|
41
|
+
xml.root.xmlelems[:size].should == "500"
|
42
|
+
xml.root.xmlelems[:wise].should == "none"
|
43
|
+
xml.root.xmlelems[:"a-z"].should == "alphabutt"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "stores elements with 'indifferent access'" do
|
47
|
+
xml = parser.parse(input)
|
48
|
+
xml.root.xmlelems["size"].should == xml.root.xmlelems[:size]
|
49
|
+
xml.root.xmlelems["wise"].should == xml.root.xmlelems[:wise]
|
50
|
+
xml.root.xmlelems["a-z"].should == xml.root.xmlelems[:"a-z"]
|
41
51
|
end
|
42
52
|
|
43
53
|
it "creates accessors for elements" do
|
44
54
|
xml = parser.parse(input)
|
45
55
|
xml.root.xmlelems[:size].should eql(xml.root.size)
|
46
56
|
xml.root.xmlelems[:wise].should eql(xml.root.wise)
|
57
|
+
xml.root.xmlelems[:"a-z"].should eql(xml.root.send("a-z"))
|
47
58
|
end
|
48
59
|
end
|
49
60
|
|
@@ -60,6 +71,7 @@ describe XmlParsable, "accessors" do
|
|
60
71
|
include XmlParsable
|
61
72
|
element :size
|
62
73
|
element :wise, accessor: :wisdom
|
74
|
+
element "a-z", accessor: "atoz"
|
63
75
|
element :secret, accessor: false
|
64
76
|
end
|
65
77
|
|
@@ -70,9 +82,10 @@ describe XmlParsable, "accessors" do
|
|
70
82
|
|
71
83
|
it "stores elements in `xmlelems`" do
|
72
84
|
xml = parser.parse(input)
|
73
|
-
xml.root.xmlelems[:size] == "500"
|
74
|
-
xml.root.xmlelems[:wise] == "none"
|
75
|
-
xml.root.xmlelems[:
|
85
|
+
xml.root.xmlelems[:size].should == "500"
|
86
|
+
xml.root.xmlelems[:wise].should == "none"
|
87
|
+
xml.root.xmlelems[:"a-z"].should == "alphabutt"
|
88
|
+
xml.root.xmlelems[:secret].should == "Ovaltine"
|
76
89
|
end
|
77
90
|
|
78
91
|
it "doesn't create accessors for hidden elements" do
|
@@ -83,6 +96,7 @@ describe XmlParsable, "accessors" do
|
|
83
96
|
it "creates accessors for renamed elements" do
|
84
97
|
xml = parser.parse(input)
|
85
98
|
xml.root.xmlelems[:wise].should eql(xml.root.wisdom)
|
99
|
+
xml.root.xmlelems[:"a-z"].should eql(xml.root.atoz)
|
86
100
|
end
|
87
101
|
end
|
88
102
|
|