Class: Typed::Coercion::FloatCoercer

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

Constant Summary collapse

Target =
type_member { {fixed: Float} }

Instance Method Summary collapse

Instance Method Details

#coerce(type:, value:) ⇒ Object



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

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

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

#used_for_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

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