Class: RiCal::PropertyValue::Date

Inherits:
RiCal::PropertyValue show all
Defined in:
lib/ri_cal/property_value/date.rb

Overview

  • ©2009 Rick DeNatale

  • All rights reserved. Refer to the file README.txt for the license

RiCal::PropertyValue::CalAddress represents an icalendar Date property value which is defined in RFC 2445 section 4.3.4 p 34

Instance Attribute Summary

Attributes inherited from RiCal::PropertyValue

#params, #timezone_finder

Instance Method Summary collapse

Methods inherited from RiCal::PropertyValue

#==, convert, date_or_date_time, #enumerator, #equality_value, #for_parent, from_string, #initialize, #parms_string, #to_options_hash, #to_ri_cal_property_value, #to_s, #validate_value

Constructor Details

This class inherits a constructor from RiCal::PropertyValue

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(selector, *args) ⇒ Object

Delegate unknown messages to the wrappered Date instance. TODO: Is this really necessary?



107
108
109
# File 'lib/ri_cal/property_value/date.rb', line 107

def method_missing(selector, *args) #:nodoc:
  @date_time_value.send(selector, *args)
end

Instance Method Details

#add_date_times_to(required_timezones) ⇒ Object

:nodoc:



100
101
102
# File 'lib/ri_cal/property_value/date.rb', line 100

def add_date_times_to(required_timezones) #:nodoc:
  # Do nothing since dates don't have a timezone
end

#advance(options) ⇒ Object

:nodoc:



92
93
94
# File 'lib/ri_cal/property_value/date.rb', line 92

def advance(options) #:nodoc:
  PropertyValue::Date.new(timezone_finder, :value => compute_advance(@date_time_value, options), :params =>(params ? params.dup : nil) )
end

#change(options) ⇒ Object

:nodoc:



96
97
98
# File 'lib/ri_cal/property_value/date.rb', line 96

def change(options) #:nodoc:
  PropertyValue::Date.new(timezone_finder,:value => compute_change(@date_time_value, options), :params => (params ? params.dup : nil) )
end

#compute_advance(d, options) ⇒ Object

:nodoc:



84
85
86
87
88
89
90
# File 'lib/ri_cal/property_value/date.rb', line 84

def compute_advance(d, options) #:nodoc:
  d = d >> options[:years] * 12 if options[:years]
  d = d >> options[:months]     if options[:months]
  d = d +  options[:weeks] * 7  if options[:weeks]
  d = d +  options[:days]       if options[:days]
  compute_change(@date_time_value, :year => d.year, :month => d.month, :day => d.day)
end

#compute_change(d, options) ⇒ Object

:nodoc:



80
81
82
# File 'lib/ri_cal/property_value/date.rb', line 80

def compute_change(d, options) #:nodoc:
  ::Date.civil((options[:year] || d.year), (options[:month] || d.month), (options[:day] || d.day))
end

#dayObject

Returns the day of the month



54
55
56
# File 'lib/ri_cal/property_value/date.rb', line 54

def day
  @date_time_value.day
end

#monthObject

Returns the month of the year (1..12)



49
50
51
# File 'lib/ri_cal/property_value/date.rb', line 49

def month
  @date_time_value.month
end

#occurrence_hash(default_duration) ⇒ Object

TODO: consider if this should be a period rather than a hash



112
113
114
115
116
# File 'lib/ri_cal/property_value/date.rb', line 112

def occurrence_hash(default_duration) #:nodoc:
date_time = self.to_ri_cal_date_time_value
{:start => date_time,
  :end => date_time.advance(:hours => 24, :seconds => -1)}
end

#ruby_valueObject Also known as: to_ri_cal_ruby_value

Returns the ruby representation a ::Date



59
60
61
# File 'lib/ri_cal/property_value/date.rb', line 59

def ruby_value
  ::Date.parse(@date_time_value.strftime("%Y%m%d"))
end

#to_ri_cal_date_or_date_time_valueObject

Return the “Natural’ property value for the date_property, in this case the date property itself.”



76
77
78
# File 'lib/ri_cal/property_value/date.rb', line 76

def to_ri_cal_date_or_date_time_value
  self
end

#to_ri_cal_date_time_valueObject

Return an instance of RiCal::PropertyValue::DateTime representing the start of this date



66
67
68
# File 'lib/ri_cal/property_value/date.rb', line 66

def to_ri_cal_date_time_value
  PropertyValue::DateTime.new(:value => @date_time_value)
end

#to_ri_cal_date_valueObject

Return this date property



71
72
73
# File 'lib/ri_cal/property_value/date.rb', line 71

def to_ri_cal_date_value
  self
end

#valueObject

Returns the value of the reciever as an RFC 2445 iCalendar string



12
13
14
15
16
17
18
# File 'lib/ri_cal/property_value/date.rb', line 12

def value
  if @date_time_value
    @date_time_value.strftime("%Y%m%d")
  else
    nil
  end
end

#value=(val) ⇒ Object

Set the value of the property to val

val may be either:

  • A string which can be parsed as a DateTime

  • A Time instance

  • A Date instance

  • A DateTime instance



28
29
30
31
32
33
34
35
36
37
# File 'lib/ri_cal/property_value/date.rb', line 28

def value=(val)
  case val
  when nil
    @date_time_value = nil
  when String
    @date_time_value = ::DateTime.parse(::DateTime.parse(val).strftime("%Y%m%d"))
  when ::Time, ::Date, ::DateTime
    @date_time_value = ::DateTime.parse(val.strftime("%Y%m%d"))
  end
end

#visible_paramsObject

:nodoc:



39
40
41
# File 'lib/ri_cal/property_value/date.rb', line 39

def visible_params #:nodoc:
  {"VALUE" => "DATE"}.merge(params)
end

#yearObject

Returns the year (including the century)



44
45
46
# File 'lib/ri_cal/property_value/date.rb', line 44

def year
  @date_time_value.year
end