xml_resource 2.0.3 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/xml_resource/base.rb +4 -4
- data/lib/xml_resource/version.rb +1 -1
- data/test/cases/xml_resource_test.rb +7 -1
- data/test/data/inflection.xml +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6d08265845c5ecc6dcfda46dd66776e0d23c489
|
4
|
+
data.tar.gz: 242e0b821fd361a46d38a864d654b1f4324446e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8cea6a257f708fcd7d6a89f51c2487c06420a2c4d807a9920962a24774d260e4ac5f1538011e2ca19845c2f66c92955f75e924364783595f004a3f3126af56d
|
7
|
+
data.tar.gz: 549b9daeb15309b922fe9240ec3a54c319ea2e52e8cd8691c706ef85b2fc36819ed1e94571cc06fedc9fc925ccd75d0af9a4a6b688d8c7adc2196fc35f7db09c
|
data/lib/xml_resource/base.rb
CHANGED
@@ -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
|
data/lib/xml_resource/version.rb
CHANGED
@@ -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
|
data/test/data/inflection.xml
CHANGED