lutaml-model 0.3.14 → 0.3.16
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/.rubocop_todo.yml +13 -7
- data/README.adoc +83 -5
- data/lib/lutaml/model/attribute.rb +18 -4
- data/lib/lutaml/model/key_value_mapping.rb +2 -0
- data/lib/lutaml/model/key_value_mapping_rule.rb +2 -0
- data/lib/lutaml/model/mapping_rule.rb +6 -0
- data/lib/lutaml/model/serialize.rb +69 -48
- data/lib/lutaml/model/utils.rb +28 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/model/xml_adapter/xml_document.rb +19 -1
- data/lib/lutaml/model/xml_mapping.rb +6 -0
- data/lib/lutaml/model/xml_mapping_rule.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9687a8a307de8226b41a87abda56626627eefe47665628c0e9112b97f27385a
|
4
|
+
data.tar.gz: e819f531048e629d4943ea7b613815a568990f372ef2da93162d0d1b4d8395cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f47d1336093db1eb45c5225913a9c63d66681de77d5803a21702baab0ca8e571e882a2d8f6f6dcf7b9180c897c9ae8bfd4b2db6532e9691487d104696657457e
|
7
|
+
data.tar.gz: 9ec8b8d841606d202acdcca81464761cc0f75e9ae5b35d2c3b8dd01c6e8a122642a348f41de2852b09386d3748eb61b1b2547df8d1ccb146a7d36e5a8ac46257
|
data/.rubocop_todo.yml
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2024-10-
|
3
|
+
# on 2024-10-28 08:29:46 UTC using RuboCop version 1.66.1.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
-
# Offense count:
|
9
|
+
# Offense count: 148
|
10
10
|
# This cop supports safe autocorrection (--autocorrect).
|
11
11
|
# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
|
12
12
|
# URISchemes: http, https
|
@@ -69,10 +69,10 @@ Metrics/CyclomaticComplexity:
|
|
69
69
|
Metrics/MethodLength:
|
70
70
|
Max: 51
|
71
71
|
|
72
|
-
# Offense count:
|
72
|
+
# Offense count: 6
|
73
73
|
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
|
74
74
|
Metrics/ParameterLists:
|
75
|
-
Max:
|
75
|
+
Max: 13
|
76
76
|
|
77
77
|
# Offense count: 22
|
78
78
|
# Configuration parameters: AllowedMethods, AllowedPatterns, Max.
|
@@ -85,7 +85,7 @@ Metrics/PerceivedComplexity:
|
|
85
85
|
- 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
|
86
86
|
- 'lib/lutaml/model/xml_adapter/xml_document.rb'
|
87
87
|
|
88
|
-
# Offense count:
|
88
|
+
# Offense count: 9
|
89
89
|
# Configuration parameters: Prefixes, AllowedPatterns.
|
90
90
|
# Prefixes: when, with, without
|
91
91
|
RSpec/ContextWording:
|
@@ -125,7 +125,7 @@ RSpec/MultipleDescribes:
|
|
125
125
|
- 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
|
126
126
|
- 'spec/lutaml/model/xml_adapter_spec.rb'
|
127
127
|
|
128
|
-
# Offense count:
|
128
|
+
# Offense count: 125
|
129
129
|
RSpec/MultipleExpectations:
|
130
130
|
Max: 14
|
131
131
|
|
@@ -134,6 +134,11 @@ RSpec/MultipleExpectations:
|
|
134
134
|
RSpec/MultipleMemoizedHelpers:
|
135
135
|
Max: 9
|
136
136
|
|
137
|
+
# Offense count: 4
|
138
|
+
# Configuration parameters: AllowedGroups.
|
139
|
+
RSpec/NestedGroups:
|
140
|
+
Max: 4
|
141
|
+
|
137
142
|
# Offense count: 7
|
138
143
|
RSpec/PendingWithoutReason:
|
139
144
|
Exclude:
|
@@ -153,13 +158,14 @@ RSpec/RepeatedExampleGroupDescription:
|
|
153
158
|
Exclude:
|
154
159
|
- 'spec/lutaml/model/collection_spec.rb'
|
155
160
|
|
156
|
-
# Offense count:
|
161
|
+
# Offense count: 3
|
157
162
|
# Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
|
158
163
|
# Include: **/*_spec.rb
|
159
164
|
RSpec/SpecFilePathFormat:
|
160
165
|
Exclude:
|
161
166
|
- '**/spec/routing/**/*'
|
162
167
|
- 'spec/lutaml/model/collection_spec.rb'
|
168
|
+
- 'spec/lutaml/model/defaults_spec.rb'
|
163
169
|
- 'spec/lutaml/model/type/decimal_spec.rb'
|
164
170
|
|
165
171
|
# Offense count: 1
|
data/README.adoc
CHANGED
@@ -390,7 +390,7 @@ acceptance of the newly updated component.
|
|
390
390
|
====
|
391
391
|
|
392
392
|
|
393
|
-
=== Attribute value default
|
393
|
+
=== Attribute value default and rendering defaults
|
394
394
|
|
395
395
|
Specify default values for attributes using the `default` option.
|
396
396
|
The `default` option can be set to a value or a lambda that returns a value.
|
@@ -423,6 +423,80 @@ end
|
|
423
423
|
----
|
424
424
|
====
|
425
425
|
|
426
|
+
The "default behavior" (pun intended) is to not render a default value if
|
427
|
+
the current value is the same as the default value.
|
428
|
+
|
429
|
+
In certain cases, it is necessary to render the default value even if the
|
430
|
+
current value is the same as the default value. This can be achieved by setting
|
431
|
+
the `render_default` option to `true`.
|
432
|
+
|
433
|
+
Syntax:
|
434
|
+
|
435
|
+
[source,ruby]
|
436
|
+
----
|
437
|
+
attribute :name_of_attribute, Type, default: -> { value }, render_default: true
|
438
|
+
----
|
439
|
+
|
440
|
+
.Using the `render_default` option to force encoding the default value
|
441
|
+
[example]
|
442
|
+
====
|
443
|
+
[source,ruby]
|
444
|
+
----
|
445
|
+
class Glaze < Lutaml::Model::Serializable
|
446
|
+
attribute :color, :string, default: -> { 'Clear' }
|
447
|
+
attribute :opacity, :string, default: -> { 'Opaque' }
|
448
|
+
attribute :temperature, :integer, default: -> { 1050 }
|
449
|
+
attribute :firing_time, :integer, default: -> { 60 }
|
450
|
+
|
451
|
+
xml do
|
452
|
+
root "glaze"
|
453
|
+
map_element 'color', to: :color
|
454
|
+
map_element 'opacity', to: :opacity, render_default: true
|
455
|
+
map_attribute 'temperature', to: :temperature
|
456
|
+
map_attribute 'firingTime', to: :firing_time, render_default: true
|
457
|
+
end
|
458
|
+
|
459
|
+
json do
|
460
|
+
map 'color', to: :color
|
461
|
+
map 'opacity', to: :opacity, render_default: true
|
462
|
+
map 'temperature', to: :temperature
|
463
|
+
map 'firingTime', to: :firing_time, render_default: true
|
464
|
+
end
|
465
|
+
end
|
466
|
+
----
|
467
|
+
|
468
|
+
.Attributes with `render_default: true` are rendered when the value is identical to the default
|
469
|
+
[example]
|
470
|
+
====
|
471
|
+
[source,ruby]
|
472
|
+
----
|
473
|
+
> glaze_new = Glaze.new
|
474
|
+
> puts glaze_new.to_xml
|
475
|
+
# <glaze firingTime="60">
|
476
|
+
# <opacity>Opaque</opacity>
|
477
|
+
# </glaze>
|
478
|
+
> puts glaze_new.to_json
|
479
|
+
# {"firingTime":60,"opacity":"Opaque"}
|
480
|
+
----
|
481
|
+
====
|
482
|
+
|
483
|
+
.Attributes with `render_default: true` with non-default values are rendered
|
484
|
+
[example]
|
485
|
+
====
|
486
|
+
[source,ruby]
|
487
|
+
----
|
488
|
+
> glaze = Glaze.new(color: 'Celadon', opacity: 'Semitransparent', temperature: 1300, firing_time: 90)
|
489
|
+
> puts glaze.to_xml
|
490
|
+
# <glaze color="Celadon" temperature="1300" firingTime="90">
|
491
|
+
# <opacity>Semitransparent</opacity>
|
492
|
+
# </glaze>
|
493
|
+
> puts glaze.to_json
|
494
|
+
# {"color":"Celadon","temperature":1300,"firingTime":90,"opacity":"Semitransparent"}
|
495
|
+
----
|
496
|
+
====
|
497
|
+
|
498
|
+
|
499
|
+
|
426
500
|
=== Attribute as raw string
|
427
501
|
|
428
502
|
An attribute can be set to read the value as raw string for XML, by using the `raw: true` option.
|
@@ -668,8 +742,10 @@ end
|
|
668
742
|
> #<example value="12"></example>
|
669
743
|
----
|
670
744
|
|
671
|
-
The map_attribute method does not inherit the root element's namespace.
|
672
|
-
|
745
|
+
The `map_attribute` method does not inherit the root element's namespace.
|
746
|
+
To specify a namespace for an attribute, please explicitly declare the
|
747
|
+
*namespace* and *prefix* in the `map_attribute` method.
|
748
|
+
|
673
749
|
[example]
|
674
750
|
====
|
675
751
|
The following class will parse the XML snippet below:
|
@@ -1081,7 +1157,7 @@ end
|
|
1081
1157
|
----
|
1082
1158
|
====
|
1083
1159
|
|
1084
|
-
TODO: How to create mixed content from `#new`?
|
1160
|
+
// TODO: How to create mixed content from `#new`?
|
1085
1161
|
|
1086
1162
|
[[xml-schema-location]]
|
1087
1163
|
==== Automatic support of `xsi:schemaLocation`
|
@@ -2020,7 +2096,9 @@ Lutaml::Model::Config.configure do |config|
|
|
2020
2096
|
end
|
2021
2097
|
----
|
2022
2098
|
|
2023
|
-
NOTE: By default `yaml_adapter_type` and `json_adapter_type` are set to
|
2099
|
+
NOTE: By default `yaml_adapter_type` and `json_adapter_type` are set to
|
2100
|
+
`:standard_yaml` and `:standard_json` respectively.
|
2101
|
+
|
2024
2102
|
|
2025
2103
|
=== XML
|
2026
2104
|
|
@@ -3,10 +3,20 @@ module Lutaml
|
|
3
3
|
class Attribute
|
4
4
|
attr_reader :name, :type, :options
|
5
5
|
|
6
|
+
ALLOWED_OPTIONS = %i[
|
7
|
+
raw
|
8
|
+
default
|
9
|
+
delegate
|
10
|
+
collection
|
11
|
+
values
|
12
|
+
].freeze
|
13
|
+
|
6
14
|
def initialize(name, type, options = {})
|
7
15
|
@name = name
|
8
16
|
@type = cast_type(type)
|
9
17
|
@options = options
|
18
|
+
validate_options!
|
19
|
+
|
10
20
|
@raw = !!options[:raw]
|
11
21
|
|
12
22
|
if collection?
|
@@ -66,10 +76,6 @@ module Lutaml
|
|
66
76
|
cast_value(value)
|
67
77
|
end
|
68
78
|
|
69
|
-
def render_nil?
|
70
|
-
options.fetch(:render_nil, false)
|
71
|
-
end
|
72
|
-
|
73
79
|
def enum_values
|
74
80
|
@options.key?(:values) ? @options[:values] : []
|
75
81
|
end
|
@@ -203,6 +209,14 @@ module Lutaml
|
|
203
209
|
Lutaml::Model::Type.cast(value, type)
|
204
210
|
end
|
205
211
|
end
|
212
|
+
|
213
|
+
private
|
214
|
+
|
215
|
+
def validate_options!
|
216
|
+
if (options = @options.keys - ALLOWED_OPTIONS).any?
|
217
|
+
raise StandardError, "Invalid options given for `#{name}` #{options}"
|
218
|
+
end
|
219
|
+
end
|
206
220
|
end
|
207
221
|
end
|
208
222
|
end
|
@@ -13,6 +13,7 @@ module Lutaml
|
|
13
13
|
name,
|
14
14
|
to: nil,
|
15
15
|
render_nil: false,
|
16
|
+
render_default: false,
|
16
17
|
with: {},
|
17
18
|
delegate: nil,
|
18
19
|
child_mappings: nil
|
@@ -23,6 +24,7 @@ module Lutaml
|
|
23
24
|
name,
|
24
25
|
to: to,
|
25
26
|
render_nil: render_nil,
|
27
|
+
render_default: render_default,
|
26
28
|
with: with,
|
27
29
|
delegate: delegate,
|
28
30
|
child_mappings: child_mappings,
|
@@ -9,6 +9,7 @@ module Lutaml
|
|
9
9
|
name,
|
10
10
|
to:,
|
11
11
|
render_nil: false,
|
12
|
+
render_default: false,
|
12
13
|
with: {},
|
13
14
|
delegate: nil,
|
14
15
|
child_mappings: nil
|
@@ -17,6 +18,7 @@ module Lutaml
|
|
17
18
|
name,
|
18
19
|
to: to,
|
19
20
|
render_nil: render_nil,
|
21
|
+
render_default: render_default,
|
20
22
|
with: with,
|
21
23
|
delegate: delegate,
|
22
24
|
)
|
@@ -4,6 +4,8 @@ module Lutaml
|
|
4
4
|
attr_reader :name,
|
5
5
|
:to,
|
6
6
|
:render_nil,
|
7
|
+
:render_default,
|
8
|
+
:attribute,
|
7
9
|
:custom_methods,
|
8
10
|
:delegate
|
9
11
|
|
@@ -11,6 +13,7 @@ module Lutaml
|
|
11
13
|
name,
|
12
14
|
to:,
|
13
15
|
render_nil: false,
|
16
|
+
render_default: false,
|
14
17
|
with: {},
|
15
18
|
attribute: false,
|
16
19
|
delegate: nil
|
@@ -18,6 +21,7 @@ module Lutaml
|
|
18
21
|
@name = name
|
19
22
|
@to = to
|
20
23
|
@render_nil = render_nil
|
24
|
+
@render_default = render_default
|
21
25
|
@custom_methods = with
|
22
26
|
@attribute = attribute
|
23
27
|
@delegate = delegate
|
@@ -25,6 +29,8 @@ module Lutaml
|
|
25
29
|
|
26
30
|
alias from name
|
27
31
|
alias render_nil? render_nil
|
32
|
+
alias render_default? render_default
|
33
|
+
alias attribute? attribute
|
28
34
|
|
29
35
|
def serialize_attribute(model, element, doc)
|
30
36
|
if custom_methods[:to]
|
@@ -40,35 +40,30 @@ module Lutaml
|
|
40
40
|
def model(klass = nil)
|
41
41
|
if klass
|
42
42
|
@model = klass
|
43
|
-
|
43
|
+
add_custom_handling_methods_to_model(klass)
|
44
44
|
else
|
45
45
|
@model
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
def
|
50
|
-
Utils.
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
Utils.add_method_if_not_defined(klass, :ordered?) do
|
55
|
-
!!@ordered
|
56
|
-
end
|
57
|
-
|
58
|
-
Utils.add_method_if_not_defined(klass, :mixed=) do |mixed|
|
59
|
-
@mixed = mixed
|
60
|
-
end
|
49
|
+
def add_custom_handling_methods_to_model(klass)
|
50
|
+
Utils.add_boolean_accessor_if_not_defined(klass, :ordered)
|
51
|
+
Utils.add_boolean_accessor_if_not_defined(klass, :mixed)
|
52
|
+
Utils.add_accessor_if_not_defined(klass, :element_order)
|
61
53
|
|
62
|
-
Utils.add_method_if_not_defined(klass, :
|
63
|
-
|
54
|
+
Utils.add_method_if_not_defined(klass, :using_default_for) do |attribute_name|
|
55
|
+
@using_default ||= {}
|
56
|
+
@using_default[attribute_name] = true
|
64
57
|
end
|
65
58
|
|
66
|
-
Utils.add_method_if_not_defined(klass, :
|
67
|
-
@
|
59
|
+
Utils.add_method_if_not_defined(klass, :value_set_for) do |attribute_name|
|
60
|
+
@using_default ||= {}
|
61
|
+
@using_default[attribute_name] = false
|
68
62
|
end
|
69
63
|
|
70
|
-
Utils.add_method_if_not_defined(klass, :
|
71
|
-
@
|
64
|
+
Utils.add_method_if_not_defined(klass, :using_default?) do |attribute_name|
|
65
|
+
@using_default ||= {}
|
66
|
+
!!@using_default[attribute_name]
|
72
67
|
end
|
73
68
|
end
|
74
69
|
|
@@ -86,6 +81,7 @@ module Lutaml
|
|
86
81
|
end
|
87
82
|
|
88
83
|
define_method(:"#{name}=") do |value|
|
84
|
+
value_set_for(name)
|
89
85
|
instance_variable_set(:"@#{name}", attr.cast_value(value))
|
90
86
|
end
|
91
87
|
end
|
@@ -159,6 +155,7 @@ module Lutaml
|
|
159
155
|
mappings.each_with_object({}) do |rule, hash|
|
160
156
|
name = rule.to
|
161
157
|
next if except&.include?(name) || (only && !only.include?(name))
|
158
|
+
next if !rule.render_default? && instance.using_default?(rule.to)
|
162
159
|
|
163
160
|
next handle_delegate(instance, rule, hash, format) if rule.delegate
|
164
161
|
|
@@ -223,11 +220,10 @@ module Lutaml
|
|
223
220
|
klass = format == :xml ? XmlMapping : KeyValueMapping
|
224
221
|
|
225
222
|
klass.new.tap do |mapping|
|
226
|
-
attributes&.
|
223
|
+
attributes&.each_key do |name|
|
227
224
|
mapping.map_element(
|
228
225
|
name.to_s,
|
229
226
|
to: name,
|
230
|
-
render_nil: attr.render_nil?,
|
231
227
|
)
|
232
228
|
end
|
233
229
|
|
@@ -304,33 +300,7 @@ module Lutaml
|
|
304
300
|
return instance if Utils.blank?(doc)
|
305
301
|
return apply_xml_mapping(doc, instance, options) if format == :xml
|
306
302
|
|
307
|
-
|
308
|
-
mappings.each do |rule|
|
309
|
-
raise "Attribute '#{rule.to}' not found in #{self}" unless valid_rule?(rule)
|
310
|
-
|
311
|
-
attr = attribute_for_rule(rule)
|
312
|
-
|
313
|
-
value = if doc.key?(rule.name) || doc.key?(rule.name.to_sym)
|
314
|
-
doc[rule.name] || doc[rule.name.to_sym]
|
315
|
-
else
|
316
|
-
attr.default
|
317
|
-
end
|
318
|
-
|
319
|
-
if rule.custom_methods[:from]
|
320
|
-
if Utils.present?(value)
|
321
|
-
value = new.send(rule.custom_methods[:from], instance, value)
|
322
|
-
end
|
323
|
-
|
324
|
-
next
|
325
|
-
end
|
326
|
-
|
327
|
-
value = apply_child_mappings(value, rule.child_mappings)
|
328
|
-
value = attr.cast(value, format)
|
329
|
-
|
330
|
-
rule.deserialize(instance, value, attributes, self)
|
331
|
-
end
|
332
|
-
|
333
|
-
instance
|
303
|
+
apply_hash_mapping(doc, instance, format, options)
|
334
304
|
end
|
335
305
|
|
336
306
|
def apply_xml_mapping(doc, instance, options = {})
|
@@ -357,6 +327,8 @@ module Lutaml
|
|
357
327
|
)
|
358
328
|
end
|
359
329
|
|
330
|
+
defaults_used = []
|
331
|
+
|
360
332
|
mappings.each do |rule|
|
361
333
|
raise "Attribute '#{rule.to}' not found in #{self}" unless valid_rule?(rule)
|
362
334
|
|
@@ -365,6 +337,7 @@ module Lutaml
|
|
365
337
|
elsif doc.key_exist?(rule.namespaced_name)
|
366
338
|
doc.fetch(rule.namespaced_name)
|
367
339
|
else
|
340
|
+
defaults_used << rule.to
|
368
341
|
rule.to_value_for(instance)
|
369
342
|
end
|
370
343
|
|
@@ -372,6 +345,40 @@ module Lutaml
|
|
372
345
|
rule.deserialize(instance, value, attributes, self)
|
373
346
|
end
|
374
347
|
|
348
|
+
defaults_used.each do |attribute_name|
|
349
|
+
instance.using_default_for(attribute_name)
|
350
|
+
end
|
351
|
+
|
352
|
+
instance
|
353
|
+
end
|
354
|
+
|
355
|
+
def apply_hash_mapping(doc, instance, format, _options = {})
|
356
|
+
mappings = mappings_for(format).mappings
|
357
|
+
mappings.each do |rule|
|
358
|
+
raise "Attribute '#{rule.to}' not found in #{self}" unless valid_rule?(rule)
|
359
|
+
|
360
|
+
attr = attribute_for_rule(rule)
|
361
|
+
|
362
|
+
value = if doc.key?(rule.name) || doc.key?(rule.name.to_sym)
|
363
|
+
doc[rule.name] || doc[rule.name.to_sym]
|
364
|
+
else
|
365
|
+
attr.default
|
366
|
+
end
|
367
|
+
|
368
|
+
if rule.custom_methods[:from]
|
369
|
+
if Utils.present?(value)
|
370
|
+
value = new.send(rule.custom_methods[:from], instance, value)
|
371
|
+
end
|
372
|
+
|
373
|
+
next
|
374
|
+
end
|
375
|
+
|
376
|
+
value = apply_child_mappings(value, rule.child_mappings)
|
377
|
+
value = attr.cast(value, format)
|
378
|
+
|
379
|
+
rule.deserialize(instance, value, attributes, self)
|
380
|
+
end
|
381
|
+
|
375
382
|
instance
|
376
383
|
end
|
377
384
|
|
@@ -433,6 +440,7 @@ module Lutaml
|
|
433
440
|
|
434
441
|
def initialize(attrs = {})
|
435
442
|
@validate_on_set = attrs.delete(:validate_on_set) || false
|
443
|
+
@using_default ||= {}
|
436
444
|
|
437
445
|
return unless self.class.attributes
|
438
446
|
|
@@ -449,6 +457,7 @@ module Lutaml
|
|
449
457
|
value = if attrs.key?(name) || attrs.key?(name.to_s)
|
450
458
|
self.class.attr_value(attrs, name, attr)
|
451
459
|
else
|
460
|
+
using_default_for(name)
|
452
461
|
attr.default
|
453
462
|
end
|
454
463
|
|
@@ -461,6 +470,18 @@ module Lutaml
|
|
461
470
|
end
|
462
471
|
end
|
463
472
|
|
473
|
+
def using_default_for(attribute_name)
|
474
|
+
@using_default[attribute_name] = true
|
475
|
+
end
|
476
|
+
|
477
|
+
def value_set_for(attribute_name)
|
478
|
+
@using_default[attribute_name] = false
|
479
|
+
end
|
480
|
+
|
481
|
+
def using_default?(attribute_name)
|
482
|
+
@using_default[attribute_name]
|
483
|
+
end
|
484
|
+
|
464
485
|
def method_missing(method_name, *args)
|
465
486
|
if method_name.to_s.end_with?("=") && self.class.attributes.key?(method_name.to_s.chomp("=").to_sym)
|
466
487
|
define_singleton_method(method_name) do |value|
|
data/lib/lutaml/model/utils.rb
CHANGED
@@ -50,6 +50,34 @@ module Lutaml
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
+
def add_accessor_if_not_defined(klass, attribute)
|
54
|
+
add_getter_if_not_defined(klass, attribute)
|
55
|
+
add_setter_if_not_defined(klass, attribute)
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_boolean_accessor_if_not_defined(klass, attribute)
|
59
|
+
add_boolean_getter_if_not_defined(klass, attribute)
|
60
|
+
add_setter_if_not_defined(klass, attribute)
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_getter_if_not_defined(klass, attribute)
|
64
|
+
add_method_if_not_defined(klass, attribute) do
|
65
|
+
instance_variable_get(:"@__#{attribute}")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def add_boolean_getter_if_not_defined(klass, attribute)
|
70
|
+
add_method_if_not_defined(klass, "#{attribute}?") do
|
71
|
+
!!instance_variable_get(:"@__#{attribute}")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def add_setter_if_not_defined(klass, attribute)
|
76
|
+
add_method_if_not_defined(klass, "#{attribute}=") do |value|
|
77
|
+
instance_variable_set(:"@__#{attribute}", value)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
53
81
|
def deep_dup(hash)
|
54
82
|
return hash if hash.nil?
|
55
83
|
|
data/lib/lutaml/model/version.rb
CHANGED
@@ -143,6 +143,8 @@ module Lutaml
|
|
143
143
|
return
|
144
144
|
end
|
145
145
|
|
146
|
+
return if !render_element?(rule, element, value)
|
147
|
+
|
146
148
|
if value && (attribute&.type&.<= Lutaml::Model::Serialize)
|
147
149
|
handle_nested_elements(
|
148
150
|
xml,
|
@@ -254,6 +256,20 @@ module Lutaml
|
|
254
256
|
rule.nil? || !rule.namespace_set? || !rule.namespace.nil?
|
255
257
|
end
|
256
258
|
|
259
|
+
def render_element?(rule, element, value)
|
260
|
+
render_default?(rule, element) && render_value?(rule, value)
|
261
|
+
end
|
262
|
+
|
263
|
+
def render_value?(rule, value)
|
264
|
+
rule.attribute? || rule.render_nil? || !value.nil?
|
265
|
+
end
|
266
|
+
|
267
|
+
def render_default?(rule, element)
|
268
|
+
!element.respond_to?(:using_default?) ||
|
269
|
+
rule.render_default? ||
|
270
|
+
!element.using_default?(rule.to)
|
271
|
+
end
|
272
|
+
|
257
273
|
def build_namespace_attributes(klass, processed = {}, options = {})
|
258
274
|
xml_mappings = klass.mappings_for(:xml)
|
259
275
|
attributes = klass.attributes
|
@@ -315,7 +331,9 @@ module Lutaml
|
|
315
331
|
hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace
|
316
332
|
end
|
317
333
|
|
318
|
-
|
334
|
+
if render_element?(mapping_rule, element, mapping_rule.to_value_for(element))
|
335
|
+
hash[mapping_rule.prefixed_name] = mapping_rule.to_value_for(element)
|
336
|
+
end
|
319
337
|
end
|
320
338
|
|
321
339
|
xml_mapping.elements.each_with_object(attrs) do |mapping_rule, hash|
|
@@ -43,6 +43,7 @@ module Lutaml
|
|
43
43
|
name,
|
44
44
|
to: nil,
|
45
45
|
render_nil: false,
|
46
|
+
render_default: false,
|
46
47
|
with: {},
|
47
48
|
delegate: nil,
|
48
49
|
namespace: (namespace_set = false
|
@@ -56,6 +57,7 @@ module Lutaml
|
|
56
57
|
name,
|
57
58
|
to: to,
|
58
59
|
render_nil: render_nil,
|
60
|
+
render_default: render_default,
|
59
61
|
with: with,
|
60
62
|
delegate: delegate,
|
61
63
|
namespace: namespace,
|
@@ -71,6 +73,7 @@ module Lutaml
|
|
71
73
|
name,
|
72
74
|
to: nil,
|
73
75
|
render_nil: false,
|
76
|
+
render_default: false,
|
74
77
|
with: {},
|
75
78
|
delegate: nil,
|
76
79
|
namespace: (namespace_set = false
|
@@ -84,6 +87,7 @@ module Lutaml
|
|
84
87
|
name,
|
85
88
|
to: to,
|
86
89
|
render_nil: render_nil,
|
90
|
+
render_default: render_default,
|
87
91
|
with: with,
|
88
92
|
delegate: delegate,
|
89
93
|
namespace: namespace,
|
@@ -101,6 +105,7 @@ module Lutaml
|
|
101
105
|
def map_content(
|
102
106
|
to: nil,
|
103
107
|
render_nil: false,
|
108
|
+
render_default: false,
|
104
109
|
with: {},
|
105
110
|
delegate: nil,
|
106
111
|
mixed: false
|
@@ -111,6 +116,7 @@ module Lutaml
|
|
111
116
|
nil,
|
112
117
|
to: to,
|
113
118
|
render_nil: render_nil,
|
119
|
+
render_default: render_default,
|
114
120
|
with: with,
|
115
121
|
delegate: delegate,
|
116
122
|
mixed_content: mixed,
|
@@ -9,6 +9,7 @@ module Lutaml
|
|
9
9
|
name,
|
10
10
|
to:,
|
11
11
|
render_nil: false,
|
12
|
+
render_default: false,
|
12
13
|
with: {},
|
13
14
|
delegate: nil,
|
14
15
|
namespace: nil,
|
@@ -23,6 +24,7 @@ module Lutaml
|
|
23
24
|
name,
|
24
25
|
to: to,
|
25
26
|
render_nil: render_nil,
|
27
|
+
render_default: render_default,
|
26
28
|
with: with,
|
27
29
|
delegate: delegate,
|
28
30
|
attribute: attribute,
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lutaml-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ribose Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|