Class: Lutaml::Model::Type::Decimal

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

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

from_format, #initialize, register_format_to_from_methods, #to_s

Constructor Details

This class inherits a constructor from Lutaml::Model::Type::Value

Class Method Details

.cast(value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lutaml/model/type/decimal.rb', line 5

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

  check_dependencies!(value)
  case value
  when BigDecimal
    # If already a BigDecimal, return as-is
    value
  else
    # Convert to string first to handle various input types
    BigDecimal(value.to_s)
  end
rescue ArgumentError
  nil
end

.check_dependencies!(value) ⇒ Object



30
31
32
33
34
# File 'lib/lutaml/model/type/decimal.rb', line 30

def self.check_dependencies!(value)
  unless defined?(BigDecimal)
    raise TypeNotEnabledError.new("Decimal", value)
  end
end

.serialize(value) ⇒ Object

# xs:decimal format



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

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

  check_dependencies!(value)
  value = cast(value)
  value.to_s("F") # Use fixed-point notation to match test expectations
end

Instance Method Details

#to_yamlObject

Override to avoid serializing ruby object in YAML



37
38
39
# File 'lib/lutaml/model/type/decimal.rb', line 37

def to_yaml
  value&.to_s("F")
end