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'

Constants inherited from Icalendar::Value

Icalendar::Value::VALUE_TYPE_GSUB_REGEX_1, Icalendar::Value::VALUE_TYPE_GSUB_REGEX_2

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.



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

def initialize(value, params = {})
  params.delete 'tzid'
  params.delete 'x-tz-store'
  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



33
34
35
36
37
38
39
# File 'lib/icalendar/values/date.rb', line 33

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



29
30
31
# File 'lib/icalendar/values/date.rb', line 29

def value_ical
  value.strftime FORMAT
end