Class: Lutaml::Model::Type::Value
- Inherits:
-
Object
- Object
- Lutaml::Model::Type::Value
- Includes:
- UninitializedClassGuard, Xml::Type::Configurable
- Defined in:
- lib/lutaml/model/type/value.rb
Overview
Base class for all value types
Direct Known Subclasses
Boolean, Date, DateTime, Decimal, Duration, Float, Hash, Integer, QName, Reference, String, Symbol, Time, TimeWithoutDate
Constant Summary collapse
- EMPTY_OPTIONS =
Performance optimization: reusable empty options hash Use options.equal?(EMPTY_OPTIONS) for fast-path checks
{}.freeze
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .cast(value, _options = {}) ⇒ Object
-
.format_type_serializer_for(format, type_class) ⇒ Hash?
Look up a format type serializer, walking the class hierarchy.
-
.from_format(value, format) ⇒ Object
Class-level format conversion.
-
.register_format_to_from_methods(format) ⇒ Object
Called from FormatRegistry when a new format is registered.
-
.register_format_type_serializer(format, type_class, to: nil, from: nil) ⇒ Object
Register a custom type serializer for a specific format and type class.
- .serialize(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(value) ⇒ Value
constructor
A new instance of Value.
- #initialized? ⇒ Boolean
-
#to_s ⇒ Object
Instance methods for serialization.
Methods included from Xml::Type::Configurable
Methods included from UninitializedClassGuard
Constructor Details
#initialize(value) ⇒ Value
Returns a new instance of Value.
51 52 53 |
# File 'lib/lutaml/model/type/value.rb', line 51 def initialize(value) @value = self.class.cast(value) end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
49 50 51 |
# File 'lib/lutaml/model/type/value.rb', line 49 def value @value end |
Class Method Details
.cast(value, _options = {}) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/lutaml/model/type/value.rb', line 59 def self.cast(value, = {}) return nil if value.nil? return value if Utils.uninitialized?(value) value end |
.format_type_serializer_for(format, type_class) ⇒ Hash?
Look up a format type serializer, walking the class hierarchy.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lutaml/model/type/value.rb', line 37 def format_type_serializer_for(format, type_class) klass = type_class while klass && klass <= Value s = @format_type_serializers[[format, klass]] return s if s klass = klass.superclass end nil end |
.from_format(value, format) ⇒ Object
Class-level format conversion
79 80 81 |
# File 'lib/lutaml/model/type/value.rb', line 79 def self.from_format(value, format) new(send(:"from_#{format}", value)) end |
.register_format_to_from_methods(format) ⇒ Object
Called from FormatRegistry when a new format is registered. Defines to_format and from_format methods that check the serializer registry first, falling back to default behavior.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/lutaml/model/type/value.rb', line 86 def self.register_format_to_from_methods(format) define_method(:"to_#{format}") do s = Value.format_type_serializer_for(format, self.class) s&.dig(:to) ? s[:to].call(self) : value end define_singleton_method(:"from_#{format}") do |v| s = Value.format_type_serializer_for(format, self) s&.dig(:from) ? s[:from].call(v) : cast(v) end end |
.register_format_type_serializer(format, type_class, to: nil, from: nil) ⇒ Object
Register a custom type serializer for a specific format and type class. Format plugins call this at load time to register their custom serialization logic.
26 27 28 29 30 |
# File 'lib/lutaml/model/type/value.rb', line 26 def register_format_type_serializer(format, type_class, to: nil, from: nil) @format_type_serializers[[format, type_class]] = { to: to, from: from }.compact end |
.serialize(value) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/lutaml/model/type/value.rb', line 66 def self.serialize(value) return nil if value.nil? return value if Utils.uninitialized?(value) new(value).to_s end |
Instance Method Details
#initialized? ⇒ Boolean
55 56 57 |
# File 'lib/lutaml/model/type/value.rb', line 55 def initialized? true end |
#to_s ⇒ Object
Instance methods for serialization
74 75 76 |
# File 'lib/lutaml/model/type/value.rb', line 74 def to_s value.to_s end |