lutaml-model 0.3.14 → 0.3.15

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
  SHA256:
3
- metadata.gz: 5b36d07a850d4736c0634735a198f05dd34c49b906cd1265b3dd49266931eb22
4
- data.tar.gz: cefd0559ad4010ecd2ab0a547ab87493b4958a480f6cb19ab317c248836b8f75
3
+ metadata.gz: 59ac2ffbc292a5e34dbbbf79441a8220a48954ba5acd21d2780235281bfbbee2
4
+ data.tar.gz: 253892694acb1359120e6401d83e05bc2daa2f371b69f667210c048c08f2336c
5
5
  SHA512:
6
- metadata.gz: 83fac7632d0eec4f5bd4339132b36d267877932cf34839eac6dbdefd0cd0e551988b73b3cd379b856af0df351e42694c157342efd8baba67e283e71d34b88171
7
- data.tar.gz: 6befe6bfce13fb6175a04f8e0ac81794304d3c56c51feea343334c501346cba517db1f89ab3387ea3300e546d160d24af264048e9762c85debc4063300d57025
6
+ metadata.gz: 920de33ce2cc20336fa2ed87c3a2d5743f4016c8a400a881c91e4eafee0452e3fdc110429f3e71b7216f84694e546b26848c50323bc25397d32c8a1aa2e6901e
7
+ data.tar.gz: 3685a35624c0c355ee457241a308bf9e46c37c9a7b14994948a897c9210a13229c2a2832d8f595b2d83a4bf96660f1ea2b9843c83ea8ba9a73978ef67e864818
data/.rubocop_todo.yml CHANGED
@@ -72,7 +72,7 @@ Metrics/MethodLength:
72
72
  # Offense count: 4
73
73
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
74
74
  Metrics/ParameterLists:
75
- Max: 12
75
+ Max: 13
76
76
 
77
77
  # Offense count: 22
78
78
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
@@ -54,6 +54,10 @@ module Lutaml
54
54
  @raw
55
55
  end
56
56
 
57
+ def render_default?
58
+ !!@options[:render_default]
59
+ end
60
+
57
61
  def default
58
62
  value = if delegate
59
63
  type.attributes[to].default
@@ -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]
@@ -86,6 +86,7 @@ module Lutaml
86
86
  end
87
87
 
88
88
  define_method(:"#{name}=") do |value|
89
+ value_set_for(name)
89
90
  instance_variable_set(:"@#{name}", attr.cast_value(value))
90
91
  end
91
92
  end
@@ -304,33 +305,7 @@ module Lutaml
304
305
  return instance if Utils.blank?(doc)
305
306
  return apply_xml_mapping(doc, instance, options) if format == :xml
306
307
 
307
- mappings = mappings_for(format).mappings
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
308
+ apply_hash_mapping(doc, instance, format, options)
334
309
  end
335
310
 
336
311
  def apply_xml_mapping(doc, instance, options = {})
@@ -357,6 +332,8 @@ module Lutaml
357
332
  )
358
333
  end
359
334
 
335
+ defaults_used = []
336
+
360
337
  mappings.each do |rule|
361
338
  raise "Attribute '#{rule.to}' not found in #{self}" unless valid_rule?(rule)
362
339
 
@@ -365,6 +342,7 @@ module Lutaml
365
342
  elsif doc.key_exist?(rule.namespaced_name)
366
343
  doc.fetch(rule.namespaced_name)
367
344
  else
345
+ defaults_used << rule.to
368
346
  rule.to_value_for(instance)
369
347
  end
370
348
 
@@ -372,6 +350,40 @@ module Lutaml
372
350
  rule.deserialize(instance, value, attributes, self)
373
351
  end
374
352
 
353
+ defaults_used.each do |attribute_name|
354
+ instance.using_default_for(attribute_name) if instance.respond_to?(:using_default_for)
355
+ end
356
+
357
+ instance
358
+ end
359
+
360
+ def apply_hash_mapping(doc, instance, format, _options = {})
361
+ mappings = mappings_for(format).mappings
362
+ mappings.each do |rule|
363
+ raise "Attribute '#{rule.to}' not found in #{self}" unless valid_rule?(rule)
364
+
365
+ attr = attribute_for_rule(rule)
366
+
367
+ value = if doc.key?(rule.name) || doc.key?(rule.name.to_sym)
368
+ doc[rule.name] || doc[rule.name.to_sym]
369
+ else
370
+ attr.default
371
+ end
372
+
373
+ if rule.custom_methods[:from]
374
+ if Utils.present?(value)
375
+ value = new.send(rule.custom_methods[:from], instance, value)
376
+ end
377
+
378
+ next
379
+ end
380
+
381
+ value = apply_child_mappings(value, rule.child_mappings)
382
+ value = attr.cast(value, format)
383
+
384
+ rule.deserialize(instance, value, attributes, self)
385
+ end
386
+
375
387
  instance
376
388
  end
377
389
 
@@ -433,6 +445,7 @@ module Lutaml
433
445
 
434
446
  def initialize(attrs = {})
435
447
  @validate_on_set = attrs.delete(:validate_on_set) || false
448
+ @using_default ||= {}
436
449
 
437
450
  return unless self.class.attributes
438
451
 
@@ -449,6 +462,7 @@ module Lutaml
449
462
  value = if attrs.key?(name) || attrs.key?(name.to_s)
450
463
  self.class.attr_value(attrs, name, attr)
451
464
  else
465
+ using_default_for(name)
452
466
  attr.default
453
467
  end
454
468
 
@@ -461,6 +475,18 @@ module Lutaml
461
475
  end
462
476
  end
463
477
 
478
+ def using_default_for(attribute_name)
479
+ @using_default[attribute_name] = true
480
+ end
481
+
482
+ def value_set_for(attribute_name)
483
+ @using_default[attribute_name] = false
484
+ end
485
+
486
+ def using_default?(attribute_name)
487
+ @using_default[attribute_name]
488
+ end
489
+
464
490
  def method_missing(method_name, *args)
465
491
  if method_name.to_s.end_with?("=") && self.class.attributes.key?(method_name.to_s.chomp("=").to_sym)
466
492
  define_singleton_method(method_name) do |value|
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.3.14"
5
+ VERSION = "0.3.15"
6
6
  end
7
7
  end
@@ -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
- hash[mapping_rule.prefixed_name] = mapping_rule.to_value_for(element)
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.14
4
+ version: 0.3.15
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-24 00:00:00.000000000 Z
11
+ date: 2024-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor