Class: RiCal::PropertyValue::RecurrenceRule::RecurringDay

Inherits:
Object
  • Object
show all
Defined in:
lib/ri_cal/property_value/recurrence_rule/recurring_day.rb

Overview

  • ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license

Instances of RecurringDay are used to represent values in BYDAY recurrence rule parts

Constant Summary collapse

DayNames =
%w{SU MO TU WE TH FR SA}
DayNums =
day_nums

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, rrule, scope = :monthly) ⇒ RecurringDay

Returns a new instance of RecurringDay.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 20

def initialize(source, rrule, scope = :monthly)
  @source = source
  @rrule = rrule
  @scope = scope
  wd_match = source.match(/([+-]?\d*)(SU|MO|TU|WE|TH|FR|SA)/)
  if wd_match
    @day, @ordinal = wd_match[2], wd_match[1]
    @wday = DayNums[@day]
    @index = (@ordinal == "") ? nil : @ordinal.to_i
  end
end

Instance Attribute Details

#indexObject (readonly)

:nodoc:



10
11
12
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 10

def index
  @index
end

#rruleObject (readonly)

:nodoc:



10
11
12
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 10

def rrule
  @rrule
end

#scopeObject (readonly)

Returns the value of attribute scope.



19
20
21
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 19

def scope
  @scope
end

#sourceObject (readonly)

Returns the value of attribute source.



19
20
21
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 19

def source
  @source
end

#wdayObject (readonly)

:nodoc:



10
11
12
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 10

def wday
  @wday
end

Instance Method Details

#==(another) ⇒ Object



36
37
38
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 36

def ==(another)
  self.class === another && to_a = another.to_a
end

#include?(date_or_time) ⇒ Boolean

Determine if a particular date, time, or date_time is included in the recurrence

Returns:

  • (Boolean)


125
126
127
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 125

def include?(date_or_time)
  date_or_time.wday == wday && ordinal_match(date_or_time)
end

#list_id(time) ⇒ Object

return a list id for a given time to allow the enumerator to cache lists



45
46
47
48
49
50
51
52
53
54
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 45

def list_id(time)
  case @scope
  when :yearly
    time.year
  when :monthly
    (time.year * 100) + time.month
  when :weekly
    time.at_start_of_week_with_wkst(rrule.wkst_day).jd
  end
end

#matches_for(time) ⇒ Object

return a list of times which match the time parameter within the scope of the RecurringDay



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 57

def matches_for(time)
  case @scope
  when :yearly
    yearly_matches_for(time)
  when :monthly
    monthly_matches_for(time)
  when :weekly
    weekly_matches_for(time)
  else
    walkback = caller.grep(/recurrence/i)
    raise "Logic error!#{@scope.inspect}\n  #{walkback.join("\n  ")}"
  end         
end

#monthly_matches_for(time) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 86

def monthly_matches_for(time)
  if @ordinal == ""
    t = time.nth_wday_in_month(1, wday)
    result = []
    month = time.month
    while t.month == month
      result << t
      t = t.advance(:days => 7)
    end
    result
  else
    result = [time.nth_wday_in_month(index, wday)]
    result
  end
end

#ordinal_match(date_or_time) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 112

def ordinal_match(date_or_time)
  if @ordinal == "" || @scope == :weekly
    true
  else
    if @scope == :yearly
      date_or_time.nth_wday_in_year?(index, wday) 
    else
      date_or_time.nth_wday_in_month?(index, wday)
    end
  end
end

#to_aObject



40
41
42
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 40

def to_a
  [@day, @ordinal]
end

#to_sObject



108
109
110
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 108

def to_s
  "#{@ordinal}#{@day}"
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 32

def valid?
  !@day.nil?
end

#weekly_matches_for(time) ⇒ Object



102
103
104
105
106
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 102

def weekly_matches_for(time)
  date = time.start_of_week_with_wkst(rrule.wkst_day)
  date += 1 while date.wday != wday
  [time.change(:year => date.year, :month => date.month, :day => date.day)]
end

#yearly_matches_for(time) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 71

def yearly_matches_for(time)
  if @ordinal == ""
    t = time.nth_wday_in_year(1, wday)
    result = []
    year = time.year
    while t.year == year
      result << t
      t = t.advance(:week => 1)
    end
    result
  else
    [time.nth_wday_in_year(@ordinal.to_i, wday)]
  end
end