Class: Typed::Coercion::IntegerCoercer

Inherits:
Coercer
  • Object
show all
Extended by:
T::Generic
Defined in:
lib/typed/coercion/integer_coercer.rb

Constant Summary collapse

Target =
type_member { {fixed: Integer} }

Instance Method Summary collapse

Instance Method Details

#coerce(type:, value:) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/typed/coercion/integer_coercer.rb', line 16

def coerce(type:, value:)
  return Failure.new(CoercionError.new("Type must be a Integer.")) unless used_for_type?(type)

  Success.new(Integer(value))
rescue ArgumentError, TypeError
  Failure.new(CoercionError.new("'#{value}' cannot be coerced into Integer."))
end

#used_for_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/typed/coercion/integer_coercer.rb', line 11

def used_for_type?(type)
  type == T::Utils.coerce(Integer)
end