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

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/type/value.rb

Overview

Base class for all value types

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Value

Returns a new instance of Value.



10
11
12
# File 'lib/lutaml/model/type/value.rb', line 10

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

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/lutaml/model/type/value.rb', line 8

def value
  @value
end

Class Method Details

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



18
19
20
21
22
# File 'lib/lutaml/model/type/value.rb', line 18

def self.cast(value, _options = {})
  return nil if value.nil?

  value
end

.from_format(value, format) ⇒ Object

Class-level format conversion



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

def self.from_format(value, format)
  new(send(:"from_#{format}", value))
end

.register_format_to_from_methods(format) ⇒ Object

called from config when a new format is added



41
42
43
44
45
46
47
48
49
# File 'lib/lutaml/model/type/value.rb', line 41

def self.register_format_to_from_methods(format)
  define_method(:"to_#{format}") do
    value
  end

  define_singleton_method(:"from_#{format}") do |value|
    cast(value)
  end
end

.serialize(value) ⇒ Object



24
25
26
27
28
# File 'lib/lutaml/model/type/value.rb', line 24

def self.serialize(value)
  return nil if value.nil?

  new(value).to_s
end

Instance Method Details

#initialized?Boolean

Returns:



14
15
16
# File 'lib/lutaml/model/type/value.rb', line 14

def initialized?
  true
end

#to_sObject

Instance methods for serialization



31
32
33
# File 'lib/lutaml/model/type/value.rb', line 31

def to_s
  value.to_s
end