multi_xml 0.4.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of multi_xml might be problematic. Click here for more details.
- data/.travis.yml +5 -1
- data/README.md +31 -28
- data/Rakefile +0 -2
- data/lib/multi_xml.rb +13 -3
- data/lib/multi_xml/version.rb +1 -1
- data/spec/helper.rb +3 -1
- data/spec/multi_xml_spec.rb +9 -0
- data/spec/parser_shared_example.rb +97 -0
- metadata +3 -3
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -4,15 +4,15 @@ A generic swappable back-end for XML parsing
|
|
4
4
|
[travis]: http://travis-ci.org/sferik/multi_xml
|
5
5
|
[gemnasium]: https://gemnasium.com/sferik/multi_xml
|
6
6
|
|
7
|
-
##
|
7
|
+
## Installation
|
8
8
|
gem install multi_xml
|
9
9
|
|
10
|
-
##
|
10
|
+
## Documentation
|
11
11
|
[http://rdoc.info/gems/multi_xml][documentation]
|
12
12
|
|
13
13
|
[documentation]: http://rdoc.info/gems/multi_xml
|
14
14
|
|
15
|
-
##
|
15
|
+
## Usage Examples
|
16
16
|
Lots of Ruby libraries utilize XML parsing in some form, and everyone has their
|
17
17
|
favorite XML library. In order to best support multiple XML parsers and
|
18
18
|
libraries, `multi_xml` is a general-purpose swappable XML backend library. You
|
@@ -43,7 +43,7 @@ supported parsers already loaded, it will utilize them before attempting to
|
|
43
43
|
load any. When loading, libraries are ordered by speed: first Ox, then LibXML,
|
44
44
|
then Nokogiri, and finally REXML.
|
45
45
|
|
46
|
-
##
|
46
|
+
## Contributing
|
47
47
|
In the spirit of [free software][free-sw] , **everyone** is encouraged to help
|
48
48
|
improve this project.
|
49
49
|
|
@@ -64,33 +64,36 @@ Here are some ways *you* can contribute:
|
|
64
64
|
|
65
65
|
[issues]: https://github.com/sferik/multi_xml/issues
|
66
66
|
|
67
|
-
##
|
67
|
+
## Submitting an Issue
|
68
68
|
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
69
69
|
submitting a bug report or feature request, check to make sure it hasn't
|
70
|
-
already been submitted.
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
bug report should include a pull request with failing specs.
|
70
|
+
already been submitted. When submitting a bug report, please include a [Gist][]
|
71
|
+
that includes a stack trace and any details that may be necessary to reproduce
|
72
|
+
the bug, including your gem version, Ruby version, and operating system.
|
73
|
+
Ideally, a bug report should include a pull request with failing specs.
|
75
74
|
|
76
75
|
[gist]: https://gist.github.com/
|
77
76
|
|
78
|
-
##
|
79
|
-
1. Fork the
|
80
|
-
2. Create a topic branch.
|
81
|
-
3.
|
82
|
-
4.
|
83
|
-
5.
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
77
|
+
## Submitting a Pull Request
|
78
|
+
1. [Fork the repository.][fork]
|
79
|
+
2. [Create a topic branch.][branch]
|
80
|
+
3. Add specs for your unimplemented feature or bug fix.
|
81
|
+
4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
|
82
|
+
5. Implement your feature or bug fix.
|
83
|
+
6. Run `bundle exec rake spec`. If your specs fail, return to step 5.
|
84
|
+
7. Run `open coverage/index.html`. If your changes are not completely covered
|
85
|
+
by your tests, return to step 3.
|
86
|
+
8. Add documentation for your feature or bug fix.
|
87
|
+
9. Run `bundle exec rake yard`. If your changes are not 100% documented, go
|
88
|
+
back to step 8.
|
89
|
+
10. Add, commit, and push your changes.
|
90
|
+
11. [Submit a pull request.][pr]
|
91
|
+
|
92
|
+
[fork]: http://help.github.com/fork-a-repo/
|
93
|
+
[branch]: http://learn.github.com/p/branching.html
|
94
|
+
[pr]: http://help.github.com/send-pull-requests/
|
95
|
+
|
96
|
+
## Supported Ruby Versions
|
94
97
|
This library aims to support and is [tested against][travis] the following Ruby
|
95
98
|
implementations:
|
96
99
|
|
@@ -115,12 +118,12 @@ implementation, you will be personally responsible for providing patches in a
|
|
115
118
|
timely fashion. If critical issues for a particular implementation exist at the
|
116
119
|
time of a major release, support for that Ruby version may be dropped.
|
117
120
|
|
118
|
-
##
|
121
|
+
## Inspiration
|
119
122
|
MultiXML was inspired by [MultiJSON][].
|
120
123
|
|
121
124
|
[multijson]: https://github.com/intridea/multi_json/
|
122
125
|
|
123
|
-
##
|
126
|
+
## Copyright
|
124
127
|
Copyright (c) 2010 Erik Michaels-Ober. See [LICENSE][] for details.
|
125
128
|
|
126
129
|
[license]: https://github.com/sferik/multi_xml/blob/master/LICENSE.md
|
data/Rakefile
CHANGED
data/lib/multi_xml.rb
CHANGED
@@ -57,7 +57,7 @@ module MultiXml
|
|
57
57
|
class << self
|
58
58
|
# Get the current parser class.
|
59
59
|
def parser
|
60
|
-
return @@parser
|
60
|
+
return @@parser unless @@parser.nil?
|
61
61
|
self.parser = self.default_parser
|
62
62
|
@@parser
|
63
63
|
end
|
@@ -226,9 +226,19 @@ module MultiXml
|
|
226
226
|
elsif value.has_key?(CONTENT_ROOT)
|
227
227
|
content = value[CONTENT_ROOT]
|
228
228
|
if block = PARSING[value['type']]
|
229
|
-
block.arity == 1
|
229
|
+
if block.arity == 1
|
230
|
+
value.delete('type') if PARSING[value['type']]
|
231
|
+
if value.keys.size > 1
|
232
|
+
value[CONTENT_ROOT] = block.call(content)
|
233
|
+
value
|
234
|
+
else
|
235
|
+
block.call(content)
|
236
|
+
end
|
237
|
+
else
|
238
|
+
block.call(content, value)
|
239
|
+
end
|
230
240
|
else
|
231
|
-
content
|
241
|
+
value.keys.size > 1 ? value : content
|
232
242
|
end
|
233
243
|
elsif value['type'] == 'string' && value['nil'] != 'true'
|
234
244
|
''
|
data/lib/multi_xml/version.rb
CHANGED
data/spec/helper.rb
CHANGED
data/spec/multi_xml_spec.rb
CHANGED
@@ -5,6 +5,15 @@ class MockDecoder; end
|
|
5
5
|
|
6
6
|
describe "MultiXml" do
|
7
7
|
context "Parsers" do
|
8
|
+
before do
|
9
|
+
MultiXml.send :class_variable_set, :@@parser, nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should pick a default parser" do
|
13
|
+
MultiXml.parser.should be_kind_of(Module)
|
14
|
+
MultiXml.parser.should respond_to(:parse)
|
15
|
+
end
|
16
|
+
|
8
17
|
it "should default to the best available gem" do
|
9
18
|
pending
|
10
19
|
MultiXml.parser.name.should be == 'MultiXml::Parsers::Rexml'
|
@@ -534,6 +534,103 @@ shared_examples_for "a parser" do |parser|
|
|
534
534
|
end
|
535
535
|
end
|
536
536
|
|
537
|
+
context "with attribute tags on content nodes" do
|
538
|
+
context "non 'type' attributes" do
|
539
|
+
before do
|
540
|
+
@xml = <<-XML
|
541
|
+
<options>
|
542
|
+
<value currency='USD'>123</value>
|
543
|
+
<value number='percent'>0.123</value>
|
544
|
+
</options>
|
545
|
+
XML
|
546
|
+
@parsed_xml = MultiXml.parse(@xml)
|
547
|
+
end
|
548
|
+
|
549
|
+
it "should add the attributes to the value hash" do
|
550
|
+
@parsed_xml['options']['value'][0]['__content__'].should == '123'
|
551
|
+
@parsed_xml['options']['value'][0]['currency'].should == 'USD'
|
552
|
+
@parsed_xml['options']['value'][1]['__content__'].should == '0.123'
|
553
|
+
@parsed_xml['options']['value'][1]['number'].should == 'percent'
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
context "unrecognized type attributes" do
|
558
|
+
before do
|
559
|
+
@xml = <<-XML
|
560
|
+
<options>
|
561
|
+
<value type='USD'>123</value>
|
562
|
+
<value type='percent'>0.123</value>
|
563
|
+
<value currency='USD'>123</value>
|
564
|
+
</options>
|
565
|
+
XML
|
566
|
+
@parsed_xml = MultiXml.parse(@xml)
|
567
|
+
end
|
568
|
+
|
569
|
+
it "should add the attributes to the value hash passing through the type" do
|
570
|
+
@parsed_xml['options']['value'][0]['__content__'].should == '123'
|
571
|
+
@parsed_xml['options']['value'][0]['type'].should == 'USD'
|
572
|
+
@parsed_xml['options']['value'][1]['__content__'].should == '0.123'
|
573
|
+
@parsed_xml['options']['value'][1]['type'].should == 'percent'
|
574
|
+
@parsed_xml['options']['value'][2]['__content__'].should == '123'
|
575
|
+
@parsed_xml['options']['value'][2]['currency'].should == 'USD'
|
576
|
+
end
|
577
|
+
end
|
578
|
+
|
579
|
+
context "mixing attributes and non-attributes content nodes type attributes" do
|
580
|
+
before do
|
581
|
+
@xml = <<-XML
|
582
|
+
<options>
|
583
|
+
<value type='USD'>123</value>
|
584
|
+
<value type='percent'>0.123</value>
|
585
|
+
<value>123</value>
|
586
|
+
</options>
|
587
|
+
XML
|
588
|
+
@parsed_xml = MultiXml.parse(@xml)
|
589
|
+
end
|
590
|
+
|
591
|
+
it "should add the attributes to the value hash passing through the type" do
|
592
|
+
@parsed_xml['options']['value'][0]['__content__'].should == '123'
|
593
|
+
@parsed_xml['options']['value'][0]['type'].should == 'USD'
|
594
|
+
@parsed_xml['options']['value'][1]['__content__'].should == '0.123'
|
595
|
+
@parsed_xml['options']['value'][1]['type'].should == 'percent'
|
596
|
+
@parsed_xml['options']['value'][2].should == '123'
|
597
|
+
end
|
598
|
+
end
|
599
|
+
|
600
|
+
context "mixing recognized type attribute and non-type attributes on content nodes" do
|
601
|
+
before do
|
602
|
+
@xml = <<-XML
|
603
|
+
<options>
|
604
|
+
<value number='USD' type='integer'>123</value>
|
605
|
+
</options>
|
606
|
+
XML
|
607
|
+
@parsed_xml = MultiXml.parse(@xml)
|
608
|
+
end
|
609
|
+
|
610
|
+
it "should add the the non-type attribute and remove the recognized type attribute and do the typecast" do
|
611
|
+
@parsed_xml['options']['value']['__content__'].should == 123
|
612
|
+
@parsed_xml['options']['value']['number'].should == 'USD'
|
613
|
+
end
|
614
|
+
end
|
615
|
+
|
616
|
+
context "mixing unrecognized type attribute and non-type attributes on content nodes" do
|
617
|
+
before do
|
618
|
+
@xml = <<-XML
|
619
|
+
<options>
|
620
|
+
<value number='USD' type='currency'>123</value>
|
621
|
+
</options>
|
622
|
+
XML
|
623
|
+
@parsed_xml = MultiXml.parse(@xml)
|
624
|
+
end
|
625
|
+
|
626
|
+
it "should add the the non-type attributes and type attribute to the value hash" do
|
627
|
+
@parsed_xml['options']['value']['__content__'].should == '123'
|
628
|
+
@parsed_xml['options']['value']['number'].should == 'USD'
|
629
|
+
@parsed_xml['options']['value']['type'].should == 'currency'
|
630
|
+
end
|
631
|
+
end
|
632
|
+
end
|
633
|
+
|
537
634
|
context "with newlines and whitespace" do
|
538
635
|
before do
|
539
636
|
@xml = <<-XML
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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-
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: maruku
|
@@ -139,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
139
|
version: '0'
|
140
140
|
requirements: []
|
141
141
|
rubyforge_project:
|
142
|
-
rubygems_version: 1.8.
|
142
|
+
rubygems_version: 1.8.24
|
143
143
|
signing_key:
|
144
144
|
specification_version: 3
|
145
145
|
summary: A generic swappable back-end for XML parsing
|