lutaml-model 0.3.4 → 0.3.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d80c5eb43638cf5d690c6c59db2fba76ddb8e2fc7252117a3c7933bc566b2607
4
- data.tar.gz: d75c694c330c017500dcceb604aaa889633dadb53c7766a4b6310ee3aef1725e
3
+ metadata.gz: 55348c33e2e23ce7c53c33e588621b8e73df49c5ca4e82e70c8216ad8f0cc6ab
4
+ data.tar.gz: dd39d10a7f036596de8bce7f154197d446d065c76deee5a0b334522b8bbfdcea
5
5
  SHA512:
6
- metadata.gz: fc9ed09d01c0c19b4038af7de38c3c458f7c200f7b5ae509f4e4cfc2d3abe116ad862168b7d2ffef16e0d5205c67b3a54226b8b826f9859a72ec5d32da00545e
7
- data.tar.gz: b4b8348522112cce9236dc7989f30b42df22ac04d6a7459b2fff6ad6c6dc7018d4bd88e97ec9f83ab8b2dcf6d92938d18fcdc882914240d3d53fb16e0ba116d3
6
+ metadata.gz: 46d78508e5cd397bf19c79c4dfba9bb49835945ae6ae83744588bc134c7809988716ea83c2f83485efdf50bf4a31e8be4daafbf6ada78a22dbfa90e14def72ba
7
+ data.tar.gz: 05005dc4d4e6309913e706f38bf925c0ac77533728b34b89f8a69c901ea728d53bddf163e377ec7638be145ebf7bf7eb77f2d3c3ff078d7e7feb9ad6e52ff14f
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2024-08-16 11:07:41 UTC using RuboCop version 1.65.1.
3
+ # on 2024-08-17 14:46:05 UTC using RuboCop version 1.64.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
@@ -14,7 +14,7 @@ Gemspec/RequireMFA:
14
14
  Exclude:
15
15
  - 'lutaml-model.gemspec'
16
16
 
17
- # Offense count: 76
17
+ # Offense count: 77
18
18
  # This cop supports safe autocorrection (--autocorrect).
19
19
  # Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
20
20
  # URISchemes: http, https
@@ -55,11 +55,11 @@ Metrics/AbcSize:
55
55
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
56
56
  - 'lib/lutaml/model/xml_adapter/xml_document.rb'
57
57
 
58
- # Offense count: 3
58
+ # Offense count: 4
59
59
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
60
60
  # AllowedMethods: refine
61
61
  Metrics/BlockLength:
62
- Max: 30
62
+ Max: 31
63
63
 
64
64
  # Offense count: 23
65
65
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
@@ -75,7 +75,7 @@ Metrics/CyclomaticComplexity:
75
75
  # Offense count: 38
76
76
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
77
77
  Metrics/MethodLength:
78
- Max: 41
78
+ Max: 52
79
79
 
80
80
  # Offense count: 4
81
81
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
@@ -102,7 +102,7 @@ RSpec/ContextWording:
102
102
  - 'spec/lutaml/model/xml_adapter/oga_adapter_spec.rb'
103
103
  - 'spec/lutaml/model/xml_adapter/ox_adapter_spec.rb'
104
104
 
105
- # Offense count: 70
105
+ # Offense count: 72
106
106
  # Configuration parameters: CountAsOne.
107
107
  RSpec/ExampleLength:
108
108
  Max: 57
@@ -129,7 +129,7 @@ RSpec/MultipleDescribes:
129
129
  - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
130
130
  - 'spec/lutaml/model/xml_adapter_spec.rb'
131
131
 
132
- # Offense count: 69
132
+ # Offense count: 70
133
133
  RSpec/MultipleExpectations:
134
134
  Max: 15
135
135
 
data/README.adoc CHANGED
@@ -1291,10 +1291,30 @@ each serialization mapping block for `from` and `to`.
1291
1291
 
1292
1292
  Syntax:
1293
1293
 
1294
+ .XML serialization with custom methods
1294
1295
  [source,ruby]
1295
1296
  ----
1296
- xml | json | yaml | toml do
1297
- map 'key_value_model_attribute_name', to: :name_of_attribute, with: {
1297
+ xml do
1298
+ map_element 'element_name', to: :name_of_element, with: {
1299
+ to: :method_name_to_serialize,
1300
+ from: :method_name_to_deserialize
1301
+ }
1302
+ map_attribute 'attribute_name', to: :name_of_attribute, with: {
1303
+ to: :method_name_to_serialize,
1304
+ from: :method_name_to_deserialize
1305
+ }
1306
+ map_content, to: :name_of_content, with: {
1307
+ to: :method_name_to_serialize,
1308
+ from: :method_name_to_deserialize
1309
+ }
1310
+ end
1311
+ ----
1312
+
1313
+ .Key-value data model serialization with custom methods
1314
+ [source,ruby]
1315
+ ----
1316
+ json | yaml | toml do
1317
+ map 'attribute_name', to: :name_of_attribute, with: {
1298
1318
  to: :method_name_to_serialize,
1299
1319
  from: :method_name_to_deserialize
1300
1320
  }
@@ -210,9 +210,7 @@ module Lutaml
210
210
  else
211
211
  # TODO: This code is problematic because Type.cast does not know
212
212
  # about all the types.
213
- Lutaml::Model::Type.cast(
214
- v, attr_rule.type
215
- )
213
+ Lutaml::Model::Type.cast(v, attr_rule.type)
216
214
  end
217
215
  end
218
216
  elsif value.is_a?(Hash) && attr_rule.type != Lutaml::Model::Type::Hash
@@ -276,7 +274,7 @@ module Lutaml
276
274
  end.public_send(:[]=, path.last.to_s, child_obj.send(attr_name))
277
275
  end
278
276
  end
279
- # hash[mapping.name] ||= {}
277
+
280
278
  hash[map_key] = map_value
281
279
  end
282
280
 
@@ -337,6 +335,8 @@ module Lutaml
337
335
  mapping_hash.item_order = doc.item_order
338
336
  mapping_hash.ordered = mappings_for(:xml).mixed_content? || mixed_content
339
337
 
338
+ mapping_from = []
339
+
340
340
  mappings.each_with_object(mapping_hash) do |rule, hash|
341
341
  attr = attributes[rule.to]
342
342
  raise "Attribute '#{rule.to}' not found in #{self}" unless attr
@@ -372,8 +372,22 @@ module Lutaml
372
372
  value = attr.type.cast(value) unless is_content_mapping
373
373
  end
374
374
 
375
+ mapping_from << rule if rule.custom_methods[:from]
376
+
375
377
  hash[rule.to] = value
376
378
  end
379
+
380
+ mapping_from.each do |rule|
381
+ value = if rule.name.nil?
382
+ mapping_hash[rule.to].join("\n").strip
383
+ else
384
+ mapping_hash[rule.to]
385
+ end
386
+
387
+ mapping_hash[rule.to] = new.send(rule.custom_methods[:from], mapping_hash, value)
388
+ end
389
+
390
+ mapping_hash
377
391
  end
378
392
 
379
393
  def ensure_utf8(value)
@@ -385,7 +399,9 @@ module Lutaml
385
399
  when Hash
386
400
  value.transform_keys do |k|
387
401
  ensure_utf8(k)
388
- end.transform_values { |v| ensure_utf8(v) }
402
+ end.transform_values do |v|
403
+ ensure_utf8(v)
404
+ end
389
405
  else
390
406
  value
391
407
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.3.4"
5
+ VERSION = "0.3.5"
6
6
  end
7
7
  end
@@ -71,10 +71,14 @@ module Lutaml
71
71
  end
72
72
  end
73
73
 
74
- if xml_mapping.content_mapping
75
- text = element.send(xml_mapping.content_mapping.to)
74
+ if (content_rule = xml_mapping.content_mapping)
75
+ text = element.send(content_rule.to)
76
76
  text = text.join if text.is_a?(Array)
77
77
 
78
+ if content_rule.custom_methods[:to]
79
+ text = @root.send(content_rule.custom_methods[:to], @root, text)
80
+ end
81
+
78
82
  prefixed_xml.text text
79
83
  end
80
84
  end
@@ -130,6 +134,10 @@ module Lutaml
130
134
  end
131
135
 
132
136
  def add_to_xml(xml, value, attribute, rule)
137
+ if rule.custom_methods[:to]
138
+ value = @root.send(rule.custom_methods[:to], @root, value)
139
+ end
140
+
133
141
  if value && (attribute&.type&.<= Lutaml::Model::Serialize)
134
142
  handle_nested_elements(
135
143
  xml,
@@ -69,10 +69,14 @@ module Lutaml
69
69
  end
70
70
  end
71
71
 
72
- if xml_mapping.content_mapping
72
+ if (content_rule = xml_mapping.content_mapping)
73
73
  text = element.send(xml_mapping.content_mapping.to)
74
74
  text = text.join if text.is_a?(Array)
75
75
 
76
+ if content_rule.custom_methods[:to]
77
+ text = @root.send(content_rule.custom_methods[:to], @root, text)
78
+ end
79
+
76
80
  el.text text
77
81
  end
78
82
  end
@@ -113,6 +117,10 @@ module Lutaml
113
117
  end
114
118
 
115
119
  def add_to_xml(xml, value, attribute, rule)
120
+ if rule.custom_methods[:to]
121
+ value = @root.send(rule.custom_methods[:to], @root, value)
122
+ end
123
+
116
124
  if value && (attribute&.type&.<= Lutaml::Model::Serialize)
117
125
  handle_nested_elements(
118
126
  xml,
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
4
+ version: 0.3.5
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-08-16 00:00:00.000000000 Z
11
+ date: 2024-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor