shale 0.6.0 → 0.7.0
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 +4 -4
- data/CHANGELOG.md +12 -0
- data/LICENSE.txt +1 -1
- data/README.md +172 -29
- data/lib/shale/adapter/json.rb +3 -3
- data/lib/shale/adapter/nokogiri.rb +6 -5
- data/lib/shale/adapter/ox.rb +5 -4
- data/lib/shale/adapter/rexml/document.rb +1 -1
- data/lib/shale/adapter/rexml.rb +5 -4
- data/lib/shale/error.rb +8 -3
- data/lib/shale/mapping/descriptor/dict.rb +12 -1
- data/lib/shale/mapping/descriptor/xml.rb +3 -2
- data/lib/shale/mapping/dict.rb +8 -2
- data/lib/shale/mapping/xml.rb +16 -5
- data/lib/shale/schema/json_generator.rb +1 -3
- data/lib/shale/schema/xml_generator/import.rb +2 -2
- data/lib/shale/schema/xml_generator.rb +4 -6
- data/lib/shale/type/complex.rb +397 -76
- data/lib/shale/type/value.rb +10 -10
- data/lib/shale/version.rb +1 -1
- metadata +2 -2
data/lib/shale/type/complex.rb
CHANGED
@@ -16,11 +16,14 @@ module Shale
|
|
16
16
|
# Convert Hash to Object using Hash/JSON/YAML/TOML mapping
|
17
17
|
#
|
18
18
|
# @param [Hash] hash Hash to convert
|
19
|
+
# @param [Array<Symbol>] only
|
20
|
+
# @param [Array<Symbol>] except
|
21
|
+
# @param [any] context
|
19
22
|
#
|
20
|
-
# @return [
|
23
|
+
# @return [model instance]
|
21
24
|
#
|
22
25
|
# @api public
|
23
|
-
def of_#{format}(hash)
|
26
|
+
def of_#{format}(hash, only: nil, except: nil, context: nil)
|
24
27
|
instance = model.new
|
25
28
|
|
26
29
|
attributes
|
@@ -30,25 +33,57 @@ module Shale
|
|
30
33
|
|
31
34
|
mapping_keys = #{format}_mapping.keys
|
32
35
|
|
36
|
+
only = to_partial_render_attributes(only)
|
37
|
+
except = to_partial_render_attributes(except)
|
38
|
+
|
33
39
|
hash.each do |key, value|
|
34
40
|
mapping = mapping_keys[key]
|
35
41
|
next unless mapping
|
36
42
|
|
37
43
|
if mapping.method_from
|
38
|
-
|
44
|
+
mapper = new
|
45
|
+
|
46
|
+
if mapper.method(mapping.method_from).arity == 3
|
47
|
+
mapper.send(mapping.method_from, instance, value, context)
|
48
|
+
else
|
49
|
+
mapper.send(mapping.method_from, instance, value)
|
50
|
+
end
|
39
51
|
else
|
40
52
|
attribute = attributes[mapping.attribute]
|
41
53
|
next unless attribute
|
42
54
|
|
55
|
+
if only
|
56
|
+
attribute_only = only[attribute.name]
|
57
|
+
next unless only.key?(attribute.name)
|
58
|
+
end
|
59
|
+
|
60
|
+
if except
|
61
|
+
attribute_except = except[attribute.name]
|
62
|
+
next if except.key?(attribute.name) && attribute_except.nil?
|
63
|
+
end
|
64
|
+
|
43
65
|
if value.nil?
|
44
66
|
instance.send(attribute.setter, nil)
|
45
67
|
elsif attribute.collection?
|
46
68
|
[*value].each do |val|
|
47
|
-
|
69
|
+
if val
|
70
|
+
val = attribute.type.of_#{format}(
|
71
|
+
val,
|
72
|
+
only: attribute_only,
|
73
|
+
except: attribute_except,
|
74
|
+
context: context
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
48
78
|
instance.send(attribute.name) << attribute.type.cast(val)
|
49
79
|
end
|
50
80
|
else
|
51
|
-
val = attribute.type.of_#{format}(
|
81
|
+
val = attribute.type.of_#{format}(
|
82
|
+
value,
|
83
|
+
only: attribute_only,
|
84
|
+
except: attribute_except,
|
85
|
+
context: context
|
86
|
+
)
|
52
87
|
instance.send(attribute.setter, attribute.type.cast(val))
|
53
88
|
end
|
54
89
|
end
|
@@ -60,13 +95,16 @@ module Shale
|
|
60
95
|
# Convert Object to Hash using Hash/JSON/YAML/TOML mapping
|
61
96
|
#
|
62
97
|
# @param [any] instance Object to convert
|
98
|
+
# @param [Array<Symbol>] only
|
99
|
+
# @param [Array<Symbol>] except
|
100
|
+
# @param [any] context
|
63
101
|
#
|
64
102
|
# @raise [IncorrectModelError]
|
65
103
|
#
|
66
104
|
# @return [Hash]
|
67
105
|
#
|
68
106
|
# @api public
|
69
|
-
def as_#{format}(instance)
|
107
|
+
def as_#{format}(instance, only: nil, except: nil, context: nil)
|
70
108
|
unless instance.is_a?(model)
|
71
109
|
msg = "argument is a '\#{instance.class}' but should be a '\#{model}'"
|
72
110
|
raise IncorrectModelError, msg
|
@@ -74,23 +112,56 @@ module Shale
|
|
74
112
|
|
75
113
|
hash = {}
|
76
114
|
|
115
|
+
only = to_partial_render_attributes(only)
|
116
|
+
except = to_partial_render_attributes(except)
|
117
|
+
|
77
118
|
#{format}_mapping.keys.each_value do |mapping|
|
78
119
|
if mapping.method_to
|
79
|
-
|
120
|
+
mapper = new
|
121
|
+
|
122
|
+
if mapper.method(mapping.method_to).arity == 3
|
123
|
+
mapper.send(mapping.method_to, instance, hash, context)
|
124
|
+
else
|
125
|
+
mapper.send(mapping.method_to, instance, hash)
|
126
|
+
end
|
80
127
|
else
|
81
128
|
attribute = attributes[mapping.attribute]
|
82
129
|
next unless attribute
|
83
130
|
|
131
|
+
if only
|
132
|
+
attribute_only = only[attribute.name]
|
133
|
+
next unless only.key?(attribute.name)
|
134
|
+
end
|
135
|
+
|
136
|
+
if except
|
137
|
+
attribute_except = except[attribute.name]
|
138
|
+
next if except.key?(attribute.name) && attribute_except.nil?
|
139
|
+
end
|
140
|
+
|
84
141
|
value = instance.send(attribute.name)
|
85
142
|
|
86
143
|
if value.nil?
|
87
|
-
hash[mapping.name] = nil
|
144
|
+
hash[mapping.name] = nil if mapping.render_nil?
|
88
145
|
elsif attribute.collection?
|
89
|
-
hash[mapping.name] = [*value].map do |
|
90
|
-
|
146
|
+
hash[mapping.name] = [*value].map do |val|
|
147
|
+
if val
|
148
|
+
attribute.type.as_#{format}(
|
149
|
+
val,
|
150
|
+
only: attribute_only,
|
151
|
+
except: attribute_except,
|
152
|
+
context: context
|
153
|
+
)
|
154
|
+
else
|
155
|
+
val
|
156
|
+
end
|
91
157
|
end
|
92
158
|
else
|
93
|
-
hash[mapping.name] = attribute.type.as_#{format}(
|
159
|
+
hash[mapping.name] = attribute.type.as_#{format}(
|
160
|
+
value,
|
161
|
+
only: attribute_only,
|
162
|
+
except: attribute_except,
|
163
|
+
context: context
|
164
|
+
)
|
94
165
|
end
|
95
166
|
end
|
96
167
|
end
|
@@ -107,80 +178,123 @@ module Shale
|
|
107
178
|
# Convert JSON to Object
|
108
179
|
#
|
109
180
|
# @param [String] json JSON to convert
|
181
|
+
# @param [Array<Symbol>] only
|
182
|
+
# @param [Array<Symbol>] except
|
183
|
+
# @param [any] context
|
110
184
|
#
|
111
|
-
# @return [
|
185
|
+
# @return [model instance]
|
112
186
|
#
|
113
187
|
# @api public
|
114
|
-
def from_json(json)
|
115
|
-
of_json(
|
188
|
+
def from_json(json, only: nil, except: nil, context: nil)
|
189
|
+
of_json(
|
190
|
+
Shale.json_adapter.load(json),
|
191
|
+
only: only,
|
192
|
+
except: except,
|
193
|
+
context: context
|
194
|
+
)
|
116
195
|
end
|
117
196
|
|
118
197
|
# Convert Object to JSON
|
119
198
|
#
|
120
|
-
# @param [
|
121
|
-
# @param [Array<Symbol>]
|
199
|
+
# @param [model instance] instance Object to convert
|
200
|
+
# @param [Array<Symbol>] only
|
201
|
+
# @param [Array<Symbol>] except
|
202
|
+
# @param [any] context
|
203
|
+
# @param [true, false] pretty
|
122
204
|
#
|
123
205
|
# @return [String]
|
124
206
|
#
|
125
207
|
# @api public
|
126
|
-
def to_json(instance,
|
127
|
-
Shale.json_adapter.dump(
|
208
|
+
def to_json(instance, only: nil, except: nil, context: nil, pretty: false)
|
209
|
+
Shale.json_adapter.dump(
|
210
|
+
as_json(instance, only: only, except: except, context: context),
|
211
|
+
pretty: pretty
|
212
|
+
)
|
128
213
|
end
|
129
214
|
|
130
215
|
# Convert YAML to Object
|
131
216
|
#
|
132
217
|
# @param [String] yaml YAML to convert
|
218
|
+
# @param [Array<Symbol>] only
|
219
|
+
# @param [Array<Symbol>] except
|
220
|
+
# @param [any] context
|
133
221
|
#
|
134
|
-
# @return [
|
222
|
+
# @return [model instance]
|
135
223
|
#
|
136
224
|
# @api public
|
137
|
-
def from_yaml(yaml)
|
138
|
-
of_yaml(
|
225
|
+
def from_yaml(yaml, only: nil, except: nil, context: nil)
|
226
|
+
of_yaml(
|
227
|
+
Shale.yaml_adapter.load(yaml),
|
228
|
+
only: only,
|
229
|
+
except: except,
|
230
|
+
context: context
|
231
|
+
)
|
139
232
|
end
|
140
233
|
|
141
234
|
# Convert Object to YAML
|
142
235
|
#
|
143
|
-
# @param [
|
236
|
+
# @param [model instance] instance Object to convert
|
237
|
+
# @param [Array<Symbol>] only
|
238
|
+
# @param [Array<Symbol>] except
|
239
|
+
# @param [any] context
|
144
240
|
#
|
145
241
|
# @return [String]
|
146
242
|
#
|
147
243
|
# @api public
|
148
|
-
def to_yaml(instance)
|
149
|
-
Shale.yaml_adapter.dump(
|
244
|
+
def to_yaml(instance, only: nil, except: nil, context: nil)
|
245
|
+
Shale.yaml_adapter.dump(
|
246
|
+
as_yaml(instance, only: only, except: except, context: context)
|
247
|
+
)
|
150
248
|
end
|
151
249
|
|
152
250
|
# Convert TOML to Object
|
153
251
|
#
|
154
252
|
# @param [String] toml TOML to convert
|
253
|
+
# @param [Array<Symbol>] only
|
254
|
+
# @param [Array<Symbol>] except
|
255
|
+
# @param [any] context
|
155
256
|
#
|
156
|
-
# @return [
|
257
|
+
# @return [model instance]
|
157
258
|
#
|
158
259
|
# @api public
|
159
|
-
def from_toml(toml)
|
260
|
+
def from_toml(toml, only: nil, except: nil, context: nil)
|
160
261
|
validate_toml_adapter
|
161
|
-
of_toml(
|
262
|
+
of_toml(
|
263
|
+
Shale.toml_adapter.load(toml),
|
264
|
+
only: only,
|
265
|
+
except: except,
|
266
|
+
context: context
|
267
|
+
)
|
162
268
|
end
|
163
269
|
|
164
270
|
# Convert Object to TOML
|
165
271
|
#
|
166
|
-
# @param [
|
272
|
+
# @param [model instance] instance Object to convert
|
273
|
+
# @param [Array<Symbol>] only
|
274
|
+
# @param [Array<Symbol>] except
|
275
|
+
# @param [any] context
|
167
276
|
#
|
168
277
|
# @return [String]
|
169
278
|
#
|
170
279
|
# @api public
|
171
|
-
def to_toml(instance)
|
280
|
+
def to_toml(instance, only: nil, except: nil, context: nil)
|
172
281
|
validate_toml_adapter
|
173
|
-
Shale.toml_adapter.dump(
|
282
|
+
Shale.toml_adapter.dump(
|
283
|
+
as_toml(instance, only: only, except: except, context: context)
|
284
|
+
)
|
174
285
|
end
|
175
286
|
|
176
287
|
# Convert XML document to Object
|
177
288
|
#
|
178
|
-
# @param [Shale::Adapter::<XML adapter>::Node]
|
289
|
+
# @param [Shale::Adapter::<XML adapter>::Node] element
|
290
|
+
# @param [Array<Symbol>] only
|
291
|
+
# @param [Array<Symbol>] except
|
292
|
+
# @param [any] context
|
179
293
|
#
|
180
|
-
# @return [
|
294
|
+
# @return [model instance]
|
181
295
|
#
|
182
296
|
# @api public
|
183
|
-
def of_xml(element)
|
297
|
+
def of_xml(element, only: nil, except: nil, context: nil)
|
184
298
|
instance = model.new
|
185
299
|
|
186
300
|
attributes
|
@@ -188,16 +302,28 @@ module Shale
|
|
188
302
|
.select { |attr| attr.default }
|
189
303
|
.each { |attr| instance.send(attr.setter, attr.default.call) }
|
190
304
|
|
305
|
+
only = to_partial_render_attributes(only)
|
306
|
+
except = to_partial_render_attributes(except)
|
307
|
+
|
191
308
|
element.attributes.each do |key, value|
|
192
309
|
mapping = xml_mapping.attributes[key.to_s]
|
193
310
|
next unless mapping
|
194
311
|
|
195
312
|
if mapping.method_from
|
196
|
-
|
313
|
+
mapper = new
|
314
|
+
|
315
|
+
if mapper.method(mapping.method_from).arity == 3
|
316
|
+
mapper.send(mapping.method_from, instance, value, context)
|
317
|
+
else
|
318
|
+
mapper.send(mapping.method_from, instance, value)
|
319
|
+
end
|
197
320
|
else
|
198
321
|
attribute = attributes[mapping.attribute]
|
199
322
|
next unless attribute
|
200
323
|
|
324
|
+
next if only && !only.key?(attribute.name)
|
325
|
+
next if except&.key?(attribute.name)
|
326
|
+
|
201
327
|
if attribute.collection?
|
202
328
|
instance.send(attribute.name) << attribute.type.cast(value)
|
203
329
|
else
|
@@ -210,13 +336,28 @@ module Shale
|
|
210
336
|
|
211
337
|
if content_mapping
|
212
338
|
if content_mapping.method_from
|
213
|
-
|
339
|
+
mapper = new
|
340
|
+
|
341
|
+
if mapper.method(content_mapping.method_from).arity == 3
|
342
|
+
mapper.send(content_mapping.method_from, instance, element, context)
|
343
|
+
else
|
344
|
+
mapper.send(content_mapping.method_from, instance, element)
|
345
|
+
end
|
214
346
|
else
|
215
347
|
attribute = attributes[content_mapping.attribute]
|
216
348
|
|
217
349
|
if attribute
|
218
|
-
|
219
|
-
|
350
|
+
skip = false
|
351
|
+
|
352
|
+
# rubocop:disable Metrics/BlockNesting
|
353
|
+
skip = true if only && !only.key?(attribute.name)
|
354
|
+
skip = true if except&.key?(attribute.name)
|
355
|
+
|
356
|
+
unless skip
|
357
|
+
value = attribute.type.of_xml(element)
|
358
|
+
instance.send(attribute.setter, attribute.type.cast(value))
|
359
|
+
end
|
360
|
+
# rubocop:enable Metrics/BlockNesting
|
220
361
|
end
|
221
362
|
end
|
222
363
|
end
|
@@ -226,12 +367,33 @@ module Shale
|
|
226
367
|
next unless mapping
|
227
368
|
|
228
369
|
if mapping.method_from
|
229
|
-
|
370
|
+
mapper = new
|
371
|
+
|
372
|
+
if mapper.method(mapping.method_from).arity == 3
|
373
|
+
mapper.send(mapping.method_from, instance, node, context)
|
374
|
+
else
|
375
|
+
mapper.send(mapping.method_from, instance, node)
|
376
|
+
end
|
230
377
|
else
|
231
378
|
attribute = attributes[mapping.attribute]
|
232
379
|
next unless attribute
|
233
380
|
|
234
|
-
|
381
|
+
if only
|
382
|
+
attribute_only = only[attribute.name]
|
383
|
+
next unless only.key?(attribute.name)
|
384
|
+
end
|
385
|
+
|
386
|
+
if except
|
387
|
+
attribute_except = except[attribute.name]
|
388
|
+
next if except.key?(attribute.name) && attribute_except.nil?
|
389
|
+
end
|
390
|
+
|
391
|
+
value = attribute.type.of_xml(
|
392
|
+
node,
|
393
|
+
only: attribute_only,
|
394
|
+
except: attribute_except,
|
395
|
+
context: context
|
396
|
+
)
|
235
397
|
|
236
398
|
if attribute.collection?
|
237
399
|
instance.send(attribute.name) << attribute.type.cast(value)
|
@@ -247,15 +409,23 @@ module Shale
|
|
247
409
|
# Convert XML to Object
|
248
410
|
#
|
249
411
|
# @param [String] xml XML to convert
|
412
|
+
# @param [Array<Symbol>] only
|
413
|
+
# @param [Array<Symbol>] except
|
414
|
+
# @param [any] context
|
250
415
|
#
|
251
416
|
# @raise [AdapterError]
|
252
417
|
#
|
253
|
-
# @return [
|
418
|
+
# @return [model instance]
|
254
419
|
#
|
255
420
|
# @api public
|
256
|
-
def from_xml(xml)
|
421
|
+
def from_xml(xml, only: nil, except: nil, context: nil)
|
257
422
|
validate_xml_adapter
|
258
|
-
of_xml(
|
423
|
+
of_xml(
|
424
|
+
Shale.xml_adapter.load(xml),
|
425
|
+
only: only,
|
426
|
+
except: except,
|
427
|
+
context: context
|
428
|
+
)
|
259
429
|
end
|
260
430
|
|
261
431
|
# Convert Object to XML document
|
@@ -263,13 +433,24 @@ module Shale
|
|
263
433
|
# @param [any] instance Object to convert
|
264
434
|
# @param [String, nil] node_name XML node name
|
265
435
|
# @param [Shale::Adapter::<xml adapter>::Document, nil] doc Object to convert
|
436
|
+
# @param [Array<Symbol>] only
|
437
|
+
# @param [Array<Symbol>] except
|
438
|
+
# @param [any] context
|
266
439
|
#
|
267
440
|
# @raise [IncorrectModelError]
|
268
441
|
#
|
269
442
|
# @return [::REXML::Document, ::Nokogiri::Document, ::Ox::Document]
|
270
443
|
#
|
271
444
|
# @api public
|
272
|
-
def as_xml(
|
445
|
+
def as_xml(
|
446
|
+
instance,
|
447
|
+
node_name = nil,
|
448
|
+
doc = nil,
|
449
|
+
_cdata = nil,
|
450
|
+
only: nil,
|
451
|
+
except: nil,
|
452
|
+
context: nil
|
453
|
+
)
|
273
454
|
unless instance.is_a?(model)
|
274
455
|
msg = "argument is a '#{instance.class}' but should be a '#{model}'"
|
275
456
|
raise IncorrectModelError, msg
|
@@ -277,7 +458,17 @@ module Shale
|
|
277
458
|
|
278
459
|
unless doc
|
279
460
|
doc = Shale.xml_adapter.create_document
|
280
|
-
|
461
|
+
|
462
|
+
element = as_xml(
|
463
|
+
instance,
|
464
|
+
xml_mapping.prefixed_root,
|
465
|
+
doc,
|
466
|
+
only: only,
|
467
|
+
except: except,
|
468
|
+
context: context
|
469
|
+
)
|
470
|
+
doc.add_element(doc.doc, element)
|
471
|
+
|
281
472
|
return doc.doc
|
282
473
|
end
|
283
474
|
|
@@ -288,18 +479,31 @@ module Shale
|
|
288
479
|
xml_mapping.default_namespace.name
|
289
480
|
)
|
290
481
|
|
482
|
+
only = to_partial_render_attributes(only)
|
483
|
+
except = to_partial_render_attributes(except)
|
484
|
+
|
291
485
|
xml_mapping.attributes.each_value do |mapping|
|
292
486
|
if mapping.method_to
|
293
|
-
|
487
|
+
mapper = new
|
488
|
+
|
489
|
+
if mapper.method(mapping.method_to).arity == 4
|
490
|
+
mapper.send(mapping.method_to, instance, element, doc, context)
|
491
|
+
else
|
492
|
+
mapper.send(mapping.method_to, instance, element, doc)
|
493
|
+
end
|
294
494
|
else
|
295
495
|
attribute = attributes[mapping.attribute]
|
296
496
|
next unless attribute
|
297
497
|
|
498
|
+
next if only && !only.key?(attribute.name)
|
499
|
+
next if except&.key?(attribute.name)
|
500
|
+
|
298
501
|
value = instance.send(attribute.name)
|
299
|
-
next if value.nil?
|
300
502
|
|
301
|
-
|
302
|
-
|
503
|
+
if mapping.render_nil? || !value.nil?
|
504
|
+
doc.add_namespace(mapping.namespace.prefix, mapping.namespace.name)
|
505
|
+
doc.add_attribute(element, mapping.prefixed_name, value)
|
506
|
+
end
|
303
507
|
end
|
304
508
|
end
|
305
509
|
|
@@ -307,18 +511,31 @@ module Shale
|
|
307
511
|
|
308
512
|
if content_mapping
|
309
513
|
if content_mapping.method_to
|
310
|
-
|
514
|
+
mapper = new
|
515
|
+
|
516
|
+
if mapper.method(content_mapping.method_to).arity == 4
|
517
|
+
mapper.send(content_mapping.method_to, instance, element, doc, context)
|
518
|
+
else
|
519
|
+
mapper.send(content_mapping.method_to, instance, element, doc)
|
520
|
+
end
|
311
521
|
else
|
312
522
|
attribute = attributes[content_mapping.attribute]
|
313
523
|
|
314
524
|
if attribute
|
315
|
-
|
525
|
+
skip = false
|
316
526
|
|
317
527
|
# rubocop:disable Metrics/BlockNesting
|
318
|
-
if
|
319
|
-
|
320
|
-
|
321
|
-
|
528
|
+
skip = true if only && !only.key?(attribute.name)
|
529
|
+
skip = true if except&.key?(attribute.name)
|
530
|
+
|
531
|
+
unless skip
|
532
|
+
value = instance.send(attribute.name)
|
533
|
+
|
534
|
+
if content_mapping.cdata
|
535
|
+
doc.create_cdata(value.to_s, element)
|
536
|
+
else
|
537
|
+
doc.add_text(element, value.to_s)
|
538
|
+
end
|
322
539
|
end
|
323
540
|
# rubocop:enable Metrics/BlockNesting
|
324
541
|
end
|
@@ -327,24 +544,62 @@ module Shale
|
|
327
544
|
|
328
545
|
xml_mapping.elements.each_value do |mapping|
|
329
546
|
if mapping.method_to
|
330
|
-
|
547
|
+
mapper = new
|
548
|
+
|
549
|
+
if mapper.method(mapping.method_to).arity == 4
|
550
|
+
mapper.send(mapping.method_to, instance, element, doc, context)
|
551
|
+
else
|
552
|
+
mapper.send(mapping.method_to, instance, element, doc)
|
553
|
+
end
|
331
554
|
else
|
332
555
|
attribute = attributes[mapping.attribute]
|
333
556
|
next unless attribute
|
334
557
|
|
558
|
+
if only
|
559
|
+
attribute_only = only[attribute.name]
|
560
|
+
next unless only.key?(attribute.name)
|
561
|
+
end
|
562
|
+
|
563
|
+
if except
|
564
|
+
attribute_except = except[attribute.name]
|
565
|
+
next if except.key?(attribute.name) && attribute_except.nil?
|
566
|
+
end
|
567
|
+
|
335
568
|
value = instance.send(attribute.name)
|
336
|
-
next if value.nil?
|
337
569
|
|
338
|
-
|
570
|
+
if mapping.render_nil? || !value.nil?
|
571
|
+
doc.add_namespace(mapping.namespace.prefix, mapping.namespace.name)
|
572
|
+
end
|
339
573
|
|
340
|
-
if
|
574
|
+
if value.nil?
|
575
|
+
if mapping.render_nil?
|
576
|
+
child = doc.create_element(mapping.prefixed_name)
|
577
|
+
doc.add_element(element, child)
|
578
|
+
end
|
579
|
+
elsif attribute.collection?
|
341
580
|
[*value].each do |v|
|
342
581
|
next if v.nil?
|
343
|
-
child = attribute.type.as_xml(
|
582
|
+
child = attribute.type.as_xml(
|
583
|
+
v,
|
584
|
+
mapping.prefixed_name,
|
585
|
+
doc,
|
586
|
+
mapping.cdata,
|
587
|
+
only: attribute_only,
|
588
|
+
except: attribute_except,
|
589
|
+
context: context
|
590
|
+
)
|
344
591
|
doc.add_element(element, child)
|
345
592
|
end
|
346
593
|
else
|
347
|
-
child = attribute.type.as_xml(
|
594
|
+
child = attribute.type.as_xml(
|
595
|
+
value,
|
596
|
+
mapping.prefixed_name,
|
597
|
+
doc,
|
598
|
+
mapping.cdata,
|
599
|
+
only: attribute_only,
|
600
|
+
except: attribute_except,
|
601
|
+
context: context
|
602
|
+
)
|
348
603
|
doc.add_element(element, child)
|
349
604
|
end
|
350
605
|
end
|
@@ -355,17 +610,32 @@ module Shale
|
|
355
610
|
|
356
611
|
# Convert Object to XML
|
357
612
|
#
|
358
|
-
# @param [
|
359
|
-
# @param [Array<Symbol>]
|
613
|
+
# @param [model instance] instance Object to convert
|
614
|
+
# @param [Array<Symbol>] only
|
615
|
+
# @param [Array<Symbol>] except
|
616
|
+
# @param [any] context
|
617
|
+
# @param [true, false] pretty
|
618
|
+
# @param [true, false] declaration
|
360
619
|
#
|
361
620
|
# @raise [AdapterError]
|
362
621
|
#
|
363
622
|
# @return [String]
|
364
623
|
#
|
365
624
|
# @api public
|
366
|
-
def to_xml(
|
625
|
+
def to_xml(
|
626
|
+
instance,
|
627
|
+
only: nil,
|
628
|
+
except: nil,
|
629
|
+
context: nil,
|
630
|
+
pretty: false,
|
631
|
+
declaration: false
|
632
|
+
)
|
367
633
|
validate_xml_adapter
|
368
|
-
Shale.xml_adapter.dump(
|
634
|
+
Shale.xml_adapter.dump(
|
635
|
+
as_xml(instance, only: only, except: except, context: context),
|
636
|
+
pretty: pretty,
|
637
|
+
declaration: declaration
|
638
|
+
)
|
369
639
|
end
|
370
640
|
|
371
641
|
private
|
@@ -387,55 +657,106 @@ module Shale
|
|
387
657
|
def validate_xml_adapter
|
388
658
|
raise AdapterError, XML_ADAPTER_NOT_SET_MESSAGE unless Shale.xml_adapter
|
389
659
|
end
|
660
|
+
|
661
|
+
# Convert array with attributes to a hash
|
662
|
+
#
|
663
|
+
# @param [Array] ary
|
664
|
+
#
|
665
|
+
# @return [Hash, nil]
|
666
|
+
#
|
667
|
+
# @api private
|
668
|
+
def to_partial_render_attributes(ary)
|
669
|
+
return unless ary
|
670
|
+
|
671
|
+
ary.each_with_object([]) do |e, obj|
|
672
|
+
if e.is_a?(Hash)
|
673
|
+
obj.push(*e.to_a)
|
674
|
+
else
|
675
|
+
obj.push([e, nil])
|
676
|
+
end
|
677
|
+
end.to_h
|
678
|
+
end
|
390
679
|
end
|
391
680
|
|
392
681
|
# Convert Object to Hash
|
393
682
|
#
|
683
|
+
# @param [Array<Symbol>] only
|
684
|
+
# @param [Array<Symbol>] except
|
685
|
+
# @param [any] context
|
686
|
+
#
|
394
687
|
# @return [Hash]
|
395
688
|
#
|
396
689
|
# @api public
|
397
|
-
def to_hash
|
398
|
-
self.class.to_hash(self)
|
690
|
+
def to_hash(only: nil, except: nil, context: nil)
|
691
|
+
self.class.to_hash(self, only: only, except: except, context: context)
|
399
692
|
end
|
400
693
|
|
401
694
|
# Convert Object to JSON
|
402
695
|
#
|
403
|
-
# @param [Array<Symbol>]
|
696
|
+
# @param [Array<Symbol>] only
|
697
|
+
# @param [Array<Symbol>] except
|
698
|
+
# @param [any] context
|
699
|
+
# @param [true, false] pretty
|
404
700
|
#
|
405
701
|
# @return [String]
|
406
702
|
#
|
407
703
|
# @api public
|
408
|
-
def to_json(
|
409
|
-
self.class.to_json(
|
704
|
+
def to_json(only: nil, except: nil, context: nil, pretty: false)
|
705
|
+
self.class.to_json(
|
706
|
+
self,
|
707
|
+
only: only,
|
708
|
+
except: except,
|
709
|
+
context: context,
|
710
|
+
pretty: pretty
|
711
|
+
)
|
410
712
|
end
|
411
713
|
|
412
714
|
# Convert Object to YAML
|
413
715
|
#
|
716
|
+
# @param [Array<Symbol>] only
|
717
|
+
# @param [Array<Symbol>] except
|
718
|
+
# @param [any] context
|
719
|
+
#
|
414
720
|
# @return [String]
|
415
721
|
#
|
416
722
|
# @api public
|
417
|
-
def to_yaml
|
418
|
-
self.class.to_yaml(self)
|
723
|
+
def to_yaml(only: nil, except: nil, context: nil)
|
724
|
+
self.class.to_yaml(self, only: only, except: except, context: context)
|
419
725
|
end
|
420
726
|
|
421
727
|
# Convert Object to TOML
|
422
728
|
#
|
729
|
+
# @param [Array<Symbol>] only
|
730
|
+
# @param [Array<Symbol>] except
|
731
|
+
# @param [any] context
|
732
|
+
#
|
423
733
|
# @return [String]
|
424
734
|
#
|
425
735
|
# @api public
|
426
|
-
def to_toml
|
427
|
-
self.class.to_toml(self)
|
736
|
+
def to_toml(only: nil, except: nil, context: nil)
|
737
|
+
self.class.to_toml(self, only: only, except: except, context: context)
|
428
738
|
end
|
429
739
|
|
430
740
|
# Convert Object to XML
|
431
741
|
#
|
432
|
-
# @param [Array<Symbol>]
|
742
|
+
# @param [Array<Symbol>] only
|
743
|
+
# @param [Array<Symbol>] except
|
744
|
+
# @param [any] context
|
745
|
+
# @param [true, false] pretty
|
746
|
+
# @param [true, false] declaration
|
433
747
|
#
|
434
748
|
# @return [String]
|
435
749
|
#
|
436
750
|
# @api public
|
437
|
-
def to_xml(
|
438
|
-
self.class.to_xml(
|
751
|
+
def to_xml(only: nil, except: nil, context: nil, pretty: false, declaration: false)
|
752
|
+
self.class.to_xml(
|
753
|
+
self,
|
754
|
+
only: only,
|
755
|
+
except: except,
|
756
|
+
context: context,
|
757
|
+
pretty: pretty,
|
758
|
+
declaration: declaration
|
759
|
+
)
|
439
760
|
end
|
440
761
|
end
|
441
762
|
end
|