Class: DataMapper::Property::Legacy::DateString

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

Instance Method Summary collapse

Instance Method Details

#dump(value) ⇒ String?

Dumps a date to a string.

Parameters:

  • value (Date, nil)

    The date to dump.

Returns:

  • (String, nil)

    The date string.



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

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

#load(value) ⇒ Date?

Parses a date string.

Parameters:

  • value (String)

    The date string.

Returns:

  • (Date, nil)

    The parsed date.



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

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

#typecast(value) ⇒ Date?

Typecasts a date.

Parameters:

  • value (Date, Time, String, nil)

    The date to typecast.

Returns:

  • (Date, nil)

    The typecasted date.



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

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