Sha256: 226ddc53c65780aeb5ce6689e12dc7f4a4a5034f36f8e7c7eb841920fba844d4

Contents?: true

Size: 961 Bytes

Versions: 22

Compression:

Stored size: 961 Bytes

Contents

module Lutaml
  module Model
    module Type
      class Integer < Value
        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

        # Override serialize to return Integer instead of String
        def self.serialize(value)
          return nil if value.nil?

          cast(value)
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
lutaml-model-0.7.1 lib/lutaml/model/type/integer.rb
lutaml-model-0.7.0 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.7 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.6 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.5 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.4 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.3 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.2 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.1 lib/lutaml/model/type/integer.rb
lutaml-model-0.6.0 lib/lutaml/model/type/integer.rb
lutaml-model-0.5.4 lib/lutaml/model/type/integer.rb
lutaml-model-0.5.3 lib/lutaml/model/type/integer.rb
lutaml-model-0.5.2 lib/lutaml/model/type/integer.rb
lutaml-model-0.5.1 lib/lutaml/model/type/integer.rb
lutaml-model-0.5.0 lib/lutaml/model/type/integer.rb
lutaml-model-0.4.0 lib/lutaml/model/type/integer.rb
lutaml-model-0.3.30 lib/lutaml/model/type/integer.rb
lutaml-model-0.3.29 lib/lutaml/model/type/integer.rb
lutaml-model-0.3.28 lib/lutaml/model/type/integer.rb
lutaml-model-0.3.27 lib/lutaml/model/type/integer.rb