Class: Lutaml::Model::Type::Integer

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/integer.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

Performance-optimized cast with short-circuit for already-correct types



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lutaml/model/type/integer.rb', line 8

def self.cast(value, options = {})
  return nil if value.nil?
  return value if Utils.uninitialized?(value)
  # Short-circuit: return immediately if already an Integer
  # Use identity check for EMPTY_OPTIONS (faster than .empty?)
  return value if value.is_a?(::Integer) && options.equal?(EMPTY_OPTIONS)

  return 1 if value === true
  return 0 if value === false

  value = case value
          when ::String
            if value.match?(/^0[0-7]+$/) # Octal
              value.to_i(8)
            elsif value.match?(/^-?\d+(\.\d+)?(e-?\d+)?$/i) # Float/exponential
              Float(value).to_i
            else
              begin
                Integer(value, 10)
              rescue StandardError
                nil
              end
            end
          else
            begin
              Integer(value)
            rescue StandardError
              nil
            end
          end

  unless options.equal?(EMPTY_OPTIONS)
    Model::Services::Type::Validator::Number.validate!(value,
                                                       options)
  end
  value
end

.default_xsd_typeString

Default XSD type for Integer

Returns:



56
57
58
# File 'lib/lutaml/model/type/integer.rb', line 56

def self.default_xsd_type
  "xs:integer"
end

.serialize(value) ⇒ Object

Override serialize to return Integer instead of String



47
48
49
50
51
# File 'lib/lutaml/model/type/integer.rb', line 47

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

  cast(value)
end