xml-mapping_extensions 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +4 -0
- data/lib/xml/mapping_extensions/version.rb +1 -1
- data/lib/xml/mapping_extensions.rb +23 -0
- data/spec/unit/xml/mapping_extensions/fallback_mapping_custom_node_spec.rb +67 -0
- data/spec/unit/xml/mapping_extensions/mapping_extensions_spec.rb +0 -2
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80244c4ebbcf9f398b53a71edff5b5ec7c884fc5
|
4
|
+
data.tar.gz: 55fc77d2fba3a0a5e701e2328d076cd173f854bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b1388133c111c18d26a5e4509a12a8ac99e225886164b07e42ab203313c1cae64076b946157252ad0b0ebcd795a84459387c5fa437920d6fa337ce1317c9ca7
|
7
|
+
data.tar.gz: a75557813d6716fce717f2a163afce686830b937a0c544dec915e3d6c96b5dee25415ec869c551ea9def763e0158d8b354cc3ef3edae2ecbb20e74c2484cb205
|
data/CHANGES.md
CHANGED
@@ -75,6 +75,22 @@ module XML
|
|
75
75
|
attr_accessor :mapping
|
76
76
|
end
|
77
77
|
|
78
|
+
# Monkey-patch ArrayNode to regenerate marshallers and unmarshallers after cloning
|
79
|
+
class ArrayNode
|
80
|
+
old_init = instance_method(:initialize)
|
81
|
+
define_method(:initialize) do |*args|
|
82
|
+
@init_args = args
|
83
|
+
old_init.bind(self).call(*args)
|
84
|
+
end
|
85
|
+
|
86
|
+
define_method(:re_initialize) do |args|
|
87
|
+
old_init.bind(self).call(*args)
|
88
|
+
end
|
89
|
+
|
90
|
+
attr_reader :init_args
|
91
|
+
attr_accessor :options
|
92
|
+
end
|
93
|
+
|
78
94
|
module ClassMethods
|
79
95
|
|
80
96
|
# Gets the configured root element names for this object.
|
@@ -156,6 +172,13 @@ module XML
|
|
156
172
|
def clone_node(fallback_node, new_mapping)
|
157
173
|
node = fallback_node.clone
|
158
174
|
node.mapping = new_mapping
|
175
|
+
if node.is_a?(ArrayNode)
|
176
|
+
args = node.init_args.clone
|
177
|
+
options = args.pop.clone
|
178
|
+
options[:mapping] = new_mapping
|
179
|
+
args << options
|
180
|
+
node.re_initialize(args)
|
181
|
+
end
|
159
182
|
node
|
160
183
|
end
|
161
184
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module XML
|
4
|
+
module Mapping
|
5
|
+
class Value
|
6
|
+
attr_reader :value
|
7
|
+
|
8
|
+
def initialize(value:)
|
9
|
+
@value = value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ValueNode < SingleAttributeNode
|
14
|
+
def initialize(*args)
|
15
|
+
path, *args = super(*args)
|
16
|
+
@path = ::XML::XXPath.new(path)
|
17
|
+
args
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_attr_value(xml, value)
|
21
|
+
element = @path.first(xml, ensure_created: true)
|
22
|
+
if mapping == :fancy
|
23
|
+
element.attributes << REXML::Attribute.new('value', value)
|
24
|
+
else
|
25
|
+
element.text = value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
XML::Mapping.add_node_class ValueNode
|
30
|
+
|
31
|
+
class Holder
|
32
|
+
include XML::Mapping
|
33
|
+
|
34
|
+
def initialize(value)
|
35
|
+
@value = value
|
36
|
+
end
|
37
|
+
|
38
|
+
value_node :value, 'value', default_value: nil
|
39
|
+
use_mapping :fancy
|
40
|
+
fallback_mapping :fancy, :_default
|
41
|
+
end
|
42
|
+
|
43
|
+
class HolderHolder
|
44
|
+
include XML::Mapping
|
45
|
+
array_node :holders, 'holders', 'holder', class: Holder, default_value: []
|
46
|
+
use_mapping :fancy
|
47
|
+
fallback_mapping :fancy, :_default
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#fallback_mapping' do
|
51
|
+
it 'works with arrays mapped by custom nodes' do
|
52
|
+
expected = '<holder-holder>
|
53
|
+
<holders>
|
54
|
+
<holder><value value="foo"/></holder>
|
55
|
+
<holder><value value="bar"/></holder>
|
56
|
+
</holders>
|
57
|
+
</holder-holder>'
|
58
|
+
|
59
|
+
hh = HolderHolder.new
|
60
|
+
hh.holders = [Holder.new('foo'), Holder.new('bar')]
|
61
|
+
xml = hh.write_xml(mapping: :fancy)
|
62
|
+
expect(xml).to be_xml(expected)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
@@ -77,7 +77,6 @@ module XML
|
|
77
77
|
outfile = "#{tmp}/mxspec.xml"
|
78
78
|
@obj.write_to_file(outfile, indent: 4)
|
79
79
|
outxml = File.read(outfile)
|
80
|
-
puts("'#{outxml}'")
|
81
80
|
expect(outxml).to be_xml(@expected)
|
82
81
|
end
|
83
82
|
end
|
@@ -87,7 +86,6 @@ module XML
|
|
87
86
|
outfile = "#{tmp}/mxspec.xml"
|
88
87
|
@obj.write_to_file(outfile, pretty: true)
|
89
88
|
outxml = File.read(outfile)
|
90
|
-
puts("'#{outxml}'")
|
91
89
|
expect(outxml).to be_xml(@expected)
|
92
90
|
end
|
93
91
|
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.7
|
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-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mime-types
|
@@ -202,6 +202,7 @@ files:
|
|
202
202
|
- spec/spec_helper.rb
|
203
203
|
- spec/unit/xml/mapping_extensions/array_node_spec.rb
|
204
204
|
- spec/unit/xml/mapping_extensions/date_node_spec.rb
|
205
|
+
- spec/unit/xml/mapping_extensions/fallback_mapping_custom_node_spec.rb
|
205
206
|
- spec/unit/xml/mapping_extensions/fallback_mapping_spec.rb
|
206
207
|
- spec/unit/xml/mapping_extensions/mapping_extensions_spec.rb
|
207
208
|
- spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
|
@@ -232,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
232
233
|
version: '0'
|
233
234
|
requirements: []
|
234
235
|
rubyforge_project:
|
235
|
-
rubygems_version: 2.
|
236
|
+
rubygems_version: 2.6.6
|
236
237
|
signing_key:
|
237
238
|
specification_version: 4
|
238
239
|
summary: Some extensions for for XML::Mapping
|
@@ -242,6 +243,7 @@ test_files:
|
|
242
243
|
- spec/spec_helper.rb
|
243
244
|
- spec/unit/xml/mapping_extensions/array_node_spec.rb
|
244
245
|
- spec/unit/xml/mapping_extensions/date_node_spec.rb
|
246
|
+
- spec/unit/xml/mapping_extensions/fallback_mapping_custom_node_spec.rb
|
245
247
|
- spec/unit/xml/mapping_extensions/fallback_mapping_spec.rb
|
246
248
|
- spec/unit/xml/mapping_extensions/mapping_extensions_spec.rb
|
247
249
|
- spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
|