multi_xml 0.5.4 → 0.5.5

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.
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,8 @@
1
+ 0.5.5
2
+ -----
3
+ * [Fix symbolize_keys function](https://github.com/sferik/multi_xml/commit/a4cae3aeb690999287cd30206399abaa5ce1ae81)
4
+ * [Fix Nokogiri parser for the same attr and inner element name](https://github.com/sferik/multi_xml/commit/a28ed86e2d7826b2edeed98552736b4c7ca52726)
5
+
1
6
  0.5.4
2
7
  -----
3
8
  * [Add option to not cast parsed values](https://github.com/sferik/multi_xml/commit/44fc05fbcfd60cc8b555b75212471fab29fa8cd0)
@@ -70,9 +70,9 @@ module MultiXml
70
70
  class << self
71
71
  # Get the current parser class.
72
72
  def parser
73
- return @@parser if defined?(@@parser)
73
+ return @parser if defined?(@parser)
74
74
  self.parser = self.default_parser
75
- @@parser
75
+ @parser
76
76
  end
77
77
 
78
78
  # The default parser based on what you currently
@@ -84,7 +84,7 @@ module MultiXml
84
84
  return :libxml if defined?(::LibXML)
85
85
  return :nokogiri if defined?(::Nokogiri)
86
86
 
87
- REQUIREMENT_MAP.each do |(library, parser)|
87
+ REQUIREMENT_MAP.each do |library, parser|
88
88
  begin
89
89
  require library
90
90
  return parser
@@ -105,9 +105,9 @@ module MultiXml
105
105
  case new_parser
106
106
  when String, Symbol
107
107
  require "multi_xml/parsers/#{new_parser.to_s.downcase}"
108
- @@parser = MultiXml::Parsers.const_get("#{new_parser.to_s.split('_').map{|s| s.capitalize}.join('')}")
108
+ @parser = MultiXml::Parsers.const_get("#{new_parser.to_s.split('_').map{|s| s.capitalize}.join('')}")
109
109
  when Class, Module
110
- @@parser = new_parser
110
+ @parser = new_parser
111
111
  else
112
112
  raise "Did not recognize your parser specification. Please specify either a symbol or a class."
113
113
  end
@@ -180,22 +180,16 @@ module MultiXml
180
180
  f
181
181
  end
182
182
 
183
- def symbolize_keys(hash)
184
- hash.inject({}) do |result, (key, value)|
185
- new_key = case key
186
- when String
187
- key.to_sym
188
- else
189
- key
190
- end
191
- new_value = case value
192
- when Hash
193
- symbolize_keys(value)
194
- else
195
- value
183
+ def symbolize_keys(params)
184
+ case params
185
+ when Hash
186
+ params.inject({}) do |result, (key, value)|
187
+ result.merge(key.to_sym => symbolize_keys(value))
196
188
  end
197
- result[new_key] = new_value
198
- result
189
+ when Array
190
+ params.map{|value| symbolize_keys(value)}
191
+ else
192
+ params
199
193
  end
200
194
  end
201
195
 
@@ -35,7 +35,15 @@ module MultiXml
35
35
  end
36
36
 
37
37
  # Handle attributes
38
- each_attr(node) {|a| node_hash[node_name(a)] = a.value }
38
+ each_attr(node) do |a|
39
+ key = node_name(a)
40
+
41
+ node_hash[key] = if v = node_hash[key]
42
+ [a.value, v]
43
+ else
44
+ a.value
45
+ end
46
+ end
39
47
 
40
48
  hash
41
49
  end
@@ -10,7 +10,7 @@ require 'ox' unless defined?(Ox)
10
10
  # pairs. Children are added as described by this rule.
11
11
  #
12
12
  # - Text and CDATE is stored in the parent element Hash with a key of
13
- # '__content__' and a value of the text itself.
13
+ # MultiXml::CONTENT_ROOT and a value of the text itself.
14
14
  #
15
15
  # - If a key already exists in the Hash then the value associated with the key
16
16
  # is converted to an Array with the old and new value in it.
@@ -52,11 +52,11 @@ module MultiXml
52
52
  end
53
53
 
54
54
  def text(value)
55
- append('__content__', value)
55
+ append(MultiXml::CONTENT_ROOT, value)
56
56
  end
57
57
 
58
58
  def cdata(value)
59
- append('__content__', value)
59
+ append(MultiXml::CONTENT_ROOT, value)
60
60
  end
61
61
 
62
62
  def start_element(name)
@@ -1,3 +1,3 @@
1
1
  module MultiXml
2
- VERSION = "0.5.4" unless defined?(MultiXML::VERSION)
2
+ VERSION = "0.5.5" unless defined?(MultiXML::VERSION)
3
3
  end
@@ -11,17 +11,14 @@ describe "MultiXml" do
11
11
  end
12
12
 
13
13
  it "defaults to the best available gem" do
14
- pending
15
- expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Rexml')
16
- require 'nokogiri'
17
- expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Nokogiri')
18
- require 'libxml'
19
- expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Libxml')
14
+ # Clear cache variable already set by previous tests
15
+ MultiXml.send(:remove_instance_variable, :@parser)
16
+ expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Ox')
20
17
  end
21
18
 
22
19
  it "is settable via a symbol" do
23
- MultiXml.parser = :nokogiri
24
- expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Nokogiri')
20
+ MultiXml.parser = :rexml
21
+ expect(MultiXml.parser.name).to eq('MultiXml::Parsers::Rexml')
25
22
  end
26
23
 
27
24
  it "is settable via a class" do
@@ -58,6 +58,16 @@ shared_examples_for "a parser" do |parser|
58
58
  end
59
59
  end
60
60
 
61
+ context "element with the same inner element and attribute name" do
62
+ before do
63
+ @xml = "<user name='John'><name>Smith</name></user>"
64
+ end
65
+
66
+ it "returns nams as Array" do
67
+ expect(MultiXml.parse(@xml)['user']['name']).to eq ['John', 'Smith']
68
+ end
69
+ end
70
+
61
71
  context "with content" do
62
72
  before do
63
73
  @xml = '<user>Erik Michaels-Ober</user>'
@@ -122,63 +132,11 @@ shared_examples_for "a parser" do |parser|
122
132
 
123
133
  context "with :symbolize_keys => true" do
124
134
  before do
125
- @xml = '<user><name>Erik Michaels-Ober</name></user>'
135
+ @xml = '<users><user name="Erik Michaels-Ober"/><user><name>Wynn Netherland</name></user></users>'
126
136
  end
127
137
 
128
138
  it "symbolizes keys" do
129
- expect(MultiXml.parse(@xml, :symbolize_keys => true)).to eq({:user => {:name => "Erik Michaels-Ober"}})
130
- end
131
- end
132
-
133
- context "when value is true" do
134
- before do
135
- pending
136
- @xml = '<tag>true</tag>'
137
- end
138
-
139
- it "returns true" do
140
- expect(MultiXml.parse(@xml)['tag']).to be_true
141
- end
142
- end
143
-
144
- context "when value is false" do
145
- before do
146
- pending
147
- @xml = '<tag>false</tag>'
148
- end
149
-
150
- it "returns false" do
151
- expect(MultiXml.parse(@xml)['tag']).to be_false
152
- end
153
- end
154
-
155
- context "when key is id" do
156
- before do
157
- pending
158
- @xml = '<id>1</id>'
159
- end
160
-
161
- it "returns a Fixnum" do
162
- expect(MultiXml.parse(@xml)['id']).to be_a(Fixnum)
163
- end
164
-
165
- it "returns the correct number" do
166
- expect(MultiXml.parse(@xml)['id']).to eq(1)
167
- end
168
- end
169
-
170
- context "when key contains _id" do
171
- before do
172
- pending
173
- @xml = '<tag_id>1</tag_id>'
174
- end
175
-
176
- it "returns a Fixnum" do
177
- expect(MultiXml.parse(@xml)['tag_id']).to be_a(Fixnum)
178
- end
179
-
180
- it "returns the correct number" do
181
- expect(MultiXml.parse(@xml)['tag_id']).to eq(1)
139
+ expect(MultiXml.parse(@xml, :symbolize_keys => true)).to eq({:users => {:user => [{:name => "Erik Michaels-Ober"}, {:name => "Wynn Netherland"}]}})
182
140
  end
183
141
  end
184
142
 
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.5.4
4
+ version: 0.5.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -36,7 +36,7 @@ cert_chain:
36
36
  U0xxV3ZRUnNCbHlwSGZoczZKSnVMbHlaUEdoVTNSL3YKU2YzbFZLcEJDV2dS
37
37
  cEdUdnk0NVhWcEIrNTl5MzNQSm1FdVExUFRFT1l2UXlhbzlVS01BQWFBTi83
38
38
  cVdRdGpsMApobHc9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
39
- date: 2013-06-04 00:00:00.000000000 Z
39
+ date: 2013-08-06 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
metadata.gz.sig CHANGED
Binary file