Module: Lutaml::Model::Type

Defined in:
lib/lutaml/model/type.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, 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

Class Method Summary collapse

Class Method Details

.cast(value, type) ⇒ Object

Cast a value to the specified type.

Parameters:

  • value (Object)

    The value to cast

  • type (Symbol, String, Class)

    The target type

Returns:

  • (Object)

    The cast value



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

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.

Parameters:

  • type_name (Symbol, String, Class)

    The type name or class

Returns:

  • (Class)

    The type class

Raises:



109
110
111
112
113
114
115
116
117
118
# File 'lib/lutaml/model/type.rb', line 109

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.

Parameters:

  • type_name (Symbol, String, Class)

    The type name or class

Returns:

  • (Class, nil)

    The type class or nil if not found



125
126
127
128
129
130
# File 'lib/lutaml/model/type.rb', line 125

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.

Parameters:

  • type_name (Symbol, String)

    The type name

  • type_class (Class)

    The type class (must inherit from Value)

Raises:

  • (TypeError)

    If type_class is not a valid type class



94
95
96
97
98
99
100
101
102
# File 'lib/lutaml/model/type.rb', line 94

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.



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

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

Parameters:



70
71
72
73
74
75
76
# File 'lib/lutaml/model/type.rb', line 70

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.

Parameters:

  • value (Object)

    The value to serialize

  • type (Symbol, String, Class)

    The type

Returns:

  • (Object)

    The serialized value



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

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

  lookup(type).serialize(value)
end