File: by_weekno_incrementer.rb

package info (click to toggle)
mhc 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,320 kB
  • ctags: 3,529
  • sloc: ruby: 12,404; lisp: 7,448; makefile: 70; sh: 69
file content (69 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module RiCal
  class PropertyValue
    class RecurrenceRule < PropertyValue
      #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
      #
      class OccurrenceIncrementer # :nodoc:
        class ByWeekNoIncrementer < ListIncrementer #:nodoc:
          attr_reader :wkst
          # include WeeklyBydayMethods

          def initialize(rrule, list, sub_cycle_incrementer)
            @wkst = rrule.wkst_day
            super(rrule, list, sub_cycle_incrementer)
          end

          def self.for_rrule(rrule)
            conditional_incrementer(rrule, :byweekno, WeeklyIncrementer)
          end

          def weeknum_incrementer?
            true
          end
          
          def unneeded?(candidate)
            false
          end

          def first_within_outer_cycle(previous_occurrence, outer_range)
            new_range_start = outer_range.first
            new_range_end = new_range_start.end_of_iso_year(wkst)
            super(previous_occurrence, outer_range.first..new_range_end)
          end

          def start_of_cycle(date_time)
            result = date_time.at_start_of_iso_year(wkst)
            result
          end

          def occurrences_for(date_time)
            iso_year, year_start = *date_time.iso_year_and_week_one_start(wkst)
            week_one_occurrence = date_time.change(
              :year => year_start.year,
              :month => year_start.month,
              :day => year_start.day
            )
            weeks_in_year_plus_one = week_one_occurrence.iso_weeks_in_year(wkst)
            weeks = list.map {|recurring_weeknum|
              wk_num = recurring_weeknum.ordinal
              (wk_num > 0) ? wk_num : weeks_in_year_plus_one + wk_num
              }.uniq.sort
            weeks.map {|wk_num| week_one_occurrence.advance(:days => (wk_num - 1) * 7)}
          end

          def candidate_acceptable?(candidate)
            list.include?(candidate.iso_week_num(wkst))
          end

          def advance_cycle(date_time)
            date_time.at_start_of_next_iso_year(wkst)
          end

          def end_of_occurrence(date_time)
            date_time.end_of_week_with_wkst(wkst)
          end
        end
      end
    end
  end
end