File: negative_setpos_enumerator.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 (53 lines) | stat: -rw-r--r-- 1,478 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
module RiCal
  class PropertyValue
    class RecurrenceRule < PropertyValue
      #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
      #
      class NegativeSetposEnumerator < Enumerator # :nodoc:

        def initialize(recurrence_rule, component, setpos_list)
          super
          @current_set = []
          @valids = []
        end

        def next_occurrence
          while true
            result = advance
            if result >= start_time
              @count += 1
              return recurrence_rule.exhausted?(@count, result) ? nil : result_occurrence_period(result)
            end
          end
        end
        
        

        def advance
          if @valids.empty?
            fill_set
            @valids = @setpos_list.map {|sp| sp < 0 ? @current_set.length + sp : sp - 1}
            current_time_index = @current_set.index(@start_time)
            if current_time_index
              @valids << current_time_index
            end
            @valids = @valids.uniq.sort
          end
          @current_set[@valids.shift]
        end

        def fill_set
          @current_set = [next_time]
          while true
            self.next_time = @incrementer.next_time(next_time)
            if recurrence_rule.in_same_set?(@current_set.last, next_time)
              @current_set << next_time
            else
              return
            end
          end
        end
      end
    end
  end
end