xmlparsable 1.4.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/xmlparsable.rb CHANGED
@@ -25,7 +25,12 @@ module XmlParsable
25
25
  element ||= arguments.shift if arguments.first.is_a?(Class)
26
26
  element ||= Elements::StringElement
27
27
 
28
- (@parsers ||= {})[name.to_s] = [element.parsable, *arguments]
28
+ # Look up the inheritance hierarchy to map element name to parser
29
+ parent = superclass < XmlParsable ?
30
+ superclass.instance_variable_get(:@parsers) : Hash.new
31
+
32
+ @parsers ||= Hash.new{|h,k| parent[k] }
33
+ @parsers[name.to_s] = [element.parsable, *arguments]
29
34
 
30
35
  define_method(name) { instance_variable_get("@#{name}") }
31
36
  end
@@ -35,7 +40,12 @@ module XmlParsable
35
40
  element ||= arguments.shift if arguments.first.is_a?(Class)
36
41
  element ||= Elements::StringElement
37
42
 
38
- (@parsers ||= {})[name.to_s.singularize] = [element.parsable, *arguments.push(:repeated)]
43
+ # Look up the inheritance hierarchy to map element name to parser
44
+ parent = superclass < XmlParsable ?
45
+ superclass.instance_variable_get(:@parsers) : Hash.new
46
+
47
+ @parsers ||= Hash.new{|h,k| parent[k] }
48
+ @parsers[name.to_s.singularize] = [element.parsable, *arguments.push(:repeated)]
39
49
 
40
50
  define_method(name) { instance_variable_get("@#{name.to_s.singularize}") }
41
51
  end
@@ -0,0 +1,155 @@
1
+ require "spec_helper"
2
+
3
+ describe XmlParsable::Elements::RecordElement, "inheritance" do
4
+
5
+ class BaseRecord
6
+ include XmlParsable
7
+
8
+ class Record
9
+ include XmlParsable
10
+ element :id, StringElement
11
+ element :size, NumericElement
12
+ end
13
+
14
+ element :record, Record
15
+ end
16
+
17
+ class FirstRecord
18
+ include XmlParsable
19
+
20
+ class Record < BaseRecord::Record
21
+ element :id, TimeElement
22
+ element :name, StringElement
23
+ end
24
+
25
+ element :record, Record
26
+ end
27
+
28
+ class SecondRecord
29
+ include XmlParsable
30
+
31
+ class Record < BaseRecord::Record
32
+ element :id, NumericElement
33
+ end
34
+
35
+ element :record, Record
36
+ end
37
+
38
+ class ThirdRecord < BaseRecord
39
+ include XmlParsable
40
+
41
+ class Record < BaseRecord::Record
42
+ element :parent, Record
43
+ end
44
+
45
+ element :record, Record
46
+ end
47
+
48
+ describe "parent" do
49
+ subject { BaseRecord::Record.new }
50
+ specify { should respond_to(:id) }
51
+ specify { should respond_to(:size) }
52
+ specify { should_not respond_to(:name) }
53
+ specify { should_not respond_to(:parent) }
54
+
55
+ it "parses attributes" do
56
+ data = BaseRecord.parse(<<-XML.strip)
57
+ <record>
58
+ <id>0xCAFE</id>
59
+ <size>0xFE</size>
60
+ </record>
61
+ XML
62
+
63
+ data.record.should be_a(BaseRecord::Record)
64
+ data.record.id.should == "0xCAFE"
65
+ data.record.size.should == 0xFE
66
+ end
67
+ end
68
+
69
+ describe "first" do
70
+ subject { FirstRecord::Record.new }
71
+ specify { should respond_to(:id) }
72
+ specify { should respond_to(:size) }
73
+ specify { should respond_to(:name) }
74
+ specify { should_not respond_to(:parent) }
75
+
76
+ it "parses attributes" do
77
+ data = FirstRecord.parse(<<-XML.strip)
78
+ <record>
79
+ <id>20120530T154500</id>
80
+ <size>0xCA7</size>
81
+ <name>Sabji</name>
82
+ </record>
83
+ XML
84
+
85
+ data.record.should be_a(FirstRecord::Record)
86
+ data.record.id.should == Time.local(2012, 5, 30, 15, 45)
87
+ data.record.size.should == 0xCA7
88
+ data.record.name.should == "Sabji"
89
+ end
90
+ end
91
+
92
+ describe "second" do
93
+ subject { SecondRecord::Record.new }
94
+ specify { should respond_to(:id) }
95
+ specify { should respond_to(:size) }
96
+ specify { should_not respond_to(:name) }
97
+ specify { should_not respond_to(:parent) }
98
+
99
+ it "parses attributes" do
100
+ data = SecondRecord.parse(<<-XML)
101
+ <record>
102
+ <id>0.314</id>
103
+ <size>3.014</size>
104
+ </record>
105
+ XML
106
+
107
+ data.record.should be_a(SecondRecord::Record)
108
+ data.record.id.should == 0.314
109
+ data.record.size.should == 3.014
110
+ end
111
+ end
112
+
113
+ describe "third" do
114
+ subject { ThirdRecord::Record.new }
115
+ specify { should respond_to(:id) }
116
+ specify { should respond_to(:size) }
117
+ specify { should_not respond_to(:name) }
118
+ specify { should respond_to(:parent) }
119
+
120
+ it "parses attributes" do
121
+ data = ThirdRecord.parse(<<-XML)
122
+ <record>
123
+ <id>X039-P99-THX1138-U</id>
124
+ <size>11235813</size>
125
+ <parent>
126
+ <id>X039-P99-THX1138</id>
127
+ <size>112358</size>
128
+ <parent>
129
+ <id>X039-P99</id>
130
+ <size>11235</size>
131
+ <parent>
132
+ <id>X039</id>
133
+ <size>1123</size>
134
+ </parent>
135
+ </parent>
136
+ </parent>
137
+ </record>
138
+ XML
139
+
140
+ data.record.should be_a(ThirdRecord::Record)
141
+ data.record.id.should == "X039-P99-THX1138-U"
142
+ data.record.size.should == 11235813
143
+
144
+ data.record.parent.id.should == "X039-P99-THX1138"
145
+ data.record.parent.size.should == 112358
146
+
147
+ data.record.parent.parent.id.should == "X039-P99"
148
+ data.record.parent.parent.size.should == 11235
149
+
150
+ data.record.parent.parent.parent.id.should == "X039"
151
+ data.record.parent.parent.parent.size.should == 1123
152
+ end
153
+ end
154
+
155
+ end
@@ -156,7 +156,7 @@ describe XmlParsable::Elements::RecordElement do
156
156
 
157
157
  it "parses rationals" do
158
158
  number = rand(1_000) + rand
159
- Record.parse("<number>#{number}</number>").number.should == number
159
+ Record.parse("<number>#{number}</number>").number.should == BigDecimal(number.to_s)
160
160
  end
161
161
 
162
162
  it "ignores comments" do
@@ -205,7 +205,7 @@ describe XmlParsable::Elements::RecordElement do
205
205
 
206
206
  it "parses number" do
207
207
  number = rand(1_000) + rand
208
- Record.parse("<record><number>#{number}</number></record>").record.number.should == number
208
+ Record.parse("<record><number>#{number}</number></record>").record.number.should == BigDecimal(number.to_s)
209
209
  end
210
210
 
211
211
  it "parses multiple elements" do
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.4.1
4
+ version: 1.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -47,6 +47,7 @@ files:
47
47
  - lib/xmlparsable/elements.rb
48
48
  - lib/xmlparsable/parser.rb
49
49
  - lib/xmlparsable.rb
50
+ - spec/examples/inheritance.example
50
51
  - spec/examples/record.example
51
52
  - spec/spec_helper.rb
52
53
  homepage: https://github.com/kputnam/xmlparsable
@@ -74,4 +75,5 @@ signing_key:
74
75
  specification_version: 3
75
76
  summary: Simple declarative XML parsing
76
77
  test_files:
78
+ - spec/examples/inheritance.example
77
79
  - spec/examples/record.example