Class: ActiveAttr::Typecasting::DateTimeTypecaster

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

Overview

Typecasts an Object to a DateTime

Examples:

Usage

typecaster = DateTimeTypecaster.new
typecaster.call("2012-01-01") #=> Sun, 01 Jan 2012 00:00:00 +0000

Since:

  • 0.5.0

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ DateTime?

Typecasts an object to a DateTime

Attempts to convert using #to_datetime.

Examples:

Typecast a String

typecaster.call("2012-01-01") #=> Sun, 01 Jan 2012 00:00:00 +0000

Parameters:

  • value (Object, #to_datetime)

    The object to typecast

Returns:

  • (DateTime, nil)

    The result of typecasting

Since:

  • 0.5.0



26
27
28
29
# File 'lib/active_attr/typecasting/date_time_typecaster.rb', line 26

def call(value)
  value.to_datetime if value.respond_to? :to_datetime
rescue ArgumentError
end