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

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

Instance Attribute Summary

Attributes inherited from Value

#value

Class Method Summary collapse

Methods inherited from Value

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

Constructor Details

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

Class Method Details

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



5
6
7
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
# File 'lib/lutaml/model/type/integer.rb', line 5

def self.cast(value, options = {})
  return nil if value.nil?
  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

  Model::Services::Type::Validator::Number.validate!(value, options)
  value
end

.serialize(value) ⇒ Object

Override serialize to return Integer instead of String



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

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

  cast(value)
end