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 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
- 1. Declare XML grammar
8
-
9
- require "xmlparsable"
10
-
11
- # Needed only for using the "collection" declaration or
12
- # implement your own String#pluralize, String#singularize
13
- require "active_support/core_ext"
14
-
15
- class AddressBook
16
- include XmlParsable
17
-
18
- class Contact
19
- include XmlParsable
20
- element :givenname
21
- element :familyname
22
- element :phone
23
- element :email
24
- element :spouse, Contact
25
- collection :children, Contact
26
- element :birthdate, DateElement
27
- element :updated, TimeElement
28
- end
29
-
30
- collection :contact, Contact
31
- end
32
-
33
- 2. Parse input XML from string, IO, or File
34
-
35
- book = AddressBook.parse(<<-XML)
36
- <contacts>
37
- <contact>
38
- <familyname>Kim</familyname>
39
- <givenname>Jong-il</givenname>
40
- <birthdate>1942-02-16</birthdate>
41
- <updated>2010-01-01T12:45:30</updated>
42
- <children>
43
- <child><givenname>Sul-song</givenname></child>
44
- <child><givenname>Jong-chul</givenname></child>
45
- <child><givenname>Jong-un</givenname></child>
46
- <child>
47
- <givenname>Jong-nam</givenname>
48
- <spouse>
49
- <givenname>Jong-hui</givenname>
50
- <familyname>Shin</familyname>
51
- </spouse>
52
- </child>
53
- </children>
54
- </contact>
55
-
56
- <contact>
57
- <givenname>Vladimir</givenname>
58
- <familyname>Putin</familyname>
59
- <email>vlad@gmail.com</email>
60
- <spouse>
61
- <givenname>Lyudmila</givenname>
62
- <familyname>Putina</familyname>
63
- </spouse>
64
- </contact>
65
- </contacts>
66
- XML
67
-
68
- 3. Iterate parsed data
69
-
70
- kim, vlad = book.contacts
71
- kim.givenname #=> "Kim"
72
- kim.birthdate #=> #<Date: 4860813/2,0,2299161>
73
- kim.updated #=> Fri Jan 01 12:45:30 2010
74
-
75
- sulsong, jongchul, jongun, jongnam = kim.children
76
- jongnam.spouse.givenname #=> "Jong-hui"
77
- jongnam.spouse.familyname #=> "Shin"
78
-
79
- vlad.email #=> "vlad@gmail.com"
80
- vlad.spouse #=> #<AddressBook::Contact @givenname="Lyudmila" @familyname="Putina">
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] = "500"
40
- xml.root.xmlelems[:wise] = "none"
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[:secret] == "Ovaltine"
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmlparsable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: