lutaml-model 0.3.7 → 0.3.8

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: 8362f4af3671083805d20014ef7e2422358aac08f1c15036a283dbedb91225ca
4
- data.tar.gz: 661838fcd61e972cd1827a1b4889b3d2073e7a8db16a4fce371dcbf09fb79b96
3
+ metadata.gz: 30487a24277470ff7b3d12193e46262b0fc1cac85461224ed98749c8b6de5104
4
+ data.tar.gz: a161a8e5f83a10ec5db5b77a0d5a0abe72b629d0900a4ab040414c2a02c22725
5
5
  SHA512:
6
- metadata.gz: 31757feb85022ff8472b8e178ff433a84e2dcbd7a00b6cc2055f3f4d860b4470201c2ee6c39d2a2b96cc86d96ec3654affd7078bb2cad595345afa50fec9826d
7
- data.tar.gz: ff6f2048f11d46a9e4df1ec872bca13e5ebcbbc098aa35689840e3601d18337e2e7a85afd48f8a67b1b39482782258cdc1494f13eb1cd67eac8ac37cc2f5809d
6
+ metadata.gz: 2a805f81f93bc0dcb983d83e7f7fc870989f84fb25434de7edee18269c1b96b8572516132e17e3db2e188075e985cb8219ec84257fd380c7abfe4b65b32116af
7
+ data.tar.gz: 0243f89d6fb6c5466e29500e810a077096e260b51d53a8a23db7918c7b2d57dc30a9757dd00814a2c497dd552e39bd17e92729c509670dde555a0b508a77013b
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-23 04:30:59 UTC using RuboCop version 1.65.1.
3
+ # on 2024-08-27 06:48:03 UTC using RuboCop version 1.65.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,76 +14,7 @@ Gemspec/RequireMFA:
14
14
  Exclude:
15
15
  - 'lutaml-model.gemspec'
16
16
 
17
- # Offense count: 28
18
- # This cop supports safe autocorrection (--autocorrect).
19
- # Configuration parameters: EnforcedStyle, IndentationWidth.
20
- # SupportedStyles: with_first_argument, with_fixed_indentation
21
- Layout/ArgumentAlignment:
22
- Exclude:
23
- - 'lib/lutaml/model/serialize.rb'
24
- - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
25
- - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
26
- - 'lib/lutaml/model/xml_adapter/xml_document.rb'
27
- - 'spec/lutaml/model/serializable_spec.rb'
28
-
29
- # Offense count: 1
30
- # This cop supports safe autocorrection (--autocorrect).
31
- # Configuration parameters: IndentationWidth.
32
- Layout/AssignmentIndentation:
33
- Exclude:
34
- - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
35
-
36
- # Offense count: 6
37
- # This cop supports safe autocorrection (--autocorrect).
38
- # Configuration parameters: EnforcedStyleAlignWith.
39
- # SupportedStylesAlignWith: either, start_of_block, start_of_line
40
- Layout/BlockAlignment:
41
- Exclude:
42
- - 'lib/lutaml/model/xml_adapter/xml_document.rb'
43
- - 'spec/lutaml/model/serializable_spec.rb'
44
-
45
- # Offense count: 6
46
- # This cop supports safe autocorrection (--autocorrect).
47
- Layout/BlockEndNewline:
48
- Exclude:
49
- - 'lib/lutaml/model/xml_adapter/xml_document.rb'
50
- - 'spec/lutaml/model/serializable_spec.rb'
51
-
52
- # Offense count: 1
53
- # This cop supports safe autocorrection (--autocorrect).
54
- Layout/EmptyLineAfterGuardClause:
55
- Exclude:
56
- - 'lib/lutaml/model/serialize.rb'
57
-
58
- # Offense count: 16
59
- # This cop supports safe autocorrection (--autocorrect).
60
- # Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
61
- # SupportedHashRocketStyles: key, separator, table
62
- # SupportedColonStyles: key, separator, table
63
- # SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit
64
- Layout/HashAlignment:
65
- Exclude:
66
- - 'lib/lutaml/model/serialize.rb'
67
- - 'lib/lutaml/model/xml_adapter/xml_document.rb'
68
- - 'spec/lutaml/model/serializable_spec.rb'
69
-
70
- # Offense count: 1
71
- # This cop supports safe autocorrection (--autocorrect).
72
- # Configuration parameters: EnforcedStyle.
73
- # SupportedStyles: normal, indented_internal_methods
74
- Layout/IndentationConsistency:
75
- Exclude:
76
- - 'lutaml-model.gemspec'
77
-
78
- # Offense count: 12
79
- # This cop supports safe autocorrection (--autocorrect).
80
- # Configuration parameters: Width, AllowedPatterns.
81
- Layout/IndentationWidth:
82
- Exclude:
83
- - 'lib/lutaml/model/xml_adapter/xml_document.rb'
84
- - 'spec/lutaml/model/serializable_spec.rb'
85
-
86
- # Offense count: 83
17
+ # Offense count: 57
87
18
  # This cop supports safe autocorrection (--autocorrect).
88
19
  # Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
89
20
  # URISchemes: http, https
@@ -103,17 +34,6 @@ Layout/LineLength:
103
34
  - 'spec/lutaml/model/schema/json_schema_spec.rb'
104
35
  - 'spec/lutaml/model/serializable_spec.rb'
105
36
 
106
- # Offense count: 24
107
- # This cop supports safe autocorrection (--autocorrect).
108
- # Configuration parameters: AllowInHeredoc.
109
- Layout/TrailingWhitespace:
110
- Exclude:
111
- - 'lib/lutaml/model/serialize.rb'
112
- - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
113
- - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
114
- - 'lib/lutaml/model/xml_adapter/xml_document.rb'
115
- - 'spec/lutaml/model/serializable_spec.rb'
116
-
117
37
  # Offense count: 4
118
38
  # Configuration parameters: AllowedMethods.
119
39
  # AllowedMethods: enums
@@ -141,12 +61,13 @@ Metrics/AbcSize:
141
61
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
142
62
  # AllowedMethods: refine
143
63
  Metrics/BlockLength:
144
- Max: 29
64
+ Max: 43
145
65
 
146
- # Offense count: 20
66
+ # Offense count: 22
147
67
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
148
68
  Metrics/CyclomaticComplexity:
149
69
  Exclude:
70
+ - 'lib/lutaml/model/attribute.rb'
150
71
  - 'lib/lutaml/model/comparable_model.rb'
151
72
  - 'lib/lutaml/model/serialize.rb'
152
73
  - 'lib/lutaml/model/type.rb'
@@ -154,20 +75,21 @@ Metrics/CyclomaticComplexity:
154
75
  - 'lib/lutaml/model/xml_adapter/ox_adapter.rb'
155
76
  - 'lib/lutaml/model/xml_adapter/xml_document.rb'
156
77
 
157
- # Offense count: 37
78
+ # Offense count: 36
158
79
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
159
80
  Metrics/MethodLength:
160
- Max: 45
81
+ Max: 43
161
82
 
162
83
  # Offense count: 4
163
84
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
164
85
  Metrics/ParameterLists:
165
86
  Max: 9
166
87
 
167
- # Offense count: 16
88
+ # Offense count: 18
168
89
  # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
169
90
  Metrics/PerceivedComplexity:
170
91
  Exclude:
92
+ - 'lib/lutaml/model/attribute.rb'
171
93
  - 'lib/lutaml/model/comparable_model.rb'
172
94
  - 'lib/lutaml/model/serialize.rb'
173
95
  - 'lib/lutaml/model/xml_adapter/nokogiri_adapter.rb'
@@ -239,35 +161,9 @@ Security/CompoundHash:
239
161
  Exclude:
240
162
  - 'lib/lutaml/model/comparable_model.rb'
241
163
 
242
- # Offense count: 10
243
- # This cop supports safe autocorrection (--autocorrect).
244
- # Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, AllowedMethods, AllowedPatterns, AllowBracesOnProceduralOneLiners, BracesRequiredMethods.
245
- # SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces
246
- # ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object
247
- # FunctionalMethods: let, let!, subject, watch
248
- # AllowedMethods: lambda, proc, it
249
- Style/BlockDelimiters:
250
- Exclude:
251
- - 'lib/lutaml/model/xml_adapter/xml_document.rb'
252
- - 'spec/lutaml/model/serializable_spec.rb'
253
-
254
- # Offense count: 2
255
- # This cop supports safe autocorrection (--autocorrect).
256
- Style/MultilineIfModifier:
257
- Exclude:
258
- - 'lib/lutaml/model/serialize.rb'
259
-
260
164
  # Offense count: 1
261
165
  # Configuration parameters: AllowedMethods.
262
166
  # AllowedMethods: respond_to_missing?
263
167
  Style/OptionalBooleanParameter:
264
168
  Exclude:
265
169
  - 'lib/lutaml/model/comparable_model.rb'
266
-
267
- # Offense count: 2
268
- # This cop supports safe autocorrection (--autocorrect).
269
- # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
270
- # SupportedStyles: single_quotes, double_quotes
271
- Style/StringLiterals:
272
- Exclude:
273
- - 'lutaml-model.gemspec'
@@ -61,7 +61,7 @@ module Lutaml
61
61
  value.map do |v|
62
62
  cast(v, format, instance: instance)
63
63
  end
64
- elsif type <= Serialize
64
+ elsif type <= Serialize && value.is_a?(Hash)
65
65
  type.apply_mappings(value, format, options)
66
66
  else
67
67
  Lutaml::Model::Type.cast(value, type)
@@ -84,28 +84,47 @@ module Lutaml
84
84
 
85
85
  define_method(:"from_#{format}") do |data|
86
86
  adapter = Lutaml::Model::Config.send(:"#{format}_adapter")
87
- doc = adapter.parse(data)
88
87
 
89
- apply_mappings(doc.to_h, format)
88
+ doc = adapter.parse(data)
89
+ public_send(:"of_#{format}", doc.to_h)
90
90
  end
91
91
 
92
- define_method(:"to_#{format}") do |instance|
93
- unless instance.is_a?(model)
94
- msg = "argument is a '#{instance.class}' but should be a '#{model}'"
95
- raise Lutaml::Model::IncorrectModelError, msg
92
+ define_method(:"of_#{format}") do |hash|
93
+ if hash.is_a?(Array)
94
+ return hash.map do |item|
95
+ apply_mappings(item, format)
96
+ end
96
97
  end
97
98
 
99
+ apply_mappings(hash, format)
100
+ end
101
+
102
+ define_method(:"to_#{format}") do |instance|
103
+ value = public_send(:"as_#{format}", instance)
98
104
  adapter = Lutaml::Model::Config.public_send(:"#{format}_adapter")
99
105
 
100
106
  if format == :xml
101
107
  xml_options = { mapper_class: self }
102
-
103
- adapter.new(instance).public_send(:"to_#{format}", xml_options)
108
+ adapter.new(value).public_send(:"to_#{format}", xml_options)
104
109
  else
105
- hash = hash_representation(instance, format)
106
- adapter.new(hash).public_send(:"to_#{format}")
110
+ adapter.new(value).public_send(:"to_#{format}")
107
111
  end
108
112
  end
113
+
114
+ define_method(:"as_#{format}") do |instance|
115
+ if instance.is_a?(Array)
116
+ return instance.map { |item| public_send(:"as_#{format}", item) }
117
+ end
118
+
119
+ unless instance.is_a?(model)
120
+ msg = "argument is a '#{instance.class}' but should be a '#{model}'"
121
+ raise Lutaml::Model::IncorrectModelError, msg
122
+ end
123
+
124
+ return instance if format == :xml
125
+
126
+ hash_representation(instance, format)
127
+ end
109
128
  end
110
129
 
111
130
  def hash_representation(instance, format, options = {})
@@ -120,8 +139,7 @@ module Lutaml
120
139
  next handle_delegate(instance, rule, hash, format) if rule.delegate
121
140
 
122
141
  if rule.custom_methods[:to]
123
- next instance.send(rule.custom_methods[:to], instance,
124
- hash)
142
+ next instance.send(rule.custom_methods[:to], instance, hash)
125
143
  end
126
144
 
127
145
  value = instance.send(name)
@@ -238,6 +256,7 @@ module Lutaml
238
256
 
239
257
  def apply_mappings(doc, format, options = {})
240
258
  instance = options[:instance] || model.new
259
+ return instance if !doc || doc.empty?
241
260
  return apply_xml_mapping(doc, instance, options) if format == :xml
242
261
 
243
262
  mappings = mappings_for(format).mappings
@@ -257,7 +276,7 @@ module Lutaml
257
276
  end
258
277
 
259
278
  if rule.custom_methods[:from]
260
- value = new.send(rule.custom_methods[:from], instance, value)
279
+ value = new.send(rule.custom_methods[:from], instance, value) if value && !value.empty?
261
280
  next
262
281
  end
263
282
 
@@ -289,7 +308,7 @@ module Lutaml
289
308
  "missing for #{self} in #{options[:caller_class]}"
290
309
  end
291
310
 
292
- if instance.respond_to?(:ordered=)
311
+ if instance.respond_to?(:ordered=) && doc.is_a?(Lutaml::Model::MappingHash)
293
312
  instance.element_order = doc.item_order
294
313
  instance.ordered = mappings_for(:xml).mixed_content? || options[:mixed_content]
295
314
  end
@@ -299,12 +318,15 @@ module Lutaml
299
318
  raise "Attribute '#{rule.to}' not found in #{self}" unless attr
300
319
 
301
320
  is_content_mapping = rule.name.nil?
321
+
302
322
  value = if is_content_mapping
303
323
  doc["text"]
304
324
  else
305
325
  doc[rule.name.to_s] || doc[rule.name.to_sym]
306
326
  end
307
327
 
328
+ value = [value].compact if attr.collection? && !value.is_a?(Array)
329
+
308
330
  if value.is_a?(Array)
309
331
  value = value.map do |v|
310
332
  v.is_a?(Hash) && !(attr.type <= Serialize) ? v["text"] : v
@@ -22,11 +22,13 @@ module Lutaml
22
22
  class #{t} # class Integer
23
23
  def self.cast(value) # def self.cast(value)
24
24
  return if value.nil? # return if value.nil?
25
+ #
25
26
  Type.cast(value, #{t}) # Type.cast(value, Integer)
26
27
  end # end
27
28
 
28
29
  def self.serialize(value) # def self.serialize(value)
29
30
  return if value.nil? # return if value.nil?
31
+ #
30
32
  Type.serialize(value, #{t}) # Type.serialize(value, Integer)
31
33
  end # end
32
34
  end # end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.3.7"
5
+ VERSION = "0.3.8"
6
6
  end
7
7
  end
@@ -72,17 +72,18 @@ module Lutaml
72
72
  text = text[curr_index] if text.is_a?(Array)
73
73
 
74
74
  prefixed_xml.text text
75
- elsif attribute_def.collection?
75
+ elsif !value.nil? || element_rule.render_nil?
76
+ value = value[curr_index] if attribute_def.collection?
77
+
76
78
  add_to_xml(
77
79
  xml,
78
80
  element_rule.prefix,
79
- value[curr_index],
80
- attribute_def,
81
- element_rule,
81
+ value,
82
+ options.merge(
83
+ attribute: attribute_def,
84
+ rule: element_rule,
85
+ ),
82
86
  )
83
- elsif !value.nil? || element_rule.render_nil?
84
- add_to_xml(xml, element_rule.prefix, value, attribute_def,
85
- element_rule)
86
87
  end
87
88
  end
88
89
  end
@@ -158,7 +159,7 @@ module Lutaml
158
159
  end
159
160
  end
160
161
 
161
- def build_attributes(node)
162
+ def build_attributes(node, _options = {})
162
163
  attrs = node.attributes.transform_values(&:value)
163
164
 
164
165
  attrs.merge(build_namespace_attributes(node))
@@ -56,11 +56,18 @@ module Lutaml
56
56
  text = text[curr_index] if text.is_a?(Array)
57
57
 
58
58
  el.add_text(el, text)
59
- elsif attribute_def.collection?
60
- add_to_xml(el, nil, value[curr_index], attribute_def,
61
- element_rule)
62
59
  elsif !value.nil? || element_rule.render_nil?
63
- add_to_xml(el, nil, value, attribute_def, element_rule)
60
+ value = value[curr_index] if attribute_def.collection?
61
+
62
+ add_to_xml(
63
+ el,
64
+ nil,
65
+ value,
66
+ options.merge(
67
+ attribute: attribute_def,
68
+ rule: element_rule,
69
+ ),
70
+ )
64
71
  end
65
72
  end
66
73
  end
@@ -42,21 +42,24 @@ module Lutaml
42
42
  @root.order
43
43
  end
44
44
 
45
- def handle_nested_elements(builder, value, rule: nil, attribute: nil)
46
- options = build_options_for_nested_elements(attribute, rule)
45
+ def handle_nested_elements(builder, value, options = {})
46
+ element_options = build_options_for_nested_elements(options)
47
47
 
48
48
  case value
49
49
  when Array
50
- value.each { |val| build_element(builder, val, options) }
50
+ value.each { |val| build_element(builder, val, element_options) }
51
51
  else
52
- build_element(builder, value, options)
52
+ build_element(builder, value, element_options)
53
53
  end
54
54
  end
55
55
 
56
- def build_options_for_nested_elements(attribute, rule)
56
+ def build_options_for_nested_elements(options = {})
57
+ attribute = options.delete(:attribute)
58
+ rule = options.delete(:rule)
59
+
57
60
  return {} unless rule
58
61
 
59
- options = {}
62
+ # options = {}
60
63
 
61
64
  options[:namespace_prefix] = rule.prefix if rule&.namespace_set?
62
65
  options[:mixed_content] = rule.mixed_content
@@ -97,7 +100,18 @@ module Lutaml
97
100
  end
98
101
  end
99
102
 
100
- def add_to_xml(xml, prefix, value, attribute, rule)
103
+ def add_to_xml(xml, prefix, value, options = {})
104
+ if value.is_a?(Array)
105
+ value.each do |item|
106
+ add_to_xml(xml, prefix, item, options)
107
+ end
108
+
109
+ return
110
+ end
111
+
112
+ attribute = options[:attribute]
113
+ rule = options[:rule]
114
+
101
115
  if rule.custom_methods[:to]
102
116
  @root.send(rule.custom_methods[:to], @root, xml.parent, xml)
103
117
  return
@@ -107,8 +121,7 @@ module Lutaml
107
121
  handle_nested_elements(
108
122
  xml,
109
123
  value,
110
- rule: rule,
111
- attribute: attribute,
124
+ options.merge({ rule: rule, attribute: attribute }),
112
125
  )
113
126
  else
114
127
  xml.create_and_add_element(rule.name, prefix: prefix) do
@@ -136,7 +149,7 @@ module Lutaml
136
149
 
137
150
  attributes = options[:xml_attributes] ||= {}
138
151
  attributes = build_attributes(element,
139
- xml_mapping).merge(attributes)&.compact
152
+ xml_mapping, options).merge(attributes)&.compact
140
153
 
141
154
  prefix = if options.key?(:namespace_prefix)
142
155
  options[:namespace_prefix]
@@ -156,21 +169,19 @@ module Lutaml
156
169
  xml_mapping.elements.each do |element_rule|
157
170
  attribute_def = attribute_definition_for(element, element_rule,
158
171
  mapper_class: mapper_class)
172
+
159
173
  value = attribute_value_for(element, element_rule)
160
174
 
161
175
  next if value.nil? && !element_rule.render_nil?
162
176
 
163
- if attribute_def.collection?
164
- value = [value] unless value.is_a?(Array)
177
+ value = [value] if attribute_def.collection? && !value.is_a?(Array)
165
178
 
166
- value.each do |v|
167
- add_to_xml(xml, element_rule.prefix, v, attribute_def,
168
- element_rule)
169
- end
170
- elsif !value.nil? || element_rule.render_nil?
171
- add_to_xml(xml, element_rule.prefix, value, attribute_def,
172
- element_rule)
173
- end
179
+ add_to_xml(
180
+ xml,
181
+ element_rule.prefix,
182
+ value,
183
+ options.merge({ attribute: attribute_def, rule: element_rule }),
184
+ )
174
185
  end
175
186
 
176
187
  if (content_rule = xml_mapping.content_mapping)
@@ -231,10 +242,12 @@ module Lutaml
231
242
  attrs
232
243
  end
233
244
 
234
- def build_attributes(element, xml_mapping)
245
+ def build_attributes(element, xml_mapping, options = {})
235
246
  attrs = namespace_attributes(xml_mapping)
236
247
 
237
248
  xml_mapping.attributes.each_with_object(attrs) do |mapping_rule, hash|
249
+ next if options[:except]&.include?(mapping_rule.to)
250
+
238
251
  if mapping_rule.namespace
239
252
  hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace
240
253
  end
@@ -243,6 +256,8 @@ module Lutaml
243
256
  end
244
257
 
245
258
  xml_mapping.elements.each_with_object(attrs) do |mapping_rule, hash|
259
+ next if options[:except]&.include?(mapping_rule.to)
260
+
246
261
  if mapping_rule.namespace
247
262
  hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace
248
263
  end
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.7
4
+ version: 0.3.8
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-23 00:00:00.000000000 Z
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor