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,
  "day-time-duration" => :string,

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

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

Default JSON value key by data type. Determines the JSON property name that holds a field’s content value when serialized.

{
  "markup-line" => "RICHTEXT",
  "markup-multiline" => "prose",
}.freeze

Class Method Summary collapse

Class Method Details

.json_value_key(as_type) ⇒ Object

Returns the default JSON value key for the given data type. E.g. “markup-line” -> “RICHTEXT”, “markup-multiline” -> “prose”, everything else -> “STRVALUE”.



68
69
70
# File 'lib/metaschema/type_mapper.rb', line 68

def json_value_key(as_type)
  JSON_VALUE_KEY[as_type.to_s] || "STRVALUE"
end

.map(as_type) ⇒ Object



53
54
55
# File 'lib/metaschema/type_mapper.rb', line 53

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

.markup?(as_type) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/metaschema/type_mapper.rb', line 57

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

.multiline?(as_type) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/metaschema/type_mapper.rb', line 61

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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/metaschema/type_mapper.rb', line 74

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