Class: Lutaml::Model::Type::Value

Inherits:
Object
  • Object
show all
Includes:
UninitializedClassGuard, Xml::Type::Configurable
Defined in:
lib/lutaml/model/type/value.rb

Overview

Base class for all value types

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Xml::Type::Configurable

included

Methods included from UninitializedClassGuard

#cast, #serialize

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

#valueObject (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, _options = {})
  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.

Parameters:

  • format (Symbol)

    the format

  • type_class (Class)

    the type class to look up

Returns:

  • (Hash, nil)

    { to: Proc, from: Proc } or nil



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.

Parameters:

  • format (Symbol)

    the format (e.g., :xml, :json)

  • type_class (Class)

    the type class (must be <= Value)

  • to (Proc, nil) (defaults to: nil)

    custom instance serialization proc (receives the type instance)

  • from (Proc, nil) (defaults to: nil)

    custom class deserialization proc (receives the raw value)



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

Returns:



55
56
57
# File 'lib/lutaml/model/type/value.rb', line 55

def initialized?
  true
end

#to_sObject

Instance methods for serialization



74
75
76
# File 'lib/lutaml/model/type/value.rb', line 74

def to_s
  value.to_s
end