Module: Lutaml::Xml::Type::Serializers
- Defined in:
- lib/lutaml/xml/type/serializers.rb
Overview
Registers XML-specific type serializers for all types that need custom to_xml / from_xml behavior beyond the default (return value).
Class Method Summary collapse
Class Method Details
.register_all! ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lutaml/xml/type/serializers.rb', line 11 def register_all! v = Lutaml::Model::Type::Value # String — value&.to_s v.register_format_type_serializer( :xml, Lutaml::Model::Type::String, to: ->(inst) { inst.value&.to_s }, from: ->(val) { Lutaml::Model::Type::String.cast(val) } ) # Float — value.to_s v.register_format_type_serializer( :xml, Lutaml::Model::Type::Float, to: ->(inst) { inst.value.to_s } ) # Boolean — value.to_s v.register_format_type_serializer( :xml, Lutaml::Model::Type::Boolean, to: ->(inst) { inst.value.to_s } ) # Time — ISO8601 with fractional seconds handling v.register_format_type_serializer( :xml, Lutaml::Model::Type::Time, to: lambda { |inst| return nil unless inst.value if inst.value.subsec.zero? inst.value.iso8601 else inst.value.iso8601(6).sub(/(\.\d{3})0{3}([+-])/, '\1\2') end } ) # DateTime — ISO8601 with Z for UTC v.register_format_type_serializer( :xml, Lutaml::Model::Type::DateTime, to: lambda { |inst| return nil unless inst.value result = Lutaml::Model::Type::DateTime.format_datetime_iso8601(inst.value) inst.value.offset.zero? ? result.sub(/\+00:00$/, "Z") : result } ) # Date — ISO8601 with Z for UTC v.register_format_type_serializer( :xml, Lutaml::Model::Type::Date, to: lambda { |inst| return nil unless inst.value result = Lutaml::Model::Type::Date.serialize(inst.value) result = result.sub(/\+00:00$/, "Z") if result.include?("+00:00") result } ) # TimeWithoutDate — delegates to self.class.serialize v.register_format_type_serializer( :xml, Lutaml::Model::Type::TimeWithoutDate, to: ->(inst) { Lutaml::Model::Type::TimeWithoutDate.serialize(inst.value) } ) # Symbol — :symbol: format v.register_format_type_serializer( :xml, Lutaml::Model::Type::Symbol, to: ->(inst) { ":#{inst.value}:" } ) # Hash — value (pass-through) v.register_format_type_serializer( :xml, Lutaml::Model::Type::Hash, to: lambda(&:value) ) # Reference — key&.to_s v.register_format_type_serializer( :xml, Lutaml::Model::Type::Reference, to: ->(inst) { inst.key&.to_s }, from: ->(val) { Lutaml::Model::Type::Reference.cast(val) } ) end |