lutaml-model 0.3.16 → 0.3.18

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: b9687a8a307de8226b41a87abda56626627eefe47665628c0e9112b97f27385a
4
- data.tar.gz: e819f531048e629d4943ea7b613815a568990f372ef2da93162d0d1b4d8395cd
3
+ metadata.gz: fba4d6c61d5d3e174461bbf07c8a7db59169565726c93472680fa146e833f309
4
+ data.tar.gz: 1de02cedf4b5eb2e1971e26960d313831f15214e40fbd89657d5da781b289ad4
5
5
  SHA512:
6
- metadata.gz: f47d1336093db1eb45c5225913a9c63d66681de77d5803a21702baab0ca8e571e882a2d8f6f6dcf7b9180c897c9ae8bfd4b2db6532e9691487d104696657457e
7
- data.tar.gz: 9ec8b8d841606d202acdcca81464761cc0f75e9ae5b35d2c3b8dd01c6e8a122642a348f41de2852b09386d3748eb61b1b2547df8d1ccb146a7d36e5a8ac46257
6
+ metadata.gz: 4c02cb07f6e85f5272e821cffebed2e2b179580a76441641401a8f321a9cf3901dce21022eb846e5d2cd8a6ec1fd9fca4cb7187d6b3a736c191c8697605b0180
7
+ data.tar.gz: 84512d6d0695a2176a8c31869fad1f7f3ebbd8449abc0cf44d017bca081ef85c95f4834931842b9e8f9a9b68e143af45ec6ea642445ab82413501727b1fd6818
data/README.adoc CHANGED
@@ -464,6 +464,7 @@ class Glaze < Lutaml::Model::Serializable
464
464
  end
465
465
  end
466
466
  ----
467
+ ====
467
468
 
468
469
  .Attributes with `render_default: true` are rendered when the value is identical to the default
469
470
  [example]
@@ -741,6 +742,7 @@ end
741
742
  > Example.new(value: 12).to_xml
742
743
  > #<example value="12"></example>
743
744
  ----
745
+ ====
744
746
 
745
747
  The `map_attribute` method does not inherit the root element's namespace.
746
748
  To specify a namespace for an attribute, please explicitly declare the
@@ -1,7 +1,7 @@
1
1
  module Lutaml
2
2
  module Model
3
3
  class MappingHash < Hash
4
- attr_accessor :ordered
4
+ attr_accessor :ordered, :node
5
5
 
6
6
  def initialize
7
7
  @ordered = false
@@ -397,7 +397,7 @@ module Lutaml
397
397
  value
398
398
  end
399
399
 
400
- if attr && !rule.content_mapping?
400
+ if attr && !rule.content_mapping? && !rule.custom_methods[:from]
401
401
  value = attr.cast(
402
402
  value,
403
403
  :xml,
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.3.16"
5
+ VERSION = "0.3.18"
6
6
  end
7
7
  end
@@ -54,7 +54,7 @@ module Lutaml
54
54
  element = element.xml.parent
55
55
  end
56
56
 
57
- add_element(element, ::Nokogiri::XML::Text.new(text.to_s, xml.doc))
57
+ element << text.to_s
58
58
  end
59
59
 
60
60
  def add_namespace_prefix(prefix)
@@ -15,7 +15,7 @@ module Lutaml
15
15
  def to_xml(options = {})
16
16
  builder = Builder::Nokogiri.build(encoding: "UTF-8") do |xml|
17
17
  if root.is_a?(Lutaml::Model::XmlAdapter::NokogiriElement)
18
- root.to_xml(xml)
18
+ root.build_xml(xml)
19
19
  else
20
20
  mapper_class = options[:mapper_class] || @root.class
21
21
  options[:xml_attributes] =
@@ -49,6 +49,7 @@ module Lutaml
49
49
  end
50
50
 
51
51
  tag_name = options[:tag_name] || xml_mapping.root_element
52
+ tag_name = "#{tag_name}_" if prefixed_xml.respond_to?(tag_name)
52
53
  prefixed_xml.public_send(tag_name, attributes) do
53
54
  if options.key?(:namespace_prefix) && !options[:namespace_prefix]
54
55
  xml.parent.namespace = nil
@@ -72,11 +73,9 @@ module Lutaml
72
73
  text = xml_mapping.content_mapping.serialize(element)
73
74
  text = text[curr_index] if text.is_a?(Array)
74
75
 
75
- if element.mixed?
76
- prefixed_xml.text text
77
- else
78
- content << text
79
- end
76
+ next prefixed_xml.text(text) if element.mixed?
77
+
78
+ content << text
80
79
  elsif !value.nil? || element_rule.render_nil?
81
80
  value = value[curr_index] if attribute_def.collection?
82
81
 
@@ -88,6 +87,7 @@ module Lutaml
88
87
  options.merge(
89
88
  attribute: attribute_def,
90
89
  rule: element_rule,
90
+ mapper_class: mapper_class,
91
91
  ),
92
92
  )
93
93
  end
@@ -123,9 +123,11 @@ module Lutaml
123
123
  namespace_prefix: attr.namespace&.prefix,
124
124
  )
125
125
  end
126
+
126
127
  default_namespace = node.namespace&.href if root_node.nil?
128
+
127
129
  super(
128
- node.name,
130
+ node,
129
131
  attributes,
130
132
  parse_all_children(node, root_node: root_node || self, default_namespace: default_namespace),
131
133
  node.text,
@@ -140,7 +142,13 @@ module Lutaml
140
142
  children.empty? && text.length.positive?
141
143
  end
142
144
 
143
- def to_xml(builder = nil)
145
+ def to_xml
146
+ return text if text?
147
+
148
+ build_xml.doc.root.to_xml
149
+ end
150
+
151
+ def build_xml(builder = nil)
144
152
  builder ||= Builder::Nokogiri.build
145
153
 
146
154
  if name == "text"
@@ -148,7 +156,7 @@ module Lutaml
148
156
  else
149
157
  builder.public_send(name, build_attributes(self)) do |xml|
150
158
  children.each do |child|
151
- child.to_xml(xml)
159
+ child.build_xml(xml)
152
160
  end
153
161
  end
154
162
  end
@@ -16,7 +16,7 @@ module Lutaml
16
16
  builder = Builder::Ox.build
17
17
 
18
18
  if @root.is_a?(Lutaml::Model::XmlAdapter::OxElement)
19
- @root.to_xml(builder)
19
+ @root.build_xml(builder)
20
20
  elsif ordered?(@root, options)
21
21
  build_ordered_element(builder, @root, options)
22
22
  else
@@ -117,7 +117,7 @@ module Lutaml
117
117
  end
118
118
 
119
119
  super(
120
- node.name.to_s,
120
+ node,
121
121
  attributes,
122
122
  parse_children(node, root_node: root_node || self),
123
123
  node.text,
@@ -126,7 +126,13 @@ module Lutaml
126
126
  end
127
127
  end
128
128
 
129
- def to_xml(builder = nil)
129
+ def to_xml
130
+ return text if text?
131
+
132
+ build_xml.xml.to_s
133
+ end
134
+
135
+ def build_xml(builder = nil)
130
136
  builder ||= Builder::Ox.build
131
137
  attrs = build_attributes(self)
132
138
 
@@ -134,9 +140,11 @@ module Lutaml
134
140
  builder.add_text(builder, text)
135
141
  else
136
142
  builder.create_and_add_element(name, attributes: attrs) do |el|
137
- children.each { |child| child.to_xml(el) }
143
+ children.each { |child| child.build_xml(el) }
138
144
  end
139
145
  end
146
+
147
+ builder
140
148
  end
141
149
 
142
150
  def namespace_attributes(attributes)
@@ -73,6 +73,7 @@ module Lutaml
73
73
 
74
74
  def parse_element(element, klass = nil, format = nil)
75
75
  result = Lutaml::Model::MappingHash.new
76
+ result.node = element
76
77
  result.item_order = element.order
77
78
 
78
79
  element.children.each_with_object(result) do |child, hash|
@@ -81,11 +82,7 @@ module Lutaml
81
82
  value = if child.text?
82
83
  child.text
83
84
  elsif attr&.raw?
84
- child.children.map do |c|
85
- next c.text if c.text?
86
-
87
- c.to_xml.doc.root.to_xml({})
88
- end.join
85
+ child.children.map(&:to_xml).join
89
86
  else
90
87
  parse_element(child, attr&.type || klass, format)
91
88
  end
@@ -10,8 +10,10 @@ module Lutaml
10
10
  :namespace_prefix,
11
11
  :parent_document
12
12
 
13
+ attr_accessor :adapter_node
14
+
13
15
  def initialize(
14
- name,
16
+ node,
15
17
  attributes = {},
16
18
  children = [],
17
19
  text = nil,
@@ -19,13 +21,22 @@ module Lutaml
19
21
  namespace_prefix: nil,
20
22
  default_namespace: nil
21
23
  )
22
- @name = extract_name(name)
23
- @namespace_prefix = namespace_prefix || extract_namespace_prefix(name)
24
- @attributes = attributes # .map { |k, v| XmlAttribute.new(k, v) }
24
+ @name = extract_name(node)
25
+ @namespace_prefix = namespace_prefix || extract_namespace_prefix(node)
26
+ @attributes = attributes
25
27
  @children = children
26
28
  @text = text
27
29
  @parent_document = parent_document
28
30
  @default_namespace = default_namespace
31
+
32
+ self.adapter_node = node
33
+ end
34
+
35
+ # This tells which attributes to pretty print, So we remove the
36
+ # @parent_document and @adapter_node because they were causing
37
+ # so much repeatative output.
38
+ def pretty_print_instance_variables
39
+ (instance_variables - %i[@adapter_node @parent_document]).sort
29
40
  end
30
41
 
31
42
  def name
@@ -79,20 +90,32 @@ module Lutaml
79
90
  namespaces[nil] || @parent_document&.namespaces&.dig(nil)
80
91
  end
81
92
 
82
- def extract_name(name)
83
- n = name.to_s.split(":")
93
+ def extract_name(node)
94
+ name = name_from_node(node)
95
+
96
+ n = name.split(":")
84
97
  return name if n.length <= 1
85
98
 
86
99
  n[1..].join(":")
87
100
  end
88
101
 
89
- def extract_namespace_prefix(name)
102
+ def extract_namespace_prefix(node)
103
+ name = name_from_node(node)
104
+
90
105
  n = name.to_s.split(":")
91
106
  return if n.length <= 1
92
107
 
93
108
  n.first
94
109
  end
95
110
 
111
+ def name_from_node(node)
112
+ if node.is_a?(String)
113
+ node
114
+ else
115
+ node.name.to_s
116
+ end
117
+ end
118
+
96
119
  def order
97
120
  children.each_with_object([]) do |child, arr|
98
121
  arr << child.unprefixed_name
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.16
4
+ version: 0.3.18
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-28 00:00:00.000000000 Z
11
+ date: 2024-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor