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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e73d6da3501c828dfcc61abf5e57819fafe02503
4
- data.tar.gz: 658f243f24e3893e5ef5c9db5119253590a35e80
3
+ metadata.gz: 8846f750f369604e10e815fa58a11019c8dd8b3d
4
+ data.tar.gz: 3b05e612551a8d455108af3d7f351f5b6566b2e0
5
5
  SHA512:
6
- metadata.gz: 5491021c83c519e27adeb55c3b4f2b965a75818c1446ade0028ffbf47e7cac9e227cc63448856945cca2852c48afdfcc9a3f0679bf6fd4dcb74b00c798fdc9f0
7
- data.tar.gz: ec5cb7318df6f5e049eb0eaec50f9efff2047060d0b7b772cf5d6770408d064b9e74227ead666aedde8d14183ab5074a763c6fddec22e028a189fb90e4f2649a
6
+ metadata.gz: 89e7edf4af982cb52e5222f385d136911e4ca48e5719bff5c6576c691e151f152dc2ec6f91a6d6f997b1f1a6ae146677c208ae6c29f5375592498912e505bc07
7
+ data.tar.gz: 44b088113a9a0bc8f1335e1cc447977ca94ac9e516823c5bf436cb78060e7a2e73336a8b6208e6611ffe2ed080d2bca55eb4fea84a5334edaef1049c77033365
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.4.3 (11 July 2016)
2
+
3
+ - Allow `write_xml` and `parse_xml` to take an `options` hash, to be passed on to `save_to_xml` or `load_from_xml`,
4
+ respectively
5
+
1
6
  ## 0.4.2 (1 June 2016)
2
7
 
3
8
  - Made `DateNode` truncate a `DateTime` to a date
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
@@ -1,6 +1,6 @@
1
1
  module XML
2
2
  module MappingExtensions
3
3
  # The version of this gem
4
- VERSION = '0.4.2'
4
+ VERSION = '0.4.3'
5
5
  end
6
6
  end
@@ -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.2
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-06-01 00:00:00.000000000 Z
11
+ date: 2016-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types