xml-mapping_extensions 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/README.md +8 -0
- data/lib/xml/mapping_extensions.rb +2 -2
- data/lib/xml/mapping_extensions/version.rb +1 -1
- data/spec/unit/xml/mapping_extensions/mapping_extensions_spec.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8846f750f369604e10e815fa58a11019c8dd8b3d
|
4
|
+
data.tar.gz: 3b05e612551a8d455108af3d7f351f5b6566b2e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89e7edf4af982cb52e5222f385d136911e4ca48e5719bff5c6576c691e151f152dc2ec6f91a6d6f997b1f1a6ae146677c208ae6c29f5375592498912e505bc07
|
7
|
+
data.tar.gz: 44b088113a9a0bc8f1335e1cc447977ca94ac9e516823c5bf436cb78060e7a2e73336a8b6208e6611ffe2ed080d2bca55eb4fea84a5334edaef1049c77033365
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -68,6 +68,14 @@ elem = MyXMLClass.load_from_xml(my_xml_element)
|
|
68
68
|
end
|
69
69
|
```
|
70
70
|
|
71
|
+
Both `write_xml` and `parse_xml` accept an `options` hash, to be passed on to `save_to_xml` or `load_from_xml`,
|
72
|
+
respectively:
|
73
|
+
|
74
|
+
```
|
75
|
+
elem = MyXMLClass.parse(my_xml_string, { mapping: :alternate })
|
76
|
+
new_xml_string = elem.write_xml({ mapping: :alternate })
|
77
|
+
```
|
78
|
+
|
71
79
|
## Custom nodes
|
72
80
|
|
73
81
|
To create a custom node type, require `xml/mapping_extensions` and extend one of
|
@@ -53,7 +53,7 @@ module XML
|
|
53
53
|
# `xml`, which can be a string, REXML document, or REXML element
|
54
54
|
# @param xml [String, REXML::Document, REXML::Element]
|
55
55
|
# @return [Object] an instance of this class.
|
56
|
-
def parse_xml(xml)
|
56
|
+
def parse_xml(xml, options = { mapping: :_default })
|
57
57
|
element = case xml
|
58
58
|
when REXML::Document
|
59
59
|
xml.root
|
@@ -63,7 +63,7 @@ module XML
|
|
63
63
|
fail ArgumentError, "Unexpected argument type; expected XML document, String, or IO source, was #{xml.class}" unless can_parse(xml)
|
64
64
|
REXML::Document.new(xml).root
|
65
65
|
end
|
66
|
-
load_from_xml(element)
|
66
|
+
load_from_xml(element, options)
|
67
67
|
end
|
68
68
|
|
69
69
|
private
|
@@ -13,6 +13,9 @@ module XML
|
|
13
13
|
text_node :text, 'text()'
|
14
14
|
array_node :children, 'child', class: String
|
15
15
|
|
16
|
+
use_mapping :alternate
|
17
|
+
text_node :text_alt, 'text()'
|
18
|
+
|
16
19
|
def <=>(other)
|
17
20
|
return nil unless self.class == other.class
|
18
21
|
[:attribute, :text, :children].each do |p|
|
@@ -38,6 +41,17 @@ module XML
|
|
38
41
|
expect(xml_string).to be_a(String)
|
39
42
|
expect(xml_string).to be_xml(expected_xml)
|
40
43
|
end
|
44
|
+
|
45
|
+
it 'accepts an alternate mapping' do
|
46
|
+
obj = MXSpecObject.new
|
47
|
+
obj.attribute = 123
|
48
|
+
obj.text_alt = 'element text'
|
49
|
+
obj.children = ['child 1', 'child 2']
|
50
|
+
expected_xml = obj.save_to_xml(mapping: :alternate)
|
51
|
+
xml_string = obj.write_xml(mapping: :alternate)
|
52
|
+
expect(xml_string).to be_a(String)
|
53
|
+
expect(xml_string).to be_xml(expected_xml)
|
54
|
+
end
|
41
55
|
end
|
42
56
|
|
43
57
|
module ClassMethods
|
@@ -86,6 +100,12 @@ module XML
|
|
86
100
|
xml_file.close(true)
|
87
101
|
end
|
88
102
|
end
|
103
|
+
|
104
|
+
it 'accepts an alternate mapping' do
|
105
|
+
@expected_element = MXSpecObject.load_from_xml(@xml_element, mapping: :alternate)
|
106
|
+
obj = MXSpecObject.parse_xml(@xml_element, mapping: :alternate)
|
107
|
+
expect(obj).to eq(@expected_element)
|
108
|
+
end
|
89
109
|
end
|
90
110
|
end
|
91
111
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-mapping_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Moles
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mime-types
|