Module: Lutaml::Yaml::Type::Serializers

Defined in:
lib/lutaml/yaml/type/serializers.rb

Overview

Registers YAML-specific type serializers for types that need custom to_yaml / from_yaml behavior beyond the default.

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
# File 'lib/lutaml/yaml/type/serializers.rb', line 11

def register_all!
  v = Lutaml::Model::Type::Value

  # String — from_yaml delegates to cast
  v.register_format_type_serializer(
    :yaml, Lutaml::Model::Type::String,
    from: ->(val) { Lutaml::Model::Type::String.cast(val) }
  )

  # Time — ISO8601 string
  v.register_format_type_serializer(
    :yaml, Lutaml::Model::Type::Time,
    to: ->(inst) { inst.value&.iso8601.to_s }
  )

  # DateTime — ISO8601 string
  v.register_format_type_serializer(
    :yaml, Lutaml::Model::Type::DateTime,
    to: ->(inst) { inst.value&.iso8601.to_s }
  )

  # TimeWithoutDate — HH:MM:SS string
  v.register_format_type_serializer(
    :yaml, Lutaml::Model::Type::TimeWithoutDate,
    to: ->(inst) { inst.value&.strftime("%H:%M:%S").to_s }
  )

  # Date — ISO8601 string
  v.register_format_type_serializer(
    :yaml, Lutaml::Model::Type::Date,
    to: ->(inst) { inst.value&.iso8601.to_s }
  )

  # Decimal — F format string
  v.register_format_type_serializer(
    :yaml, Lutaml::Model::Type::Decimal,
    to: ->(inst) { inst.value&.to_s("F") }
  )

  # Reference — key
  v.register_format_type_serializer(
    :yaml, Lutaml::Model::Type::Reference,
    to: lambda(&:key),
    from: ->(val) { Lutaml::Model::Type::Reference.cast(val) }
  )
end