lib/lutaml/model/xml_adapter/nokogiri_adapter.rb in lutaml-model-0.3.0 vs lib/lutaml/model/xml_adapter/nokogiri_adapter.rb in lutaml-model-0.3.1
- old
+ new
@@ -11,15 +11,16 @@
root = NokogiriElement.new(parsed.root)
new(root)
end
def to_xml(options = {})
- builder = Nokogiri::XML::Builder.new do |xml|
+ builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
if root.is_a?(Lutaml::Model::XmlAdapter::NokogiriElement)
root.to_xml(xml)
else
- options[:xml_attributes] = build_namespace_attributes(@root.class)
+ mapper_class = options[:mapper_class] || @root.class
+ options[:xml_attributes] = build_namespace_attributes(mapper_class)
build_element(xml, @root, options)
end
end
xml_options = {}
@@ -30,11 +31,12 @@
end
private
def build_unordered_element(xml, element, options = {})
- xml_mapping = element.class.mappings_for(:xml)
+ mapper_class = options[:mapper_class] || element.class
+ xml_mapping = mapper_class.mappings_for(:xml)
return xml unless xml_mapping
attributes = options[:xml_attributes] ||= {}
attributes = build_attributes(element,
xml_mapping).merge(attributes)&.compact
@@ -51,11 +53,11 @@
if options.key?(:namespace_prefix) && !options[:namespace_prefix]
xml.parent.namespace = nil
end
xml_mapping.elements.each do |element_rule|
- attribute_def = attribute_definition_for(element, element_rule)
+ attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class)
value = attribute_value_for(element, element_rule)
next if value.nil? && !element_rule.render_nil?
nsp_xml = element_rule.prefix ? xml[element_rule.prefix] : xml
@@ -77,11 +79,12 @@
end
end
end
def build_ordered_element(xml, element, options = {})
- xml_mapping = element.class.mappings_for(:xml)
+ mapper_class = options[:mapper_class] || element.class
+ xml_mapping = mapper_class.mappings_for(:xml)
return xml unless xml_mapping
attributes = build_attributes(element, xml_mapping)&.compact
prefixed_xml = if options.key?(:namespace_prefix)
@@ -104,11 +107,11 @@
curr_index = index_hash[name] += 1
element_rule = xml_mapping.find_by_name(name)
next if element_rule.nil?
- attribute_def = attribute_definition_for(element, element_rule)
+ attribute_def = attribute_definition_for(element, element_rule, mapper_class: mapper_class)
value = attribute_value_for(element, element_rule)
nsp_xml = element_rule.prefix ? xml[element_rule.prefix] : xml
if element_rule == xml_mapping.content_mapping
text = element.send(xml_mapping.content_mapping.to)
@@ -125,10 +128,15 @@
end
end
def add_to_xml(xml, value, attribute, rule)
if value && (attribute&.type&.<= Lutaml::Model::Serialize)
- handle_nested_elements(xml, value, rule)
+ handle_nested_elements(
+ xml,
+ value,
+ rule: rule,
+ attribute: attribute,
+ )
else
xml.public_send(rule.name) do
if !value.nil?
serialized_value = attribute.type.serialize(value)