Class: DataMapper::Property::Legacy::TimeString

Inherits:
String
  • Object
show all
Defined in:
lib/dm-core/property/legacy/time_string.rb

Instance Method Summary collapse

Instance Method Details

#dump(value) ⇒ String?

Dumps a time to a string.

Parameters:

  • value (Time, nil)

    The time to dump.

Returns:

  • (String, nil)

    The time string.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dm-core/property/legacy/time_string.rb', line 52

def dump(value)
  case value
  when ::Time
    value.to_s
  when ::Date
    value.to_time.to_s
  when nil
    nil
  else
    value.to_s
  end
end

#load(value) ⇒ Time?

Parses a time string.

Parameters:

  • value (String)

    The time string.

Returns:

  • (Time, nil)

    The parsed time.



20
21
22
# File 'lib/dm-core/property/legacy/time_string.rb', line 20

def load(value)
  ::Time.parse(value) unless (value.nil? || value.empty?)
end

#typecast(value) ⇒ Time?

Typecasts a time.

Parameters:

  • value (Time, Date, String, nil)

    The time to typecast.

Returns:

  • (Time, nil)

    The typecasted time.



33
34
35
36
37
38
39
40
41
# File 'lib/dm-core/property/legacy/time_string.rb', line 33

def typecast(value)
  if value.kind_of?(::Time)
    value
  elsif value.kind_of?(::Date)
    value.to_time
  elsif value.kind_of?(::String)
    ::Time.parse(value) unless value.empty?
  end
end