lutaml-model 0.3.21 → 0.3.23

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: f32654b195dc5ec1ea1d1969a8bd72e4f67b58c6995d4a18bcbea305422cf051
4
- data.tar.gz: d1ee4d99a0a98e420ba4b779bfae6e8ceab2c908c508938aa3def80e04507169
3
+ metadata.gz: e7e401ce23a01b4dc80f709b71326b598cb9b9024b81d2a94f501bebf8112a14
4
+ data.tar.gz: 30534172ee835a9170e4335029702ec87ec4225c52e9e09566cb694fbd9c258d
5
5
  SHA512:
6
- metadata.gz: ea73bd1071eeddac4a64dfd10b87f09cd2a357d3c841ca7169fc17edf629e8bfb1df0faa21ec2a080537022eab8a702f667cc1d0891175bb3e6181c0653a11ac
7
- data.tar.gz: 7d1f186d1a51faac0abce30525e4fdc815e9c8ac7b7f8c4387a5caaa8a4db7cfea972ad18f61d3b87a4fa1cc1613ef84de2644e1e6502c8e8890a38b7a125f72
6
+ metadata.gz: ce9d2be7aac031cba94181053fa59f19cb61ed6fafbdda177bec369a03fd92d53dcfc9caf43a34ebb439c7d69d2bf3f0db1a56a49e5cc34021a8fe4a0fc7fe25
7
+ data.tar.gz: 187168f579188ad1458013375fd5bacb6718dbb29d77994229c1c11fb14bcec5df8bae8bcfbe42dc3e57a5918c663741df7168338a2df36869562613aa06d149
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-30 07:34:33 UTC using RuboCop version 1.66.1.
3
+ # on 2024-11-03 22:29:56 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: 146
9
+ # Offense count: 124
10
10
  # This cop supports safe autocorrection (--autocorrect).
11
11
  # Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
12
12
  # URISchemes: http, https
@@ -46,7 +46,7 @@ Metrics/AbcSize:
46
46
  - 'lib/lutaml/model/xml_adapter/xml_document.rb'
47
47
  - 'lib/lutaml/model/xml_mapping_rule.rb'
48
48
 
49
- # Offense count: 5
49
+ # Offense count: 6
50
50
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
51
51
  # AllowedMethods: refine
52
52
  Metrics/BlockLength:
@@ -67,7 +67,7 @@ Metrics/CyclomaticComplexity:
67
67
  # Offense count: 51
68
68
  # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
69
69
  Metrics/MethodLength:
70
- Max: 43
70
+ Max: 46
71
71
 
72
72
  # Offense count: 7
73
73
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
@@ -95,7 +95,7 @@ RSpec/ContextWording:
95
95
  - 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
96
96
  - 'spec/lutaml/model/xml_mapping_spec.rb'
97
97
 
98
- # Offense count: 109
98
+ # Offense count: 110
99
99
  # Configuration parameters: CountAsOne.
100
100
  RSpec/ExampleLength:
101
101
  Max: 54
data/README.adoc CHANGED
@@ -157,22 +157,27 @@ Syntax:
157
157
  attribute :name_of_attribute, {symbol | string | class}
158
158
  ----
159
159
 
160
+ .Mapping between Lutaml::Model::Type classes, Ruby equivalents and serialization format types
160
161
  |===
161
- | String | Symbol | Class name | Actual value class
162
-
163
- | `String` | `:string` | `Lutaml::Model::Type::String` | `::String`
164
- | `Integer` | `:integer` | `Lutaml::Model::Type::Integer` | `::Integer`
165
- | `Float` | `:float` | `Lutaml::Model::Type::Float` | `::Float`
166
- | `Date` | `:date` | `Lutaml::Model::Type::Date` | `::Date`
167
- | `Time` | `:time` | `Lutaml::Model::Type::Time` | `::Time`
168
- | `DateTime` | `:date_time` | `Lutaml::Model::Type::DateTime` | `::DateTime`
169
- | `TimeWithoutDate` | `:time_without_date` | `Lutaml::Model::Type::TimeWithoutDate` | `::Time`
170
- | `Boolean` | `:boolean` | `Lutaml::Model::Type::Boolean` | `Boolean`
171
- | `Decimal` (optional) | `:decimal` | `Lutaml::Model::Type::Decimal` | `::BigDecimal`
172
- | `Hash` | `:hash` | `Lutaml::Model::Type::Hash` | `::Hash`
162
+ | Lutaml::Model::Type | Ruby Class | XML | JSON | YAML | Example Value
163
+
164
+ | `:string` | `String` | `xs:string` | `string` | `string` | `"text"`
165
+ | `:integer` | `Integer` | `xs:integer` | `number` | `integer` | `42`
166
+ | `:float` | `Float` | `xs:decimal` | `number` | `float` | `3.14`
167
+ | `:boolean` | `TrueClass`/`FalseClass` | `xs:boolean` | `boolean` | `boolean` | `true/false`
168
+ | `:date` | `Date` | `xs:date` | `string` (ISO8601) | `date` | `"2024-01-01"`
169
+ | `:time_without_date` | `Time` | `xs:time` | `string` (ISO8601) | `time` | `"12:34:56"`
170
+ | `:date_time` | `DateTime` | `xs:dateTime` | `string` (ISO8601) | `timestamp` | `"2024-01-01T00:00:00Z"`
171
+ | `:time` | `Time` | `xs:dateTime` | `string` (ISO8601) | `timestamp` | `"2024-01-01T00:00:00Z"`
172
+ | `:decimal` (optional) | `BigDecimal` | `xs:decimal` | `number` | `float` | `123.45`
173
+ | `:hash` | `Hash` | complex element | `object` | `map` | `{key: "value"}`
174
+ | `class` | Custom Class | complex element | `object` | `map` | `CustomObject`
175
+ | `collection: true` | `Array` of Type | repeated elements | `array` | `sequence` | `[obj1, obj2]`
176
+ // | `any`
173
177
 
174
178
  |===
175
179
 
180
+
176
181
  .Defining attributes with supported types via symbol, string and class
177
182
  [example]
178
183
  ====
@@ -538,8 +543,7 @@ For the following xml
538
543
  @description="\n A <b>fictional person</b> commonly used as a <i>placeholder name</i>.\n ",
539
544
  @element_order=["text", "name", "text", "description", "text"],
540
545
  @name="John Doe",
541
- @ordered=nil,
542
- @validate_on_set=false>
546
+ @ordered=nil>
543
547
  ----
544
548
  ====
545
549
 
@@ -1774,8 +1778,7 @@ end
1774
1778
  @name="Masterpiece: Vase",
1775
1779
  @ordered=nil,
1776
1780
  @size=12,
1777
- @description="A beautiful ceramic vase",
1778
- @validate_on_set=false>
1781
+ @description="A beautiful ceramic vase">
1779
1782
  > puts CustomCeramic.new(name: "Vase", size: 12, description: "A beautiful vase").to_xml
1780
1783
  # <CustomCeramic Size="15">
1781
1784
  # <Name>XML Masterpiece: Vase</Name>
@@ -200,11 +200,10 @@ module Lutaml
200
200
 
201
201
  def cast(value, format, options = {})
202
202
  value ||= [] if collection?
203
- instance = options[:instance]
204
203
 
205
204
  if value.is_a?(Array)
206
205
  value.map do |v|
207
- cast(v, format, instance: instance)
206
+ cast(v, format, options)
208
207
  end
209
208
  elsif type <= Serialize && value.is_a?(Hash)
210
209
  type.apply_mappings(value, format, options)
@@ -217,7 +216,8 @@ module Lutaml
217
216
 
218
217
  def validate_options!(options)
219
218
  if (invalid_opts = options.keys - ALLOWED_OPTIONS).any?
220
- raise StandardError, "Invalid options given for `#{name}` #{invalid_opts}"
219
+ raise StandardError,
220
+ "Invalid options given for `#{name}` #{invalid_opts}"
221
221
  end
222
222
  end
223
223
 
@@ -225,7 +225,8 @@ module Lutaml
225
225
  return true if type.is_a?(Class)
226
226
  return true if [Symbol, String].include?(type.class) && cast_type!(type)
227
227
 
228
- raise ArgumentError, "Invalid type: #{type}, must be a Symbol, String or a Class"
228
+ raise ArgumentError,
229
+ "Invalid type: #{type}, must be a Symbol, String or a Class"
229
230
  end
230
231
  end
231
232
  end
@@ -32,9 +32,10 @@ module Lutaml
32
32
  @mappings ||= {}
33
33
  @attributes ||= {}
34
34
 
35
- subclass.instance_variable_set(:@attributes, Utils.deep_dup(@attributes))
35
+ subclass.instance_variable_set(:@attributes,
36
+ Utils.deep_dup(@attributes))
36
37
  subclass.instance_variable_set(:@mappings, Utils.deep_dup(@mappings))
37
- subclass.instance_variable_set(:@model, subclass)
38
+ subclass.instance_variable_set(:@model, @model || subclass)
38
39
  end
39
40
 
40
41
  def model(klass = nil)
@@ -51,17 +52,20 @@ module Lutaml
51
52
  Utils.add_boolean_accessor_if_not_defined(klass, :mixed)
52
53
  Utils.add_accessor_if_not_defined(klass, :element_order)
53
54
 
54
- Utils.add_method_if_not_defined(klass, :using_default_for) do |attribute_name|
55
+ Utils.add_method_if_not_defined(klass,
56
+ :using_default_for) do |attribute_name|
55
57
  @using_default ||= {}
56
58
  @using_default[attribute_name] = true
57
59
  end
58
60
 
59
- Utils.add_method_if_not_defined(klass, :value_set_for) do |attribute_name|
61
+ Utils.add_method_if_not_defined(klass,
62
+ :value_set_for) do |attribute_name|
60
63
  @using_default ||= {}
61
64
  @using_default[attribute_name] = false
62
65
  end
63
66
 
64
- Utils.add_method_if_not_defined(klass, :using_default?) do |attribute_name|
67
+ Utils.add_method_if_not_defined(klass,
68
+ :using_default?) do |attribute_name|
65
69
  @using_default ||= {}
66
70
  !!@using_default[attribute_name]
67
71
  end
@@ -306,6 +310,7 @@ module Lutaml
306
310
  def apply_xml_mapping(doc, instance, options = {})
307
311
  return instance unless doc
308
312
 
313
+ options[:default_namespace] = mappings_for(:xml)&.namespace_uri if options[:default_namespace].nil?
309
314
  mappings = mappings_for(:xml).mappings
310
315
 
311
316
  if doc.is_a?(Array)
@@ -336,14 +341,14 @@ module Lutaml
336
341
  doc.node.inner_xml
337
342
  elsif rule.content_mapping?
338
343
  doc["text"]
339
- elsif doc.key_exist?(rule.namespaced_name)
340
- doc.fetch(rule.namespaced_name)
344
+ elsif doc.key_exist?(rule.namespaced_name(options[:default_namespace]))
345
+ doc.fetch(rule.namespaced_name(options[:default_namespace]))
341
346
  else
342
347
  defaults_used << rule.to
343
348
  rule.to_value_for(instance)
344
349
  end
345
350
 
346
- value = normalize_xml_value(value, rule)
351
+ value = normalize_xml_value(value, rule, options)
347
352
  rule.deserialize(instance, value, attributes, self)
348
353
  end
349
354
 
@@ -384,7 +389,7 @@ module Lutaml
384
389
  instance
385
390
  end
386
391
 
387
- def normalize_xml_value(value, rule)
392
+ def normalize_xml_value(value, rule, options = {})
388
393
  attr = attribute_for_rule(rule)
389
394
 
390
395
  value = [value].compact if attr&.collection? && !value.is_a?(Array)
@@ -401,11 +406,11 @@ module Lutaml
401
406
 
402
407
  return value unless cast_value?(attr, rule)
403
408
 
409
+ options.merge(caller_class: self, mixed_content: rule.mixed_content)
404
410
  attr.cast(
405
411
  value,
406
412
  :xml,
407
- caller_class: self,
408
- mixed_content: rule.mixed_content,
413
+ options,
409
414
  )
410
415
  end
411
416
 
@@ -446,8 +451,7 @@ module Lutaml
446
451
  attr_writer :ordered, :mixed
447
452
 
448
453
  def initialize(attrs = {})
449
- @validate_on_set = attrs.delete(:validate_on_set) || false
450
- @using_default ||= {}
454
+ @using_default = {}
451
455
 
452
456
  return unless self.class.attributes
453
457
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.3.21"
5
+ VERSION = "0.3.23"
6
6
  end
7
7
  end
@@ -64,7 +64,7 @@ module Lutaml
64
64
  end
65
65
 
66
66
  def method_missing(method_name, *args, &block)
67
- if block_given?
67
+ if block
68
68
  xml.public_send(method_name, *args, &block)
69
69
  else
70
70
  xml.public_send(method_name, *args)
@@ -128,12 +128,15 @@ module Lutaml
128
128
  )
129
129
  end
130
130
 
131
- default_namespace = node.namespace&.href if root_node.nil?
131
+ if root_node.nil? && !node.namespace&.prefix
132
+ default_namespace = node.namespace&.href
133
+ end
132
134
 
133
135
  super(
134
136
  node,
135
137
  attributes,
136
- parse_all_children(node, root_node: root_node || self, default_namespace: default_namespace),
138
+ parse_all_children(node, root_node: root_node || self,
139
+ default_namespace: default_namespace),
137
140
  node.text,
138
141
  parent_document: root_node,
139
142
  namespace_prefix: node.namespace&.prefix,
@@ -182,7 +185,8 @@ module Lutaml
182
185
 
183
186
  def parse_all_children(node, root_node: nil, default_namespace: nil)
184
187
  node.children.map do |child|
185
- NokogiriElement.new(child, root_node: root_node, default_namespace: default_namespace)
188
+ NokogiriElement.new(child, root_node: root_node,
189
+ default_namespace: default_namespace)
186
190
  end
187
191
  end
188
192
 
@@ -77,7 +77,10 @@ module Lutaml
77
77
  result.item_order = element.order
78
78
 
79
79
  element.children.each_with_object(result) do |child, hash|
80
- attr = klass.attribute_for_child(child.name, format) if klass&.<= Serialize
80
+ if klass&.<= Serialize
81
+ attr = klass.attribute_for_child(child.name,
82
+ format)
83
+ end
81
84
 
82
85
  value = if child.text?
83
86
  child.text
@@ -136,7 +139,8 @@ module Lutaml
136
139
  rule = options[:rule]
137
140
 
138
141
  if rule.custom_methods[:to]
139
- options[:mapper_class].new.send(rule.custom_methods[:to], element, xml.parent, xml)
142
+ options[:mapper_class].new.send(rule.custom_methods[:to], element,
143
+ xml.parent, xml)
140
144
  return
141
145
  end
142
146
 
@@ -203,7 +207,8 @@ module Lutaml
203
207
  end
204
208
 
205
209
  xml_mapping.attributes.each do |attribute_rule|
206
- attribute_rule.serialize_attribute(element, prefixed_xml.parent, xml)
210
+ attribute_rule.serialize_attribute(element, prefixed_xml.parent,
211
+ xml)
207
212
  end
208
213
 
209
214
  xml_mapping.elements.each do |element_rule|
@@ -223,11 +228,13 @@ module Lutaml
223
228
  element,
224
229
  element_rule.prefix,
225
230
  value,
226
- options.merge({ attribute: attribute_def, rule: element_rule, mapper_class: mapper_class }),
231
+ options.merge({ attribute: attribute_def, rule: element_rule,
232
+ mapper_class: mapper_class }),
227
233
  )
228
234
  end
229
235
 
230
- process_content_mapping(element, xml_mapping.content_mapping, prefixed_xml)
236
+ process_content_mapping(element, xml_mapping.content_mapping,
237
+ prefixed_xml)
231
238
 
232
239
  process_raw_mapping(element, xml_mapping.raw_mapping, prefixed_xml)
233
240
  end
@@ -318,7 +325,8 @@ module Lutaml
318
325
  next unless type
319
326
 
320
327
  if type <= Lutaml::Model::Serialize
321
- attrs = attrs.merge(build_namespace_attributes(type, processed, { caller_rule: mapping_rule }))
328
+ attrs = attrs.merge(build_namespace_attributes(type, processed,
329
+ { caller_rule: mapping_rule }))
322
330
  end
323
331
 
324
332
  if mapping_rule.namespace && mapping_rule.prefix && mapping_rule.name != "lang"
@@ -348,8 +356,10 @@ module Lutaml
348
356
  hash["xmlns:#{mapping_rule.prefix}"] = mapping_rule.namespace
349
357
  end
350
358
 
351
- if render_element?(mapping_rule, element, mapping_rule.to_value_for(element))
352
- hash[mapping_rule.prefixed_name] = mapping_rule.to_value_for(element)
359
+ if render_element?(mapping_rule, element,
360
+ mapping_rule.to_value_for(element))
361
+ hash[mapping_rule.prefixed_name] =
362
+ mapping_rule.to_value_for(element)
353
363
  end
354
364
  end
355
365
 
@@ -222,12 +222,15 @@ module Lutaml
222
222
 
223
223
  def deep_dup
224
224
  self.class.new.tap do |xml_mapping|
225
- xml_mapping.root(@root_element.dup, mixed: @mixed_content, ordered: @ordered)
225
+ xml_mapping.root(@root_element.dup, mixed: @mixed_content,
226
+ ordered: @ordered)
226
227
  xml_mapping.namespace(@namespace_uri.dup, @namespace_prefix.dup)
227
228
 
228
- xml_mapping.instance_variable_set(:@attributes, dup_mappings(@attributes))
229
+ xml_mapping.instance_variable_set(:@attributes,
230
+ dup_mappings(@attributes))
229
231
  xml_mapping.instance_variable_set(:@elements, dup_mappings(@elements))
230
- xml_mapping.instance_variable_set(:@content_mapping, @content_mapping&.deep_dup)
232
+ xml_mapping.instance_variable_set(:@content_mapping,
233
+ @content_mapping&.deep_dup)
231
234
  end
232
235
  end
233
236
 
@@ -73,7 +73,7 @@ module Lutaml
73
73
  end
74
74
  end
75
75
 
76
- def namespaced_name
76
+ def namespaced_name(parent_namespace = nil)
77
77
  if name == "lang"
78
78
  "#{prefix}:#{name}"
79
79
  elsif namespace_set? || @attribute
@@ -81,7 +81,7 @@ module Lutaml
81
81
  elsif default_namespace
82
82
  "#{default_namespace}:#{name}"
83
83
  else
84
- name
84
+ [parent_namespace, name].compact.join(":")
85
85
  end
86
86
  end
87
87
 
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.21
4
+ version: 0.3.23
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-11-02 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor