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.



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

def initialize(value)
  @value = self.class.cast(value)
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



71
72
73
# File 'lib/lutaml/model/type/value.rb', line 71

def value
  @value
end

Class Method Details

.cast(value, _options = {}) ⇒ Object



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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lutaml/model/type/value.rb', line 47

def format_type_serializer_for(format, type_class)
  # Hot path: memoize per [format, resolved class] — the hierarchy
  # walk below only runs on a miss. Format serializers register at
  # load time, so a hit is stable.
  cache = SERIALIZER_LOOKUP_CACHE
  key = [format, type_class]
  return cache[key] if cache.key?(key)

  klass = type_class
  found = nil
  while klass && klass <= Value
    s = @format_type_serializers[[format, klass]]
    if s
      found = s
      break
    end

    klass = klass.superclass
  end
  cache[key] = found
  found
end

.from_format(value, format) ⇒ Object

Class-level format conversion



101
102
103
# File 'lib/lutaml/model/type/value.rb', line 101

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.



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

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)



36
37
38
39
40
# File 'lib/lutaml/model/type/value.rb', line 36

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



88
89
90
91
92
93
# File 'lib/lutaml/model/type/value.rb', line 88

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:



77
78
79
# File 'lib/lutaml/model/type/value.rb', line 77

def initialized?
  true
end

#to_sObject

Instance methods for serialization



96
97
98
# File 'lib/lutaml/model/type/value.rb', line 96

def to_s
  value.to_s
end