xmlparsable 1.5.1 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,11 +5,11 @@ module XmlParsable
5
5
  autoload :Parser, "xmlparsable/parser"
6
6
  autoload :Elements, "xmlparsable/elements"
7
7
 
8
- def self.parser(input)
8
+ def self.parser(input, *extra)
9
9
  case input
10
- when String then LibXML::XML::SaxParser.string(input)
11
- when File then LibXML::XML::SaxParser.file(input)
12
- when IO then LibXML::XML::SaxParser.io(input)
10
+ when String then LibXML::XML::SaxParser.string(input, *extra)
11
+ when File then LibXML::XML::SaxParser.file(input, *extra)
12
+ when IO then LibXML::XML::SaxParser.io(input, *extra)
13
13
  else raise ArgumentError, "argument must be a String, File, or IO"
14
14
  end
15
15
  end
@@ -74,8 +74,8 @@ module XmlParsable
74
74
  end
75
75
  end
76
76
 
77
- def parse(input)
78
- root = XmlParsable::Elements::RecordElement.new(nil, nil, nil, new, @parsers)
77
+ def parse(input, *extras)
78
+ root = XmlParsable::Elements::RecordElement.new(nil, nil, nil, new, @parsers, Hash.new)
79
79
  stack = XmlParsable::Parser.new(root)
80
80
  parser = XmlParsable.parser(input)
81
81
 
@@ -2,19 +2,20 @@ module XmlParsable
2
2
  module Elements
3
3
 
4
4
  class ArrayElement < AbstractElement
5
- attr_reader :name
6
5
 
7
6
  class Array < ::Array
8
7
  attr_reader :xmlattrs
9
8
  end
10
9
 
10
+ attr_reader :name
11
+
11
12
  def initialize(name, attributes, parent, arguments)
12
- @name, @parent, @elements = name, parent, Array.new
13
+ @name, @attributes, @parent, @elements =
14
+ name, attributes, parent, Array.new
13
15
 
14
16
  @item_name = arguments[:item_name].to_s
15
17
  @item_type = arguments[:class]
16
18
  @item_args = arguments[:item]
17
- @elements.instance_variable_set(:@xmlattrs, attributes)
18
19
  end
19
20
 
20
21
  def open(name, attributes)
@@ -28,6 +29,7 @@ module XmlParsable
28
29
  end
29
30
 
30
31
  def finalize
32
+ @elements.instance_variable_set(:@xmlattrs, @attributes)
31
33
  @parent.close(self, @elements)
32
34
  end
33
35
  end
@@ -6,10 +6,16 @@ module XmlParsable
6
6
  # Parses content into a ruby Date value
7
7
  #
8
8
  class DateElement < AbstractElement
9
+
10
+ class Date < ::Date
11
+ attr_reader :xmlattrs
12
+ end
13
+
9
14
  attr_reader :name
10
15
 
11
- def initialize(name, attributes, parent, *arguments)
12
- @name, @parent, @string = name, parent, ""
16
+ def initialize(name, attributes, parent, arguments)
17
+ @name, @attributes, @parent, @string =
18
+ name, attributes, parent, ""
13
19
  end
14
20
 
15
21
  def read(text)
@@ -18,7 +24,10 @@ module XmlParsable
18
24
 
19
25
  def finalize
20
26
  stripped = @string.strip
21
- @parent.close(self, stripped == "" ? nil : Date.parse(stripped))
27
+ date = stripped == "" ? nil : Date.parse(stripped)
28
+ date.instance_variable_set(:@xmlattrs, @attributes)
29
+
30
+ @parent.close(self, date)
22
31
  end
23
32
  end
24
33
  end
@@ -6,8 +6,9 @@ module XmlParsable
6
6
  class NumericElement < AbstractElement
7
7
  attr_reader :name
8
8
 
9
- def initialize(name, attributes, parent, *arguments)
10
- @name, @parent, @string = name, parent, ""
9
+ def initialize(name, attributes, parent, arguments)
10
+ @name, @attributes, @parent, @string =
11
+ name, attributes, parent, ""
11
12
  end
12
13
 
13
14
  def read(text)
@@ -22,6 +23,8 @@ module XmlParsable
22
23
  Integer(stripped)
23
24
  end
24
25
 
26
+ # TODO: Subclass BigDecimal and Integer to add an accessor method
27
+ numeric.instance_variable_set(:@xmlattrs, @attributes)
25
28
  @parent.close(self, numeric)
26
29
  end
27
30
  end
@@ -3,14 +3,9 @@ module XmlParsable
3
3
  class RecordElement < AbstractElement
4
4
  attr_reader :name, :attributes, :parent, :target
5
5
 
6
- def initialize(name, attributes, parent, target, parsers, *arguments)
7
- @name = name
8
- @attributes = attributes
9
- @parent = parent
10
- @target = target
11
- @parsers = parsers
12
- @elements = Hash.new
13
- @target.instance_variable_set(:@xmlattrs, @attributes)
6
+ def initialize(name, attributes, parent, target, parsers, arguments)
7
+ @name, @attributes, @parent, @target, @parsers, @elements =
8
+ name, attributes, parent, target, parsers, Hash.new
14
9
  end
15
10
 
16
11
  def open(name, attributes)
@@ -32,6 +27,7 @@ module XmlParsable
32
27
  end
33
28
 
34
29
  def finalize
30
+ @target.instance_variable_set(:@xmlattrs, @attributes)
35
31
  @target.instance_variable_set(:@xmlelems, @elements)
36
32
  @parent.close(self, @target) if @parent
37
33
  end
@@ -41,8 +37,8 @@ module XmlParsable
41
37
  @base, @parsers = base, parsers
42
38
  end
43
39
 
44
- def new(name, attributes, parent, *arguments)
45
- RecordElement.new(name, attributes, parent, @base.new, @parsers)
40
+ def new(name, attributes, parent, arguments)
41
+ RecordElement.new(name, attributes, parent, @base.new, @parsers, arguments)
46
42
  end
47
43
  end
48
44
  end
@@ -5,10 +5,16 @@ module XmlParsable
5
5
  # elements so "abc <!-- foo --> def <b>whatever</b>" is read as "abc def"
6
6
  #
7
7
  class StringElement < AbstractElement
8
+
9
+ class String < ::String
10
+ attr_reader :xmlattrs
11
+ end
12
+
8
13
  attr_reader :name
9
14
 
10
- def initialize(name, attributes, parent, *arguments)
11
- @name, @parent, @string = name, parent, ""
15
+ def initialize(name, attributes, parent, arguments)
16
+ @name, @attributes, @parent, @string =
17
+ name, attributes, parent, String.new
12
18
  end
13
19
 
14
20
  def read(text)
@@ -16,6 +22,7 @@ module XmlParsable
16
22
  end
17
23
 
18
24
  def finalize
25
+ @string.instance_variable_set(:@xmlattrs, @attributes)
19
26
  @parent.close(self, @string)
20
27
  end
21
28
  end
@@ -5,14 +5,20 @@ module XmlParsable
5
5
  # elements so "abc def <b>what<!-- foo -->ever</b>" is read as "abc def whatever"
6
6
  #
7
7
  class TextElement < XmlElement::Node
8
+
9
+ class String < ::String
10
+ attr_reader :xmlattrs
11
+ end
12
+
8
13
  attr_reader :name, :attributes, :parent
9
14
 
10
- def initialize(name, attributes, parent, *arguments)
11
- @name, @attributes, @parent, @text = name, attributes, parent, ""
15
+ def initialize(name, attributes, parent, arguments)
16
+ @name, @attributes, @parent, @arguments, @text =
17
+ name, attributes, parent, arguments, String.new
12
18
  end
13
19
 
14
20
  def open(name, attributes)
15
- TextElement.new(name, attributes, self)
21
+ TextElement.new(name, attributes, self, @arguments)
16
22
  end
17
23
 
18
24
  def read(text)
@@ -27,6 +33,7 @@ module XmlParsable
27
33
  end
28
34
 
29
35
  def finalize
36
+ @text.instance_variable_set(:@xmlattrs, @attributes)
30
37
  @parent.close(self, @text)
31
38
  end
32
39
  end
@@ -6,10 +6,16 @@ module XmlParsable
6
6
  # Parses content into a ruby Time value
7
7
  #
8
8
  class TimeElement < AbstractElement
9
+
10
+ class Time < ::Time
11
+ attr_reader :xmlattrs
12
+ end
13
+
9
14
  attr_reader :name
10
15
 
11
- def initialize(name, attributes, parent, *arguments)
12
- @name, @parent, @string = name, parent, ""
16
+ def initialize(name, attributes, parent, arguments)
17
+ @name, @attributes, @parent, @string =
18
+ name, attributes, parent, ""
13
19
  end
14
20
 
15
21
  def read(text)
@@ -18,7 +24,10 @@ module XmlParsable
18
24
 
19
25
  def finalize
20
26
  stripped = @string.strip
21
- @parent.close(self, stripped == "" ? nil : Time.parse(stripped))
27
+ time = stripped == "" ? nil : Time.parse(stripped)
28
+ time.instance_variable_set(:@xmlattrs, @attributes)
29
+
30
+ @parent.close(self, time)
22
31
  end
23
32
  end
24
33
  end
@@ -11,14 +11,15 @@ module XmlParsable
11
11
  end
12
12
 
13
13
  class Node < AbstractElement
14
- attr_reader :name, :attributes, :parent, :children
14
+ attr_reader :name, :attributes, :parent, :children, :xmlattr
15
15
 
16
- def initialize(name, attributes, parent, *arguments)
17
- @parent, @name, @attributes, @children = parent, name, attributes, Array.new
16
+ def initialize(name, attributes, parent, arguments)
17
+ @name, @attributes, @parent, @arguments, @children =
18
+ name, attributes, parent, arguments, Array.new
18
19
  end
19
20
 
20
21
  def open(name, attributes)
21
- Node.new(name, attributes, self)
22
+ Node.new(name, attributes, self, @arguments)
22
23
  end
23
24
 
24
25
  def read(text)
@@ -72,6 +72,13 @@ describe XmlParsable::Elements::ArrayElement do
72
72
  parser.parse(input).list.xmlattrs.should == expected
73
73
  end
74
74
 
75
+ it "parses item attributes" do
76
+ input = '<list><item thang="yarn"/><item milk="yawn"/></list>'
77
+ result = parser.parse(input)
78
+ result.list[0].xmlattrs.should == Hash["thang" => "yarn"]
79
+ result.list[1].xmlattrs.should == Hash["milk" => "yawn"]
80
+ end
81
+
75
82
  it "parses non-empty list into an Array" do
76
83
  input = "<list><item>a</item><item>b</item></list>"
77
84
  expected = ["a", "b"]
@@ -10,6 +10,20 @@ describe XmlParsable::Elements::DateElement do
10
10
  end
11
11
  end
12
12
 
13
+ describe "parses attributes" do
14
+ let(:expected) { Hash["thang" => "yarn", "milk" => "yawn"] }
15
+
16
+ it "on non-empty elements" do
17
+ input = "<date thang='yarn' milk='yawn'>#{Date.today}</date>"
18
+ parser.parse(input).date.xmlattrs.should == expected
19
+ end
20
+
21
+ pending "on empty elements" do
22
+ input = '<date thang="yarn" milk="yawn"/>'
23
+ parser.parse(input).date.xmlattrs.should == expected
24
+ end
25
+ end
26
+
13
27
  it "parses date into a ruby Date" do
14
28
  expected = Date.today
15
29
  input = "<date>#{expected}</date>"
@@ -10,6 +10,20 @@ describe XmlParsable::Elements::StringElement do
10
10
  end
11
11
  end
12
12
 
13
+ describe "parses attributes" do
14
+ let(:expected) { Hash["thang" => "yarn", "milk" => "yawn"] }
15
+
16
+ pending "for non-empty elements" do
17
+ input = '<number thang="yarn" milk="yawn">0</number>'
18
+ parser.parse(input).number.xmlattrs.should == expected
19
+ end
20
+
21
+ pending "for empty elements" do
22
+ input = '<number thang="yarn" milk="yawn"/>'
23
+ parser.parse(input).number.xmlattrs.should == expected
24
+ end
25
+ end
26
+
13
27
  it "parses integers" do
14
28
  expected = rand(1_000)
15
29
  input = "<number>#{expected}</number>"
@@ -6,6 +6,8 @@ describe XmlParsable::Elements::RecordElement do
6
6
  Class.new do
7
7
  include XmlParsable
8
8
 
9
+ attr_reader :xmlattrs
10
+
9
11
  element :date, XmlParsable::Elements::DateElement
10
12
  element :string, XmlParsable::Elements::StringElement
11
13
  element :text, XmlParsable::Elements::TextElement
@@ -61,6 +63,20 @@ describe XmlParsable::Elements::RecordElement do
61
63
  DOC
62
64
  end
63
65
 
66
+ describe "parses attributes" do
67
+ let(:expected) { Hash["thang" => "yarn", "milk" => "yawn"] }
68
+
69
+ it "for non-empty elements" do
70
+ input = '<record thang="yarn" milk="yawn"><number>1</number></record>'
71
+ parser.parse(input).record.xmlattrs.should == expected
72
+ end
73
+
74
+ it "for empty elements" do
75
+ input = '<record thang="yarn" milk="yawn"/>'
76
+ parser.parse(input).record.xmlattrs.should == expected
77
+ end
78
+ end
79
+
64
80
  it "parses recursive records" do
65
81
  xml = parser.parse(<<-XML)
66
82
  <record>
@@ -10,6 +10,20 @@ describe XmlParsable::Elements::StringElement do
10
10
  end
11
11
  end
12
12
 
13
+ describe "parses attributes" do
14
+ let(:expected) { Hash["thang" => "yarn", "milk" => "yawn"] }
15
+
16
+ it "for empty elements" do
17
+ input = '<string thang="yarn" milk="yawn"/>'
18
+ parser.parse(input).string.xmlattrs.should == expected
19
+ end
20
+
21
+ it "for non-empty elements" do
22
+ input = '<string thang="yarn" milk="yawn">wormever</string>'
23
+ parser.parse(input).string.xmlattrs.should == expected
24
+ end
25
+ end
26
+
13
27
  it "parses empty string" do
14
28
  expected = ""
15
29
  input = "<string></string>"
@@ -10,6 +10,20 @@ describe XmlParsable::Elements::StringElement do
10
10
  end
11
11
  end
12
12
 
13
+ describe "parses attributes" do
14
+ let(:expected) { Hash["thang" => "yarn", "milk" => "yawn"] }
15
+
16
+ it "for empty elements" do
17
+ input = '<text thang="yarn" milk="yawn"/>'
18
+ parser.parse(input).text.xmlattrs.should == expected
19
+ end
20
+
21
+ it "for non-empty elements" do
22
+ input = '<text thang="yarn" milk="yawn">wormever</text>'
23
+ parser.parse(input).text.xmlattrs.should == expected
24
+ end
25
+ end
26
+
13
27
  it "parses text" do
14
28
  expected = "lo and behold"
15
29
  input = "<text>lo and behold</text>"
@@ -10,6 +10,20 @@ describe XmlParsable::Elements::StringElement do
10
10
  end
11
11
  end
12
12
 
13
+ describe "parses attributes" do
14
+ let(:expected) { Hash["thang" => "yarn", "milk" => "yawn"] }
15
+
16
+ pending "for empty elements" do
17
+ input = '<time thang="yarn" milk="yawn"/>'
18
+ parser.parse(input).time.xmlattrs.should == expected
19
+ end
20
+
21
+ specify "for non-empty elements" do
22
+ input = "<time thang='yarn' milk='yawn'>#{Time.now}</time>"
23
+ parser.parse(input).time.xmlattrs.should == expected
24
+ end
25
+ end
26
+
13
27
  it "parses time" do
14
28
  expected = Time.now
15
29
  input = "<time>#{expected}</time>"
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.1
4
+ version: 1.5.2
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-05-30 00:00:00.000000000 Z
12
+ date: 2012-06-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: libxml-ruby