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:

Instance Method Summary (collapse)

Instance Method Details

- (Integer?) call(value)

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



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

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