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
Class Method Summary collapse
-
.cast(value, type) ⇒ Object
Cast a value to the specified type.
-
.lookup(type_name) ⇒ Class
Look up a type class by name.
-
.lookup_ignoring_fallback(type_name) ⇒ Class?
Look up a type class by name, returning nil if not found.
-
.register(type_name, type_class) ⇒ void
Register a type in the Type module's internal registry.
-
.register_builtin_types ⇒ void
deprecated
Deprecated.
Use Type.register_builtin_types_in instead
-
.register_builtin_types_in(registry) ⇒ void
Register all built-in types into any TypeRegistry.
-
.serialize(value, type) ⇒ Object
Serialize a value using the specified type.
Class Method Details
.cast(value, type) ⇒ Object
Cast a value to the specified type.
138 139 140 141 142 |
# File 'lib/lutaml/model/type.rb', line 138 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.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lutaml/model/type.rb', line 110 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.
126 127 128 129 130 131 |
# File 'lib/lutaml/model/type.rb', line 126 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.
95 96 97 98 99 100 101 102 103 |
# File 'lib/lutaml/model/type.rb', line 95 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_types ⇒ void
Use register_builtin_types_in instead
This method returns an undefined value.
Legacy: Register built-in types into Type module's internal registry.
83 84 85 86 87 |
# File 'lib/lutaml/model/type.rb', line 83 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.
71 72 73 74 75 76 77 |
# File 'lib/lutaml/model/type.rb', line 71 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.
149 150 151 152 153 |
# File 'lib/lutaml/model/type.rb', line 149 def serialize(value, type) return nil if value.nil? lookup(type).serialize(value) end |