Class: RRule::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rrule/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, dtstart, tz) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
# File 'lib/rrule/context.rb', line 7

def initialize(options, dtstart, tz)
  @options = options
  @dtstart = dtstart
  @tz = tz
end

Instance Attribute Details

#day_of_year_maskObject (readonly)

Returns the value of attribute day_of_year_mask.



5
6
7
# File 'lib/rrule/context.rb', line 5

def day_of_year_mask
  @day_of_year_mask
end

#dtstartObject (readonly)

Returns the value of attribute dtstart.



5
6
7
# File 'lib/rrule/context.rb', line 5

def dtstart
  @dtstart
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/rrule/context.rb', line 5

def options
  @options
end

#tzObject (readonly)

Returns the value of attribute tz.



5
6
7
# File 'lib/rrule/context.rb', line 5

def tz
  @tz
end

#yearObject (readonly)

Returns the value of attribute year.



5
6
7
# File 'lib/rrule/context.rb', line 5

def year
  @year
end

Instance Method Details

#elapsed_days_in_year_by_monthObject



92
93
94
# File 'lib/rrule/context.rb', line 92

def elapsed_days_in_year_by_month
  @elapsed_days_in_year_by_month ||= [0] + (1..12).map { |month| Date.new(year, month).end_of_month.yday }
end

#first_day_of_yearObject



60
61
62
# File 'lib/rrule/context.rb', line 60

def first_day_of_year
  @first_day_of_year ||= Date.new(year).beginning_of_year
end

#first_weekday_of_yearObject



64
65
66
# File 'lib/rrule/context.rb', line 64

def first_weekday_of_year
  @first_weekday_of_year ||= first_day_of_year.wday
end

#month_by_day_of_yearObject



68
69
70
# File 'lib/rrule/context.rb', line 68

def month_by_day_of_year
  @month_by_day_of_year ||= days_in_year.map(&:month)
end

#month_day_by_day_of_yearObject



72
73
74
# File 'lib/rrule/context.rb', line 72

def month_day_by_day_of_year
  @month_day_by_day_of_year ||= days_in_year.map(&:day)
end

#negative_month_day_by_day_of_yearObject



76
77
78
# File 'lib/rrule/context.rb', line 76

def negative_month_day_by_day_of_year
  @negative_month_day_by_day_of_year ||= days_in_year.map { |day| day.day - day.end_of_month.day - 1 }
end

#negative_week_number_by_day_of_yearObject



88
89
90
# File 'lib/rrule/context.rb', line 88

def negative_week_number_by_day_of_year
  @negative_week_number_by_day_of_year ||= days_in_year.map { |day| day.cweek - Date.new(day.cwyear, 12, 28).cweek - 1 }
end

#next_year_length_in_daysObject



56
57
58
# File 'lib/rrule/context.rb', line 56

def next_year_length_in_days
  @next_year_length_in_days ||= Date.leap?(year + 1) ? 366 : 365
end

#rebuild(year, month) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rrule/context.rb', line 13

def rebuild(year, month)
  @year = year

  reset_year if year != last_year

  if options[:bynweekday] && !options[:bynweekday].empty? && (month != last_month || year != last_year)
    possible_date_ranges = []

    case options[:freq]
    when 'YEARLY'
      if options[:bymonth]
        options[:bymonth].each do |mon|
          possible_date_ranges.push(elapsed_days_in_year_by_month[(mon - 1)..mon])
        end
      else
        possible_date_ranges = [[0, year_length_in_days]]
      end
    when 'MONTHLY'
      possible_date_ranges = [elapsed_days_in_year_by_month[(month - 1)..month]]
    end

    unless possible_date_ranges.empty?
      @day_of_year_mask = Array.new(year_length_in_days, false)

      possible_date_ranges.each do |possible_date_range|
        year_day_start = possible_date_range[0]
        year_day_end = possible_date_range[1] - 1
        options[:bynweekday].each do |weekday|
          day_of_year = day_of_year_within_range(weekday, year_day_start, year_day_end)
          day_of_year_mask[day_of_year] = true if day_of_year
        end
      end
    end
  end

  @last_year = year
  @last_month = month
end

#week_number_by_day_of_yearObject



84
85
86
# File 'lib/rrule/context.rb', line 84

def week_number_by_day_of_year
  @week_number_by_day_of_year ||= days_in_year.map(&:cweek)
end

#weekday_by_day_of_yearObject



80
81
82
# File 'lib/rrule/context.rb', line 80

def weekday_by_day_of_year
  @weekday_by_day_of_year ||= weekdays_in_year.drop(first_weekday_of_year)
end

#year_length_in_daysObject



52
53
54
# File 'lib/rrule/context.rb', line 52

def year_length_in_days
  @year_length_in_days ||= leap_year? ? 366 : 365
end