Class: RiCal::PropertyValue::RecurrenceRule::OccurrenceIncrementer::ListIncrementer

Inherits:
RiCal::PropertyValue::RecurrenceRule::OccurrenceIncrementer show all
Defined in:
lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb

Overview

A ListIncrementer represents a byxxx part of a recurrence rule It contains a list of simple values or recurring values It keeps a collection of occurrences within a given range called a cycle When the collection of occurrences is exhausted it is refreshed if there is no outer incrementer, or if a new cycle would start in the current cycle of the outer incrementers.

Instance Attribute Summary collapse

Attributes inherited from RiCal::PropertyValue::RecurrenceRule::OccurrenceIncrementer

#contains_daily_incrementer, #contains_weeknum_incrementer, #current_occurrence, #leaf_iterator, #outer_incrementers, #outer_range, #sub_cycle_incrementer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RiCal::PropertyValue::RecurrenceRule::OccurrenceIncrementer

#add_outer_incrementer, #contains_daily_incrementer?, #contains_weeknum_incrementer?, #daily_incrementer?, #first_sub_occurrence, from_rrule, #in_outer_cycle?, #next_time, #outermost?, #short_name, #to_s, #update_cycle_range, #weeknum_incrementer?

Methods included from TimeManipulation

#advance_day, #advance_month, #advance_week, #advance_year, #first_day_of_month, #first_day_of_week, #first_day_of_year, #first_hour_of_day

Constructor Details

#initialize(rrule, list, sub_cycle_incrementer) ⇒ ListIncrementer

Returns a new instance of ListIncrementer.



16
17
18
19
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 16

def initialize(rrule, list, sub_cycle_incrementer)
  super(rrule, sub_cycle_incrementer)
  self.list = list
end

Instance Attribute Details

#cycle_startObject

:nodoc:



14
15
16
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 14

def cycle_start
  @cycle_start
end

#listObject

:nodoc:



14
15
16
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 14

def list
  @list
end

#occurrencesObject

:nodoc:



14
15
16
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 14

def occurrences
  @occurrences
end

#outer_occurrenceObject

:nodoc:



14
15
16
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 14

def outer_occurrence
  @outer_occurrence
end

Class Method Details

.conditional_incrementer(rrule, by_part, sub_cycle_class) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 21

def self.conditional_incrementer(rrule, by_part, sub_cycle_class)
  sub_cycle_incrementer = sub_cycle_class.for_rrule(rrule)
  list = rrule.by_rule_list(by_part)
  if list
    new(rrule, list, sub_cycle_incrementer)
  else
    sub_cycle_incrementer
  end
end

Instance Method Details

#candidate_acceptable?(candidate) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 50

def candidate_acceptable?(candidate)
  list.any? {|value| candidate.send(varying_time_attribute) == value}
end

#cycle_adjust(date_time) ⇒ Object



84
85
86
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 84

def cycle_adjust(date_time)
  sub_cycle_incrementer.cycle_adjust(start_of_cycle(date_time))
end

#first_within_outer_cycle(previous_occurrence, outer_range) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 54

def first_within_outer_cycle(previous_occurrence, outer_range)
  self.outer_range = outer_range
  self.occurrences = occurrences_within(outer_range)
  occurrences.each { |occurrence|
    sub = sub_cycle_incrementer.first_within_outer_cycle(previous_occurrence, update_cycle_range(occurrence))
    return sub if sub && sub > previous_occurrence
  }
  nil
end

#get_next_occurrencesObject



79
80
81
82
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 79

def get_next_occurrences
  adv_cycle = advance_cycle(start_of_cycle(occurrences.first))
  self.occurrences = occurrences_for(adv_cycle)
end

#next_candidate(date_time) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 64

def next_candidate(date_time)
  candidate = next_in_list(date_time)
  if outermost?
    while candidate.nil?
      get_next_occurrences
      candidate = next_in_list(date_time)
    end
  end
  candidate
end

#next_cycle(previous_occurrence) ⇒ Object

Advance to the next occurrence, if the result is within the current cycles of all outer incrementers



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 32

def next_cycle(previous_occurrence)
  unless occurrences
    self.occurrences = occurrences_for(previous_occurrence)
  end
  candidate = next_candidate(previous_occurrence)
  if candidate
    sub_cycle_incrementer.first_within_outer_cycle(previous_occurrence, update_cycle_range(candidate))
  else
    nil
  end
end

#next_in_list(date_time) ⇒ Object



75
76
77
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 75

def next_in_list(date_time)
  occurrences.find {|occurrence| occurrence > date_time}
end

#occurrences_for(date_time) ⇒ Object



88
89
90
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 88

def occurrences_for(date_time)
  list.map {|value| date_time.change(varying_time_attribute => value)}
end

#occurrences_within(date_time_range) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 92

def occurrences_within(date_time_range)
  result = []
  date_time = date_time_range.first
  while date_time <= date_time_range.last
     result << occurrences_for(date_time)
     date_time = advance_cycle(date_time)
   end
   result.flatten
end

#unneeded?(candidate) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 44

def unneeded?(candidate)
  sub_cycle_incrementer.unneeded?(candidate) &&
  list.length == 1 && 
  candidate_acceptable?(candidate)
end