Class: ActiveAttr::Typecasting::IntegerTypecaster

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

Overview

Typecasts an Object to an Integer

Examples:

Usage

IntegerTypecaster.new.call("1") #=> 1

Since:

  • 0.5.0

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Integer?

Typecasts an object to an Integer

Attempts to convert using #to_i. Handles FloatDomainError if the object is INFINITY or NAN.

Examples:

Typecast a String

typecaster.call("1") #=> 1

Parameters:

  • value (Object, #to_i)

    The object to typecast

Returns:

  • (Integer, nil)

    The result of typecasting

Since:

  • 0.5.0



25
26
27
28
# File 'lib/active_attr/typecasting/integer_typecaster.rb', line 25

def call(value)
  value.to_i if value.present? && value.respond_to?(:to_i)
rescue FloatDomainError
end