Class: ShallowAttributes::Type::Time Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/shallow_attributes/type/time.rb

Overview

This class is abstract.

Abstract class for typecast object to Time type.

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#coerce(value, _options = {}) ⇒ Time

Convert value to Time type

Examples:

Convert string to Time value

ShallowAttributes::Type::Time.new.coerce('Thu Nov 29 14:33:20 GMT 2001')
  # => '2001-11-29 14:33:20 +0000'

Parameters:

  • value (Object)
  • option (Hash)

Returns:

Raises:

Since:

  • 0.1.0


25
26
27
28
29
30
31
32
33
34
# File 'lib/shallow_attributes/type/time.rb', line 25

def coerce(value, _options = {})
  case value
  when ::Time then value
  when ::Integer then ::Time.at(value)
  else
    ::Time.parse(value.to_s)
  end
rescue
  raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Time")
end