multi_xml 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of multi_xml might be problematic. Click here for more details.

data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- multi_xml (0.1.1)
4
+ multi_xml (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/lib/multi_xml.rb CHANGED
@@ -161,22 +161,12 @@ module MultiXml
161
161
  end
162
162
  end
163
163
 
164
- def wrap(object)
165
- if object.nil?
166
- []
167
- elsif object.respond_to?(:to_ary)
168
- object.to_ary
169
- else
170
- [object]
171
- end
172
- end
173
-
174
164
  def typecast_xml_value(value)
175
165
  case value
176
166
  when Hash
177
167
  if value['type'] == 'array'
178
- _, entries = wrap(value.detect{|key, value| key != 'type'})
179
- if entries.blank? || (c = value[CONTENT_ROOT] && c.blank?)
168
+ _, entries = Array.wrap(value.detect{|key, value| key != 'type'})
169
+ if entries.blank? || (value.is_a?(Hash) && c = value[CONTENT_ROOT] && c.blank?)
180
170
  []
181
171
  else
182
172
  case entries
@@ -57,3 +57,52 @@ class String #:nodoc:
57
57
  strip.empty?
58
58
  end
59
59
  end
60
+
61
+ class Array
62
+ # Wraps its argument in an array unless it is already an array (or array-like).
63
+ #
64
+ # Specifically:
65
+ #
66
+ # * If the argument is +nil+ an empty list is returned.
67
+ # * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
68
+ # * Otherwise, returns an array with the argument as its single element.
69
+ #
70
+ # Array.wrap(nil) # => []
71
+ # Array.wrap([1, 2, 3]) # => [1, 2, 3]
72
+ # Array.wrap(0) # => [0]
73
+ #
74
+ # This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
75
+ #
76
+ # * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
77
+ # moves on to try +to_a+ if the returned value is +nil+, but <tt>Arraw.wrap</tt> returns
78
+ # such a +nil+ right away.
79
+ # * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
80
+ # raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
81
+ # * It does not call +to_a+ on the argument, though special-cases +nil+ to return an empty array.
82
+ #
83
+ # The last point is particularly worth comparing for some enumerables:
84
+ #
85
+ # Array(:foo => :bar) # => [[:foo, :bar]]
86
+ # Array.wrap(:foo => :bar) # => [{:foo => :bar}]
87
+ #
88
+ # Array("foo\nbar") # => ["foo\n", "bar"], in Ruby 1.8
89
+ # Array.wrap("foo\nbar") # => ["foo\nbar"]
90
+ #
91
+ # There's also a related idiom that uses the splat operator:
92
+ #
93
+ # [*object]
94
+ #
95
+ # which returns <tt>[nil]</tt> for +nil+, and calls to <tt>Array(object)</tt> otherwise.
96
+ #
97
+ # Thus, in this case the behavior is different for +nil+, and the differences with
98
+ # <tt>Kernel#Array</tt> explained above apply to the rest of +object+s.
99
+ def self.wrap(object)
100
+ if object.nil?
101
+ []
102
+ elsif object.respond_to?(:to_ary)
103
+ object.to_ary
104
+ else
105
+ [object]
106
+ end
107
+ end
108
+ end
@@ -1,3 +1,3 @@
1
1
  module MultiXml
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -55,7 +55,7 @@ describe "MultiXml" do
55
55
  end
56
56
  end
57
57
 
58
- context "a single-node document" do
58
+ context "an XML document" do
59
59
 
60
60
  before do
61
61
  @xml = '<user/>'
@@ -70,8 +70,8 @@ describe "MultiXml" do
70
70
  @xml = '<user><![CDATA[Erik Michaels-Ober]]></user>'
71
71
  end
72
72
 
73
- it "should parse correctly" do
74
- MultiXml.parse(@xml).should == {"user" => "Erik Michaels-Ober"}
73
+ it "should return the correct CDATA" do
74
+ MultiXml.parse(@xml)['user'].should == "Erik Michaels-Ober"
75
75
  end
76
76
  end
77
77
 
@@ -80,8 +80,8 @@ describe "MultiXml" do
80
80
  @xml = '<user>Erik Michaels-Ober</user>'
81
81
  end
82
82
 
83
- it "should parse correctly" do
84
- MultiXml.parse(@xml).should == {"user" => "Erik Michaels-Ober"}
83
+ it "should return the correct content" do
84
+ MultiXml.parse(@xml)['user'].should == "Erik Michaels-Ober"
85
85
  end
86
86
  end
87
87
 
@@ -90,8 +90,8 @@ describe "MultiXml" do
90
90
  @xml = '<user name="Erik Michaels-Ober"/>'
91
91
  end
92
92
 
93
- it "should parse correctly" do
94
- MultiXml.parse(@xml).should == {"user" => {"name" => "Erik Michaels-Ober"}}
93
+ it "should return the correct attribute" do
94
+ MultiXml.parse(@xml)['user']['name'].should == "Erik Michaels-Ober"
95
95
  end
96
96
  end
97
97
 
@@ -100,14 +100,15 @@ describe "MultiXml" do
100
100
  @xml = '<user name="Erik Michaels-Ober" screen_name="sferik"/>'
101
101
  end
102
102
 
103
- it "should parse correctly" do
104
- MultiXml.parse(@xml).should == {"user" => {"name" => "Erik Michaels-Ober", "screen_name" => "sferik"}}
103
+ it "should return the correct attributes" do
104
+ MultiXml.parse(@xml)['user']['name'].should == "Erik Michaels-Ober"
105
+ MultiXml.parse(@xml)['user']['screen_name'].should == "sferik"
105
106
  end
106
107
  end
107
108
 
108
109
  context "with :symbolize_keys => true" do
109
110
  before do
110
- @xml = '<user name="Erik Michaels-Ober"/>'
111
+ @xml = '<user><name>Erik Michaels-Ober</name></user>'
111
112
  end
112
113
 
113
114
  it "should symbolize keys" do
@@ -118,7 +119,7 @@ describe "MultiXml" do
118
119
  context "with an attribute type=\"boolean\"" do
119
120
  %w(true false).each do |boolean|
120
121
  context "when #{boolean}" do
121
- it "should be #{boolean}" do
122
+ it "should return #{boolean}" do
122
123
  xml = "<tag type=\"boolean\">#{boolean}</tag>"
123
124
  MultiXml.parse(xml)['tag'].should instance_eval("be_#{boolean}")
124
125
  end
@@ -130,7 +131,7 @@ describe "MultiXml" do
130
131
  @xml = '<tag type="boolean">1</tag>'
131
132
  end
132
133
 
133
- it "should be true" do
134
+ it "should return true" do
134
135
  MultiXml.parse(@xml)['tag'].should be_true
135
136
  end
136
137
  end
@@ -140,7 +141,7 @@ describe "MultiXml" do
140
141
  @xml = '<tag type="boolean">0</tag>'
141
142
  end
142
143
 
143
- it "should be false" do
144
+ it "should return false" do
144
145
  MultiXml.parse(@xml)['tag'].should be_false
145
146
  end
146
147
  end
@@ -152,11 +153,15 @@ describe "MultiXml" do
152
153
  @xml = '<tag type="integer">1</tag>'
153
154
  end
154
155
 
155
- it "should be a Fixnum" do
156
+ it "should return a Fixnum" do
156
157
  MultiXml.parse(@xml)['tag'].should be_a(Fixnum)
157
158
  end
158
159
 
159
- it "should be the correct number" do
160
+ it "should return a positive number" do
161
+ MultiXml.parse(@xml)['tag'].should > 0
162
+ end
163
+
164
+ it "should return the correct number" do
160
165
  MultiXml.parse(@xml)['tag'].should == 1
161
166
  end
162
167
  end
@@ -166,11 +171,15 @@ describe "MultiXml" do
166
171
  @xml = '<tag type="integer">-1</tag>'
167
172
  end
168
173
 
169
- it "should be a Fixnum" do
174
+ it "should return a Fixnum" do
170
175
  MultiXml.parse(@xml)['tag'].should be_a(Fixnum)
171
176
  end
172
177
 
173
- it "should be the correct number" do
178
+ it "should return a negative number" do
179
+ MultiXml.parse(@xml)['tag'].should < 0
180
+ end
181
+
182
+ it "should return the correct number" do
174
183
  MultiXml.parse(@xml)['tag'].should == -1
175
184
  end
176
185
  end
@@ -181,11 +190,11 @@ describe "MultiXml" do
181
190
  @xml = '<tag type="string"></tag>'
182
191
  end
183
192
 
184
- it "should be a String" do
193
+ it "should return a String" do
185
194
  MultiXml.parse(@xml)['tag'].should be_a(String)
186
195
  end
187
196
 
188
- it "should be the correct string" do
197
+ it "should return the correct string" do
189
198
  MultiXml.parse(@xml)['tag'].should == ""
190
199
  end
191
200
  end
@@ -195,11 +204,11 @@ describe "MultiXml" do
195
204
  @xml = '<tag type="date">1970-01-01</tag>'
196
205
  end
197
206
 
198
- it "should be a Date" do
207
+ it "should return a Date" do
199
208
  MultiXml.parse(@xml)['tag'].should be_a(Date)
200
209
  end
201
210
 
202
- it "should be the correct date" do
211
+ it "should return the correct date" do
203
212
  MultiXml.parse(@xml)['tag'].should == Date.parse('1970-01-01')
204
213
  end
205
214
  end
@@ -209,11 +218,11 @@ describe "MultiXml" do
209
218
  @xml = '<tag type="datetime">1970-01-01 00:00</tag>'
210
219
  end
211
220
 
212
- it "should be a Time" do
221
+ it "should return a Time" do
213
222
  MultiXml.parse(@xml)['tag'].should be_a(Time)
214
223
  end
215
224
 
216
- it "should be the correct time" do
225
+ it "should return the correct time" do
217
226
  MultiXml.parse(@xml)['tag'].should == Time.parse('1970-01-01 00:00')
218
227
  end
219
228
  end
@@ -223,11 +232,11 @@ describe "MultiXml" do
223
232
  @xml = '<tag type="datetime">1970-01-01 00:00</tag>'
224
233
  end
225
234
 
226
- it "should be a Time" do
235
+ it "should return a Time" do
227
236
  MultiXml.parse(@xml)['tag'].should be_a(Time)
228
237
  end
229
238
 
230
- it "should be the correct time" do
239
+ it "should return the correct time" do
231
240
  MultiXml.parse(@xml)['tag'].should == Time.parse('1970-01-01 00:00')
232
241
  end
233
242
  end
@@ -237,11 +246,11 @@ describe "MultiXml" do
237
246
  @xml = '<tag type="double">3.14159265358979</tag>'
238
247
  end
239
248
 
240
- it "should be a Float" do
249
+ it "should return a Float" do
241
250
  MultiXml.parse(@xml)['tag'].should be_a(Float)
242
251
  end
243
252
 
244
- it "should be the correct number" do
253
+ it "should return the correct number" do
245
254
  MultiXml.parse(@xml)['tag'].should == 3.14159265358979
246
255
  end
247
256
  end
@@ -251,11 +260,11 @@ describe "MultiXml" do
251
260
  @xml = '<tag type="decimal">3.14159265358979323846264338327950288419716939937510</tag>'
252
261
  end
253
262
 
254
- it "should be a BigDecimal" do
263
+ it "should return a BigDecimal" do
255
264
  MultiXml.parse(@xml)['tag'].should be_a(BigDecimal)
256
265
  end
257
266
 
258
- it "should be the correct number" do
267
+ it "should return the correct number" do
259
268
  MultiXml.parse(@xml)['tag'].should == 3.14159265358979323846264338327950288419716939937510
260
269
  end
261
270
  end
@@ -265,22 +274,26 @@ describe "MultiXml" do
265
274
  @xml = '<tag type="base64Binary">aW1hZ2UucG5n</tag>'
266
275
  end
267
276
 
268
- it "should be a String" do
277
+ it "should return a String" do
269
278
  MultiXml.parse(@xml)['tag'].should be_a(String)
270
279
  end
271
280
 
272
- it "should be the correct string" do
281
+ it "should return the correct string" do
273
282
  MultiXml.parse(@xml)['tag'].should == "image.png"
274
283
  end
275
284
  end
276
285
 
277
286
  context "with an attribute type=\"yaml\"" do
278
287
  before do
279
- @xml = "<tag type=\"yaml\">--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</tag>"
288
+ @xml = "<tag type=\"yaml\">--- \n1: should return an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</tag>"
280
289
  end
281
290
 
282
- it "should parse correctly" do
283
- MultiXml.parse(@xml)['tag'].should == {:message => "Have a nice day", 1 => "should be an integer", "array" => [{"should-have-dashes" => true, "should_have_underscores" => true}]}
291
+ it "should return a Hash" do
292
+ MultiXml.parse(@xml)['tag'].should be_a(Hash)
293
+ end
294
+
295
+ it "should return the correctly parsed YAML" do
296
+ MultiXml.parse(@xml)['tag'].should == {:message => "Have a nice day", 1 => "should return an integer", "array" => [{"should-have-dashes" => true, "should_have_underscores" => true}]}
284
297
  end
285
298
  end
286
299
 
@@ -289,7 +302,7 @@ describe "MultiXml" do
289
302
  @xml = '<tag type="file" name="data.txt" content_type="text/plain">ZGF0YQ==</tag>'
290
303
  end
291
304
 
292
- it "should be a StringIO" do
305
+ it "should return a StringIO" do
293
306
  MultiXml.parse(@xml)['tag'].should be_a(StringIO)
294
307
  end
295
308
 
@@ -310,7 +323,7 @@ describe "MultiXml" do
310
323
  @xml = '<tag type="file">ZGF0YQ==</tag>'
311
324
  end
312
325
 
313
- it "should be a StringIO" do
326
+ it "should return a StringIO" do
314
327
  MultiXml.parse(@xml)['tag'].should be_a(StringIO)
315
328
  end
316
329
 
@@ -328,13 +341,27 @@ describe "MultiXml" do
328
341
  end
329
342
  end
330
343
 
331
- %w(integer boolean date datetime yaml).each do |type|
344
+ context "with an attribute type=\"array\"" do
345
+ before do
346
+ @xml = '<users type="array"><user>Erik Michaels-Ober</user><user>Wynn Netherland</user></users>'
347
+ end
348
+
349
+ it "should return an Array" do
350
+ MultiXml.parse(@xml)['users'].should be_a(Array)
351
+ end
352
+
353
+ it "should return the correct array" do
354
+ MultiXml.parse(@xml)['users'].should == ["Erik Michaels-Ober", "Wynn Netherland"]
355
+ end
356
+ end
357
+
358
+ %w(integer boolean date datetime yaml file).each do |type|
332
359
  context "with an empty attribute type=\"#{type}\"" do
333
360
  before do
334
361
  @xml = "<tag type=\"#{type}\"/>"
335
362
  end
336
363
 
337
- it "should be nil" do
364
+ it "should return nil" do
338
365
  MultiXml.parse(@xml)['tag'].should be_nil
339
366
  end
340
367
  end
@@ -342,20 +369,20 @@ describe "MultiXml" do
342
369
 
343
370
  context "with an empty attribute type=\"array\"" do
344
371
  before do
345
- @xml = '<users type="array"/>'
372
+ @xml = '<tag type="array"/>'
346
373
  end
347
374
 
348
- it "should be an empty Array" do
349
- MultiXml.parse(@xml)['users'].should == []
375
+ it "should return an empty Array" do
376
+ MultiXml.parse(@xml)['tag'].should == []
350
377
  end
351
378
 
352
379
  context "with whitespace" do
353
380
  before do
354
- @xml = '<users type="array"> </users>'
381
+ @xml = '<tag type="array"> </tag>'
355
382
  end
356
383
 
357
- it "should be an empty Array" do
358
- MultiXml.parse(@xml)['users'].should == []
384
+ it "should return an empty Array" do
385
+ MultiXml.parse(@xml)['tag'].should == []
359
386
  end
360
387
  end
361
388
  end
@@ -372,7 +399,7 @@ describe "MultiXml" do
372
399
  end
373
400
 
374
401
  context "in content" do
375
- it "should unescape XML entities" do
402
+ it "should return unescaped XML entities" do
376
403
  @xml_entities.each do |key, value|
377
404
  xml = "<tag>#{value}</tag>"
378
405
  MultiXml.parse(xml)['tag'].should == key
@@ -381,7 +408,7 @@ describe "MultiXml" do
381
408
  end
382
409
 
383
410
  context "in attribute" do
384
- it "should unescape XML entities" do
411
+ it "should return unescaped XML entities" do
385
412
  @xml_entities.each do |key, value|
386
413
  xml = "<tag attribute=\"#{value}\"/>"
387
414
  MultiXml.parse(xml)['tag']['attribute'].should == key
@@ -395,7 +422,7 @@ describe "MultiXml" do
395
422
  @xml = '<tag-1/>'
396
423
  end
397
424
 
398
- it "should undasherize tag" do
425
+ it "should return undasherize tag" do
399
426
  MultiXml.parse(@xml).keys.should include('tag_1')
400
427
  end
401
428
  end
@@ -405,30 +432,20 @@ describe "MultiXml" do
405
432
  @xml = '<tag attribute-1="1"></tag>'
406
433
  end
407
434
 
408
- it "should undasherize attribute" do
435
+ it "should return undasherize attribute" do
409
436
  MultiXml.parse(@xml)['tag'].keys.should include('attribute_1')
410
437
  end
411
438
  end
412
- end
413
-
414
- context "a document" do
415
- context "with :symbolize_keys => true" do
416
- before do
417
- @xml = '<user><name>Erik Michaels-Ober</name></user>'
418
- end
419
-
420
- it "should symbolize keys" do
421
- MultiXml.parse(@xml, :symbolize_keys => true).should == {:user => {:name => "Erik Michaels-Ober"}}
422
- end
423
- end
424
439
 
425
440
  context "with children" do
426
- before do
427
- @xml = '<root><user name="Erik Michaels-Ober"/></root>'
428
- end
441
+ context "with attributes" do
442
+ before do
443
+ @xml = '<users><user name="Erik Michaels-Ober"/></users>'
444
+ end
429
445
 
430
- it "should parse correctly" do
431
- MultiXml.parse(@xml).should == {"root" => {"user" => {"name"=>"Erik Michaels-Ober"}}}
446
+ it "should return the correct attributes" do
447
+ MultiXml.parse(@xml)['users']['user']['name'].should == "Erik Michaels-Ober"
448
+ end
432
449
  end
433
450
 
434
451
  context "with text" do
@@ -436,8 +453,8 @@ describe "MultiXml" do
436
453
  @xml = '<user><name>Erik Michaels-Ober</name></user>'
437
454
  end
438
455
 
439
- it "should parse correctly" do
440
- MultiXml.parse(@xml).should == {"user" => {"name" => "Erik Michaels-Ober"}}
456
+ it "should return the correct text" do
457
+ MultiXml.parse(@xml)['user']['name'].should == "Erik Michaels-Ober"
441
458
  end
442
459
  end
443
460
 
@@ -461,35 +478,34 @@ describe "MultiXml" do
461
478
  end
462
479
 
463
480
  it "should parse correctly" do
464
- MultiXml.parse(@xml).should == {"user"=>{"name"=>"Erik Michaels-Ober"}}
481
+ MultiXml.parse(@xml).should == {"user" => {"name" => "Erik Michaels-Ober"}}
465
482
  end
466
483
  end
467
484
 
468
485
  # Babies having babies
469
486
  context "with children" do
470
487
  before do
471
- @xml = '<root><user name="Erik Michaels-Ober"><status text="Hello"/></user></root>'
488
+ @xml = '<users><user name="Erik Michaels-Ober"><status text="Hello"/></user></users>'
472
489
  end
473
490
 
474
491
  it "should parse correctly" do
475
- MultiXml.parse(@xml).should == {"root" => {"user" => {"name" => "Erik Michaels-Ober", "status" => {"text" => "Hello"}}}}
492
+ MultiXml.parse(@xml).should == {"users" => {"user" => {"name" => "Erik Michaels-Ober", "status" => {"text" => "Hello"}}}}
476
493
  end
477
494
  end
478
495
  end
479
496
 
480
497
  context "with sibling children" do
481
498
  before do
482
- @xml = '<root><users>Erik Michaels-Ober</users><users>Wynn Netherland</users></root>'
499
+ @xml = '<users><user>Erik Michaels-Ober</user><user>Wynn Netherland</user></users>'
483
500
  end
484
501
 
485
- it "should parse correctly" do
486
- MultiXml.parse(@xml).should == {"root" => {"users" => ["Erik Michaels-Ober", "Wynn Netherland"]}}
502
+ it "should return an Array" do
503
+ MultiXml.parse(@xml)['users']['user'].should be_a(Array)
487
504
  end
488
505
 
489
- it "should be Array" do
490
- MultiXml.parse(@xml)['root']['users'].should be_a(Array)
506
+ it "should parse correctly" do
507
+ MultiXml.parse(@xml).should == {"users" => {"user" => ["Erik Michaels-Ober", "Wynn Netherland"]}}
491
508
  end
492
-
493
509
  end
494
510
  end
495
511
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_xml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Erik Michaels-Ober