Module: Lutaml::Model::Type

Defined in:
lib/lutaml/model/type.rb,
lib/lutaml/model/union.rb,
lib/lutaml/model/type/uri.rb,
lib/lutaml/model/type/date.rb,
lib/lutaml/model/type/hash.rb,
lib/lutaml/model/type/time.rb,
lib/lutaml/model/type/float.rb,
lib/lutaml/model/type/qname.rb,
lib/lutaml/model/type/value.rb,
lib/lutaml/model/type/string.rb,
lib/lutaml/model/type/symbol.rb,
lib/lutaml/model/type/boolean.rb,
lib/lutaml/model/type/decimal.rb,
lib/lutaml/model/type/integer.rb,
lib/lutaml/model/type/duration.rb,
lib/lutaml/model/type/date_time.rb,
lib/lutaml/model/type/reference.rb,
lib/lutaml/model/type/hex_binary.rb,
lib/lutaml/model/type/base64_binary.rb,
lib/lutaml/model/type/time_without_date.rb,
lib/lutaml/model/error/type/max_bound_error.rb,
lib/lutaml/model/error/type/min_bound_error.rb,
lib/lutaml/model/error/type/max_length_error.rb,
lib/lutaml/model/error/type/min_length_error.rb,
lib/lutaml/model/error/type/invalid_value_error.rb,
lib/lutaml/model/type/uninitialized_class_guard.rb,
lib/lutaml/model/error/type/pattern_not_matched_error.rb

Defined Under Namespace

Modules: UninitializedClassGuard Classes: Base64Binary, Boolean, Date, DateTime, Decimal, Duration, Float, Hash, HexBinary, Integer, InvalidValueError, MaxBoundError, MaxLengthError, MinBoundError, MinLengthError, PatternNotMatchedError, QName, Reference, String, Symbol, Time, TimeWithoutDate, Union, Uri, Value

Constant Summary collapse

TYPE_CODES =
{
  string: "Lutaml::Model::Type::String",
  integer: "Lutaml::Model::Type::Integer",
  float: "Lutaml::Model::Type::Float",
  double: "Lutaml::Model::Type::Float",
  decimal: "Lutaml::Model::Type::Decimal",
  date: "Lutaml::Model::Type::Date",
  time: "Lutaml::Model::Type::Time",
  date_time: "Lutaml::Model::Type::DateTime",
  time_without_date: "Lutaml::Model::Type::TimeWithoutDate",
  boolean: "Lutaml::Model::Type::Boolean",
  reference: "Lutaml::Model::Type::Reference",
  hash: "Lutaml::Model::Type::Hash",
  symbol: "Lutaml::Model::Type::Symbol",
  duration: "Lutaml::Model::Type::Duration",
  uri: "Lutaml::Model::Type::Uri",
  qname: "Lutaml::Model::Type::QName",
  base64_binary: "Lutaml::Model::Type::Base64Binary",
  hex_binary: "Lutaml::Model::Type::HexBinary",
}.freeze
BUILTIN_CLASSES =
TYPE_CODES.each_value.map { |type_class_name| const_get(type_class_name) }.freeze

Class Method Summary collapse

Class Method Details

.builtin?(type) ⇒ Boolean



64
65
66
# File 'lib/lutaml/model/type.rb', line 64

def builtin?(type)
  BUILTIN_CLASSES.include?(type)
end

.cast(value, type) ⇒ Object

Cast a value to the specified type.



147
148
149
150
151
# File 'lib/lutaml/model/type.rb', line 147

def cast(value, type)
  return nil if value.nil?

  lookup(type).cast(value)
end

.lookup(type_name) ⇒ Class

Look up a type class by name.

Raises:

  • If type is not found



119
120
121
122
123
124
125
126
127
128
# File 'lib/lutaml/model/type.rb', line 119

def lookup(type_name)
  return type_name if type_name.is_a?(Class)

  @registry ||= {}
  klass = @registry[type_name.to_sym]

  raise UnknownTypeError.new(type_name) unless klass

  klass
end

.lookup_ignoring_fallback(type_name) ⇒ Class?

Look up a type class by name, returning nil if not found. Used by TypeResolver for backward compatibility fallback.



135
136
137
138
139
140
# File 'lib/lutaml/model/type.rb', line 135

def lookup_ignoring_fallback(type_name)
  return type_name if type_name.is_a?(Class)

  @registry ||= {}
  @registry[type_name.to_sym]
end

.register(type_name, type_class) ⇒ void

This method returns an undefined value.

Register a type in the Type module's internal registry.

Raises:

  • If type_class is not a valid type class



104
105
106
107
108
109
110
111
112
# File 'lib/lutaml/model/type.rb', line 104

def register(type_name, type_class)
  unless type_class < Value
    raise TypeError,
          "class '#{type_class}' is not a valid Lutaml::Model::Type::Value"
  end

  @registry ||= {}
  @registry[type_name.to_sym] = type_class
end

.register_builtin_typesvoid

Deprecated.

This method returns an undefined value.

Legacy: Register built-in types into Type module's internal registry.



92
93
94
95
96
# File 'lib/lutaml/model/type.rb', line 92

def register_builtin_types
  TYPE_CODES.each do |type_name, type_class|
    register(type_name, const_get(type_class))
  end
end

.register_builtin_types_in(registry) ⇒ void

This method returns an undefined value.

Register all built-in types into any TypeRegistry.

This method is used by the new type resolution architecture. It does NOT depend on the Type module's internal @registry.

Examples:

registry = TypeRegistry.new
Type.register_builtin_types_in(registry)
registry.lookup(:string)  #=> Lutaml::Model::Type::String


80
81
82
83
84
85
86
# File 'lib/lutaml/model/type.rb', line 80

def register_builtin_types_in(registry)
  TYPE_CODES.each do |type_name, type_class_name|
    # Resolve the class constant directly (not via @registry)
    type_class = const_get(type_class_name)
    registry.register(type_name, type_class)
  end
end

.serialize(value, type) ⇒ Object

Serialize a value using the specified type.



158
159
160
161
162
# File 'lib/lutaml/model/type.rb', line 158

def serialize(value, type)
  return nil if value.nil?

  lookup(type).serialize(value)
end