Sha256: d337c7068311b2d961f43da8f3fd11fd36ef59a15ce74ff3628119ef42ccf4b4

Contents?: true

Size: 728 Bytes

Versions: 2

Compression:

Stored size: 728 Bytes

Contents

# frozen_string_literal: true

# @api public
# @since 0.1.0
SmartCore::Types::Value.define_type(:Integer) do |type|
  type.define_checker do |value|
    value.is_a?(::Integer)
  end

  type.define_caster do |value|
    begin
      ::Kernel.Integer(value)
    rescue ::TypeError, ::ArgumentError, ::FloatDomainError
      begin
        # NOTE: for cases like this:
        # => ::Kernel.Integer(nil) # => ::TypeError
        # => ::Kernel.Integer(nil.to_i) # => 0 (::Kernel.Integer used as validation layer)
        ::Kernel.Integer(value.to_i)
      rescue ::TypeError, ::NoMethodError, ::ArgumentError, ::FloatDomainError
        raise(SmartCore::Types::TypeCastingError, 'Non-castable to Integer')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smart_types-0.2.0 lib/smart_core/types/value/integer.rb
smart_types-0.1.0 lib/smart_core/types/value/integer.rb