Class: ActiveAttr::Typecasting::DateTypecaster

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

Overview

Typecasts an Object to a Date

Examples:

Usage

DateTypecaster.new.call("2012-01-01") #=> Sun, 01 Jan 2012

Since:

  • 0.5.0

Instance Method Summary collapse

Instance Method Details

#call(value) ⇒ Date?

Typecasts an object to a Date

Attempts to convert using #to_date.

Examples:

Typecast a String

typecaster.call("2012-01-01") #=> Sun, 01 Jan 2012

Parameters:

  • value (Object, #to_date)

    The object to typecast

Returns:

  • (Date, nil)

    The result of typecasting

Since:

  • 0.5.0



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

def call(value)
  value.to_date if value.respond_to? :to_date
rescue NoMethodError, ArgumentError
end