lib/lutaml/model/serialize.rb in lutaml-model-0.3.4 vs lib/lutaml/model/serialize.rb in lutaml-model-0.3.5
- old
+ new
@@ -208,13 +208,11 @@
if v.is_a?(Hash)
attr_rule.type.new(v)
else
# TODO: This code is problematic because Type.cast does not know
# about all the types.
- Lutaml::Model::Type.cast(
- v, attr_rule.type
- )
+ Lutaml::Model::Type.cast(v, attr_rule.type)
end
end
elsif value.is_a?(Hash) && attr_rule.type != Lutaml::Model::Type::Hash
generate_model_object(attr_rule.type, value)
else
@@ -274,11 +272,11 @@
path[0...-1].inject(map_value) do |acc, k|
acc[k.to_s] ||= {}
end.public_send(:[]=, path.last.to_s, child_obj.send(attr_name))
end
end
- # hash[mapping.name] ||= {}
+
hash[map_key] = map_value
end
hash
end
@@ -335,10 +333,12 @@
mapping_hash = Lutaml::Model::MappingHash.new
mapping_hash.item_order = doc.item_order
mapping_hash.ordered = mappings_for(:xml).mixed_content? || mixed_content
+ mapping_from = []
+
mappings.each_with_object(mapping_hash) do |rule, hash|
attr = attributes[rule.to]
raise "Attribute '#{rule.to}' not found in #{self}" unless attr
is_content_mapping = rule.name.nil?
@@ -370,12 +370,26 @@
end
value = attr.type.cast(value) unless is_content_mapping
end
+ mapping_from << rule if rule.custom_methods[:from]
+
hash[rule.to] = value
end
+
+ mapping_from.each do |rule|
+ value = if rule.name.nil?
+ mapping_hash[rule.to].join("\n").strip
+ else
+ mapping_hash[rule.to]
+ end
+
+ mapping_hash[rule.to] = new.send(rule.custom_methods[:from], mapping_hash, value)
+ end
+
+ mapping_hash
end
def ensure_utf8(value)
case value
when String
@@ -383,10 +397,12 @@
when Array
value.map { |v| ensure_utf8(v) }
when Hash
value.transform_keys do |k|
ensure_utf8(k)
- end.transform_values { |v| ensure_utf8(v) }
+ end.transform_values do |v|
+ ensure_utf8(v)
+ end
else
value
end
end
end