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

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

Overview

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.



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

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:



8
9
10
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 8

def index
  @index
end

#rruleObject (readonly)

:nodoc:



8
9
10
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 8

def rrule
  @rrule
end

#scopeObject (readonly)

Returns the value of attribute scope.



17
18
19
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 17

def scope
  @scope
end

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 17

def source
  @source
end

#wdayObject (readonly)

:nodoc:



8
9
10
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 8

def wday
  @wday
end

Instance Method Details

#==(another) ⇒ Object



34
35
36
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 34

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)


123
124
125
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 123

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



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

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



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

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



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

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



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

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



38
39
40
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 38

def to_a
  [@day, @ordinal]
end

#to_sObject



106
107
108
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 106

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

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ri_cal/property_value/recurrence_rule/recurring_day.rb', line 30

def valid?
  !@day.nil?
end

#weekly_matches_for(time) ⇒ Object



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

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



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

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