Class: ActiveAttr::Typecasting::FloatTypecaster

Inherits:
Object
  • Object
show all
Defined in:
lib/active_attr/typecasting/float_typecaster.rb

Overview

Typecasts an Object to a Float

Examples:

Usage

FloatTypecaster.new.call(1) #=> 1.0

Since:

  • 0.5.0

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Float?

Typecasts an object to a Float

Attempts to convert using #to_f.

Examples:

Typecast an Integer

typecaster.call(1) #=> 1.0

Parameters:

  • value (Object, #to_f)

    The object to typecast

Returns:

  • (Float, nil)

    The result of typecasting

Since:

  • 0.5.0



24
25
26
# File 'lib/active_attr/typecasting/float_typecaster.rb', line 24

def call(value)
  value.to_f if value.present? && value.respond_to?(:to_f)
end