Class: Icalendar::Values::Date

Inherits:
Icalendar::Value show all
Defined in:
lib/icalendar/values/date.rb

Defined Under Namespace

Classes: FormatError

Constant Summary collapse

FORMAT =
'%Y%m%d'

Instance Attribute Summary

Attributes inherited from Icalendar::Value

#ical_params

Instance Method Summary collapse

Methods inherited from Icalendar::Value

#ical_param, #params_ical, #to_ical, #value, value_type, #value_type

Constructor Details

#initialize(value, params = {}) ⇒ Date

Returns a new instance of Date.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/icalendar/values/date.rb', line 9

def initialize(value, params = {})
  if value.is_a? String
    begin
      parsed_date = ::Date.strptime(value, FORMAT)
    rescue ArgumentError => e
      raise FormatError.new("Failed to parse \"#{value}\" - #{e.message}")
    end

    super parsed_date, params
  elsif value.respond_to? :to_date
    super value.to_date, params
  else
    super
  end
end

Instance Method Details

#<=>(other) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/icalendar/values/date.rb', line 29

def <=>(other)
  if other.is_a?(Icalendar::Values::Date) || other.is_a?(Icalendar::Values::DateTime)
    value_ical <=> other.value_ical
  else
    nil
  end
end

#value_icalObject


25
26
27
# File 'lib/icalendar/values/date.rb', line 25

def value_ical
  value.strftime FORMAT
end