Sha256: 2f53bd819a9c93bef2c1a990bb17a4b4e5216f45de09c5b2972c9292c72f2e6f

Contents?: true

Size: 788 Bytes

Versions: 6

Compression:

Stored size: 788 Bytes

Contents

# frozen_string_literal: true

using SmartCore::Ext::BasicObjectAsObject

# @api public
# @since 0.1.0
# @version 0.3.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

6 entries across 6 versions & 1 rubygems

Version Path
smart_types-0.8.0 lib/smart_core/types/value/integer.rb
smart_types-0.7.1 lib/smart_core/types/value/integer.rb
smart_types-0.7.0 lib/smart_core/types/value/integer.rb
smart_types-0.6.0 lib/smart_core/types/value/integer.rb
smart_types-0.4.0 lib/smart_core/types/value/integer.rb
smart_types-0.3.0 lib/smart_core/types/value/integer.rb