xml_resource 2.0.3 → 2.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9b84a85add7d0dace9d14d734bae2cbbebbeebf
4
- data.tar.gz: c5ff1e51e574d32546b621b4272717b90eceb8c7
3
+ metadata.gz: e6d08265845c5ecc6dcfda46dd66776e0d23c489
4
+ data.tar.gz: 242e0b821fd361a46d38a864d654b1f4324446e6
5
5
  SHA512:
6
- metadata.gz: 3850b3775a4eae2ffbfc51a89192d8adac3ee1b74175243c4ca017375b79bdfd572a0c283c20e6d4b6e43263ae19f31e5929a0f674c66f53ba23152310bea28d
7
- data.tar.gz: f60b7497c7c28d07421723e1dae9dbc2bee1ca66fdc61b9622d78497489a0b912bbb510712b2b64dde4b6b35b29fabf0e945e3f86b9e82c409e150d67a72b742
6
+ metadata.gz: c8cea6a257f708fcd7d6a89f51c2487c06420a2c4d807a9920962a24774d260e4ac5f1538011e2ca19845c2f66c92955f75e924364783595f004a3f3126af56d
7
+ data.tar.gz: 549b9daeb15309b922fe9240ec3a54c319ea2e52e8cd8691c706ef85b2fc36819ed1e94571cc06fedc9fc925ccd75d0af9a4a6b688d8c7adc2196fc35f7db09c
@@ -13,7 +13,7 @@ module XmlResource
13
13
 
14
14
  class << self
15
15
 
16
- def from_xml(xml_or_string)
16
+ def from_xml(xml_or_string, default_attrs = {})
17
17
  xml_or_string and xml = parse(xml_or_string) or return
18
18
  attrs = {}
19
19
  self.attributes.each do |name, options|
@@ -23,7 +23,7 @@ module XmlResource
23
23
  if !value.nil? && type = attribute_type(name)
24
24
  value = cast_to(type, value)
25
25
  end
26
- attrs[name] = value
26
+ attrs[name] = value.nil? ? default_attrs[name] : value
27
27
  end
28
28
  end
29
29
  self.objects.each do |name, options|
@@ -39,8 +39,8 @@ module XmlResource
39
39
  new attrs
40
40
  end
41
41
 
42
- def collection_from_xml(xml_or_string)
43
- parse(xml_or_string).search(root).map { |element| from_xml(element) }.compact
42
+ def collection_from_xml(xml_or_string, default_attrs = {})
43
+ parse(xml_or_string).search(root).map { |element| from_xml(element, default_attrs) }.compact
44
44
  end
45
45
 
46
46
  def basename
@@ -1,3 +1,3 @@
1
1
  module XmlResource
2
- VERSION = '2.0.3'
2
+ VERSION = '2.0.4'
3
3
  end
@@ -58,7 +58,7 @@ class XmlResourceTest < ActiveSupport::TestCase
58
58
  camels = Camel.collection_from_xml(xml)
59
59
  dromedaries = Dromedary.collection_from_xml(xml)
60
60
  assert_equal [2, 2], camels.map(&:humps)
61
- assert_equal [1], dromedaries.map(&:humps)
61
+ assert_equal [1, nil], dromedaries.map(&:humps)
62
62
 
63
63
  shark = Shark.collection_from_xml(xml).first
64
64
  assert_equal '2 m', shark.shark_size
@@ -68,6 +68,12 @@ class XmlResourceTest < ActiveSupport::TestCase
68
68
  assert_equal 'grey', elephant.color
69
69
  end
70
70
 
71
+ test 'Default attributes' do
72
+ xml = load_xml(:inflection)
73
+ dromedaries = Dromedary.collection_from_xml(xml, humps: 3)
74
+ assert_equal [1, 3], dromedaries.map(&:humps)
75
+ end
76
+
71
77
  private
72
78
 
73
79
  def orders
@@ -16,6 +16,9 @@
16
16
  <Dromedary>
17
17
  <Humps>1</Humps>
18
18
  </Dromedary>
19
+ <Dromedary>
20
+ <Color>gold</Color>
21
+ </Dromedary>
19
22
  <ELEPHANT>
20
23
  <COLOR>grey</COLOR>
21
24
  </ELEPHANT>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Grosser