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

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

Constant Summary

Constants inherited from Value

Value::EMPTY_OPTIONS

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Methods inherited from Value

format_type_serializer_for, from_format, #initialize, #initialized?, register_format_to_from_methods, register_format_type_serializer, #to_s

Methods included from Xml::Type::Configurable

included

Methods included from UninitializedClassGuard

#cast, #serialize

Constructor Details

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

Class Method Details

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lutaml/model/type/decimal.rb', line 7

def self.cast(value, options = {})
  return nil if value.nil?
  return value if Utils.uninitialized?(value)

  check_dependencies!(value)
  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
  # Use identity check for EMPTY_OPTIONS (faster than .empty?)
  unless options.equal?(EMPTY_OPTIONS)
    Model::Services::Type::Validator::Number.validate!(value,
                                                       options)
  end
  value
rescue ArgumentError
  nil
end

.check_dependencies!(value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lutaml/model/type/decimal.rb', line 48

def self.check_dependencies!(value)
  return if defined?(BigDecimal)

  begin
    require "bigdecimal"
  rescue LoadError
    nil
  end
  unless defined?(BigDecimal)
    raise TypeNotEnabledError.new("Decimal",
                                  value)
  end
end

.default_xsd_typeString

XSD type for Decimal

Returns:



44
45
46
# File 'lib/lutaml/model/type/decimal.rb', line 44

def self.default_xsd_type
  "xs:decimal"
end

.serialize(value) ⇒ Object

# xs:decimal format



31
32
33
34
35
36
37
38
39
# File 'lib/lutaml/model/type/decimal.rb', line 31

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

  check_dependencies!(value)

  return value.to_s("F") if value.is_a?(BigDecimal)

  value&.to_s
end