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, #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
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lutaml/model/type/integer.rb', line 5

def self.cast(value)
  return nil if value.nil?
  return 1 if value === true
  return 0 if value === false

  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
end

.serialize(value) ⇒ Object

Override serialize to return Integer instead of String



33
34
35
36
37
# File 'lib/lutaml/model/type/integer.rb', line 33

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

  cast(value)
end