xmlparsable 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -78,3 +78,48 @@ relies on LibXML::XML::SaxParser.
78
78
 
79
79
  vlad.email #=> "vlad@gmail.com"
80
80
  vlad.spouse #=> #<AddressBook::Contact @givenname="Lyudmila" @familyname="Putina">
81
+
82
+ ## Data Types
83
+
84
+ ### DateElement
85
+
86
+ Parses content into a Ruby `Date` object, using `Date.parse`.
87
+
88
+ ### ListElement
89
+
90
+ Parses children nodes into an `Array` using some other `AbstractElement`
91
+ child class.
92
+
93
+ ### NumericElement
94
+
95
+ Parses content into a Ruby `Integer` or `BigDecimal`, depending on whether
96
+ the input contains a decimal point or not. Parses using `Integer(..)` and
97
+ `BigDecimal(..)` constructor methods.
98
+
99
+ ### RecordElement
100
+
101
+ Parses children nodes according to `element` and `collection` declarations
102
+ on an `XmlParsable` class.
103
+
104
+ ### StringElement
105
+
106
+ Parses content into a Ruby `String` object.
107
+
108
+ ### TextElement
109
+
110
+ Concatenates all subordinate text nodes into a single `String`. For example,
111
+ `"abc def <b>what<!-- ever --></b>"` is read as `"abc def what"`.
112
+
113
+ ### TimeElement
114
+
115
+ Parses content into a Ruby `Time` object, using `Time.parse`.
116
+
117
+ ### XmlElement
118
+
119
+ Parses children elements into an `Array` whose elements are one of these three
120
+ classes. These really should be the same node types that `LibXML` uses, but
121
+ that's not the case for now.
122
+
123
+ * `XmlParsable::Elements::XmlElement::Node`
124
+ * `XmlParsable::Elements::XmlElement::Text`
125
+ * `XmlParsable::Elements::XmlElement::Comment`
@@ -18,7 +18,7 @@ module XmlParsable
18
18
 
19
19
  def finalize
20
20
  stripped = @string.strip
21
- @parent.closed(self, Date.parse(stripped)) if stripped != ""
21
+ @parent.close(self, stripped == "" ? nil : Date.parse(stripped))
22
22
  end
23
23
  end
24
24
  end
@@ -18,12 +18,12 @@ module XmlParsable
18
18
  end
19
19
  end
20
20
 
21
- def closed(element, value)
21
+ def close(element, value)
22
22
  @elements << value
23
23
  end
24
24
 
25
25
  def finalize
26
- @parent.closed(self, @elements)
26
+ @parent.close(self, @elements)
27
27
  end
28
28
  end
29
29
 
@@ -0,0 +1,30 @@
1
+ module XmlParsable
2
+ module Elements
3
+ #
4
+ # Parses content into a ruby Numeric value.
5
+ #
6
+ class NumericElement < AbstractElement
7
+ attr_reader :name
8
+
9
+ def initialize(name, attributes, parent, *arguments)
10
+ @name, @parent, @string = name, parent, ""
11
+ end
12
+
13
+ def read(text)
14
+ @string << text
15
+ end
16
+
17
+ def finalize
18
+ stripped = @string.strip
19
+ numeric = if stripped =~ /[e.]/i and stripped !~ /x/i
20
+ BigDecimal(stripped)
21
+ elsif stripped != ""
22
+ Integer(stripped)
23
+ end
24
+
25
+ @parent.close(self, numeric)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -19,7 +19,7 @@ module XmlParsable
19
19
  end
20
20
  end
21
21
 
22
- def closed(element, value)
22
+ def close(element, value)
23
23
  parser, *arguments = @parsers[element.name]
24
24
  if arguments.include?(:repeated)
25
25
  (@elements[element.name] ||= []) << value
@@ -30,7 +30,7 @@ module XmlParsable
30
30
 
31
31
  def finalize
32
32
  @elements.each{|name, value| @target.instance_variable_set("@#{name}", value) }
33
- @parent.closed(self, @target) if @parent
33
+ @parent.close(self, @target) if @parent
34
34
  end
35
35
 
36
36
  class Proxy
@@ -16,7 +16,7 @@ module XmlParsable
16
16
  end
17
17
 
18
18
  def finalize
19
- @parent.closed(self, @string)
19
+ @parent.close(self, @string)
20
20
  end
21
21
  end
22
22
  end
@@ -22,12 +22,12 @@ module XmlParsable
22
22
  def comment(text)
23
23
  end
24
24
 
25
- def closed(element, value)
25
+ def close(element, value)
26
26
  @text << value
27
27
  end
28
28
 
29
29
  def finalize
30
- @parent.closed(self, @text)
30
+ @parent.close(self, @text)
31
31
  end
32
32
  end
33
33
  end
@@ -18,7 +18,7 @@ module XmlParsable
18
18
 
19
19
  def finalize
20
20
  stripped = @string.strip
21
- @parent.closed(self, Time.parse(stripped)) if stripped != ""
21
+ @parent.close(self, stripped == "" ? nil : Time.parse(stripped))
22
22
  end
23
23
  end
24
24
  end
@@ -29,12 +29,12 @@ module XmlParsable
29
29
  @children << Comment.new(text, self)
30
30
  end
31
31
 
32
- def closed(element, value)
32
+ def close(element, value)
33
33
  @children << value
34
34
  end
35
35
 
36
36
  def finalize
37
- @parent.closed(self, self)
37
+ @parent.close(self, self)
38
38
  end
39
39
 
40
40
  def to_s(buffer = "")
@@ -3,6 +3,7 @@ module XmlParsable
3
3
  autoload :AbstractElement, "xmlparsable/elements/abstract"
4
4
  autoload :DateElement, "xmlparsable/elements/date"
5
5
  autoload :ListElement, "xmlparsable/elements/list"
6
+ autoload :NumericElement, "xmlparsable/elements/numeric"
6
7
  autoload :RecordElement, "xmlparsable/elements/record"
7
8
  autoload :StringElement, "xmlparsable/elements/string"
8
9
  autoload :TextElement, "xmlparsable/elements/text"
@@ -6,11 +6,12 @@ describe XmlParsable::Elements::RecordElement do
6
6
  include XmlParsable
7
7
 
8
8
  element :date, DateElement
9
- element :record, self
10
9
  element :string, StringElement
11
10
  element :text, TextElement
12
11
  element :time, TimeElement
13
12
  element :xml, XmlElement
13
+ element :number, NumericElement
14
+ element :record, self
14
15
 
15
16
  collection :kids, self
16
17
  collection :list, :item
@@ -147,6 +148,31 @@ describe XmlParsable::Elements::RecordElement do
147
148
  end
148
149
  end
149
150
 
151
+ describe "number" do
152
+ it "parses integers" do
153
+ number = rand(1_000)
154
+ Record.parse("<number>#{number}</number>").number.should == number
155
+ end
156
+
157
+ it "parses rationals" do
158
+ number = rand(1_000) + rand
159
+ Record.parse("<number>#{number}</number>").number.should == number
160
+ end
161
+
162
+ it "ignores comments" do
163
+ Record.parse("<time><!-- 65432 --></time>").number.should be_nil
164
+ end
165
+
166
+ it "ignores children" do
167
+ Record.parse("<time><b>#{rand}</b></time>").number.should be_nil
168
+ end
169
+
170
+ it "ignores whitespace" do
171
+ number = rand(1_000)
172
+ Record.parse("<number> #{number} </number>").number.should == number
173
+ end
174
+ end
175
+
150
176
  describe "record" do
151
177
  it "parses date" do
152
178
  x = Record.parse("<record><date>#{Date.today}</date></record>")
@@ -177,6 +203,11 @@ describe XmlParsable::Elements::RecordElement do
177
203
  Record.parse("<record><time>#{time}</time></record>").record.time.to_i.should == time.to_i
178
204
  end
179
205
 
206
+ it "parses number" do
207
+ number = rand(1_000) + rand
208
+ Record.parse("<record><number>#{number}</number></record>").record.number.should == number
209
+ end
210
+
180
211
  it "parses multiple elements" do
181
212
  time = Time.now
182
213
  x = Record.parse <<-XML
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.3.0
4
+ version: 1.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-14 00:00:00.000000000 Z
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: libxml-ruby
@@ -38,6 +38,7 @@ files:
38
38
  - lib/xmlparsable/elements/abstract.rb
39
39
  - lib/xmlparsable/elements/date.rb
40
40
  - lib/xmlparsable/elements/list.rb
41
+ - lib/xmlparsable/elements/numeric.rb
41
42
  - lib/xmlparsable/elements/record.rb
42
43
  - lib/xmlparsable/elements/string.rb
43
44
  - lib/xmlparsable/elements/text.rb