Class: Tilia::VObject::Recur::RRuleIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/tilia/v_object/recur/r_rule_iterator.rb

Overview

RRuleParser.

This class receives an RRULE string, and allows you to iterate to get a list of dates in that recurrence.

For instance, passing: FREQ=DAILY;LIMIT=5 will cause the iterator to contain 5 items, one for each day.

Instance Method Summary collapse

Constructor Details

#initialize(rrule, start) ⇒ RRuleIterator

Creates the Iterator.

Parameters:

  • rrule (String|array)
  • start (Time)


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
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 16

def initialize(rrule, start)
  @week_start = 'MO'
  @counter = 0
  @interval = 1
  @day_map = {
    'SU' => 0,
    'MO' => 1,
    'TU' => 2,
    'WE' => 3,
    'TH' => 4,
    'FR' => 5,
    'SA' => 6
  }
  @day_names = {
    0 => 'Sunday',
    1 => 'Monday',
    2 => 'Tuesday',
    3 => 'Wednesday',
    4 => 'Thursday',
    5 => 'Friday',
    6 => 'Saturday'
  }

  @start_date = start
  parse_r_rule(rrule)
  @current_date = @start_date.clone
end

Instance Method Details

#currentObject



44
45
46
47
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 44

def current
  return nil unless valid
  @current_date.clone
end

#fast_forward(dt) ⇒ void

This method returns an undefined value.

This method allows you to quickly go to the next occurrence after the specified date.

Parameters:

  • dt (Time)


109
110
111
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 109

def fast_forward(dt)
  self.next while valid && @current_date < dt
end

#infinite?Boolean

Returns true if this recurring event never ends.

Returns:

  • (Boolean)


99
100
101
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 99

def infinite?
  !@count && !@until
end

#keyFixnum

Returns the current item number.

Returns:

  • (Fixnum)


52
53
54
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 52

def key
  @counter
end

#nextvoid

This method returns an undefined value.

Goes on to the next iteration.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 77

def next
  # Otherwise, we find the next event in the normal RRULE
  # sequence.
  case @frequency
  when 'hourly'
    next_hourly
  when 'daily'
    next_daily
  when 'weekly'
    next_weekly
  when 'monthly'
    next_monthly
  when 'yearly'
    next_yearly
  end

  @counter += 1
end

#rewindvoid

This method returns an undefined value.

Resets the iterator.



69
70
71
72
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 69

def rewind
  @current_date = @start_date.clone
  @counter = 0
end

#validBoolean

Returns whether the current item is a valid item for the recurrence iterator. This will return false if we’ve gone beyond the UNTIL or COUNT statements.

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/tilia/v_object/recur/r_rule_iterator.rb', line 61

def valid
  return @counter < @count if @count
  @until.nil? || @current_date <= @until
end