Module: Coercible::Coercer::TimeCoercions

Included in:
Date, DateTime, Time
Defined in:
lib/coercible/coercer/time_coercions.rb

Overview

Common time coercion methods

Instance Method Summary collapse

Instance Method Details

#to_date(value) ⇒ Date

Coerce given value to Date

Examples:

coercer[Time].to_date(time)  # => Date object

Parameters:

Returns:



59
60
61
# File 'lib/coercible/coercer/time_coercions.rb', line 59

def to_date(value)
  coerce_with_method(value, :to_date)
end

#to_datetime(value) ⇒ DateTime

Coerce given value to DateTime

Examples:

coercer[Time].to_datetime(time)  # => DateTime object

Parameters:

Returns:



45
46
47
# File 'lib/coercible/coercer/time_coercions.rb', line 45

def to_datetime(value)
  coerce_with_method(value, :to_datetime)
end

#to_string(value) ⇒ String

Coerce given value to String

Examples:

coercer[Time].to_string(time)  # => "Wed Jul 20 10:30:41 -0700 2011"

Parameters:

Returns:



17
18
19
# File 'lib/coercible/coercer/time_coercions.rb', line 17

def to_string(value)
  value.to_s
end

#to_time(value) ⇒ Time

Coerce given value to Time

Examples:

coercer[DateTime].to_time(datetime)  # => Time object

Parameters:

Returns:



31
32
33
# File 'lib/coercible/coercer/time_coercions.rb', line 31

def to_time(value)
  coerce_with_method(value, :to_time)
end