Class: Metaschema::TypeMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/metaschema/type_mapper.rb

Constant Summary collapse

TYPE_MAP =
{
  # Basic types
  "string" => :string,
  "token" => :string,
  "boolean" => :boolean,
  "integer" => :integer,
  "decimal" => :decimal,

  # Integer subtypes
  "positive-integer" => :integer,
  "negative-integer" => :integer,
  "non-positive-integer" => :integer,
  "non-negative-integer" => :integer,

  # Date/time types
  "date" => :date,
  "dateTime" => :date_time,
  "date-with-timezone" => :date,
  "date-time-with-timezone" => :date_time,

  # String subtypes
  "uuid" => :string,
  "uri" => :string,
  "email" => :string,
  "hostname" => :string,
  "ip-address" => :string,

  # Markup types — use Metaschema's own types
  "markup-line" => Metaschema::MarkupLineDatatype,
  "markup-multiline" => Metaschema::MarkupLineDatatype,
}.freeze
MARKUP_TYPES =
%w[markup-line markup-multiline].freeze

Class Method Summary collapse

Class Method Details

.map(as_type) ⇒ Object



40
41
42
# File 'lib/metaschema/type_mapper.rb', line 40

def map(as_type)
  TYPE_MAP[as_type.to_s] || :string
end

.markup?(as_type) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/metaschema/type_mapper.rb', line 44

def markup?(as_type)
  MARKUP_TYPES.include?(as_type.to_s)
end

.multiline?(as_type) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/metaschema/type_mapper.rb', line 48

def multiline?(as_type)
  as_type.to_s == "markup-multiline"
end

.register_serializers!Object

Register format-specific serializers for types that lutaml-model doesn’t handle correctly out of the box.



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
# File 'lib/metaschema/type_mapper.rb', line 54

def register_serializers!
  # Decimal JSON — BigDecimal#to_s defaults to scientific
  # notation ("0.11e1"); JSON needs plain notation (1.1)
  Lutaml::Model::Type::Value.register_format_type_serializer(
    :json, Lutaml::Model::Type::Decimal,
    to: lambda { |inst|
      return nil unless inst.value

      v = inst.value
      v = Lutaml::Model::Type::Decimal.cast(v) unless v.is_a?(BigDecimal)
      v.to_f
    }
  )

  # Decimal XML — same scientific notation issue
  Lutaml::Model::Type::Value.register_format_type_serializer(
    :xml, Lutaml::Model::Type::Decimal,
    to: lambda { |inst|
      return nil unless inst.value

      v = inst.value
      v = Lutaml::Model::Type::Decimal.cast(v) unless v.is_a?(BigDecimal)
      v.to_s("F")
    }
  )
end