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.



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

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

Instance Attribute Details

#cycle_startObject

:nodoc:



12
13
14
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 12

def cycle_start
  @cycle_start
end

#listObject

:nodoc:



12
13
14
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 12

def list
  @list
end

#occurrencesObject

:nodoc:



12
13
14
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 12

def occurrences
  @occurrences
end

#outer_occurrenceObject

:nodoc:



12
13
14
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 12

def outer_occurrence
  @outer_occurrence
end

Class Method Details

.conditional_incrementer(rrule, by_part, sub_cycle_class) ⇒ Object



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

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)


48
49
50
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 48

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

#cycle_adjust(date_time) ⇒ Object



82
83
84
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 82

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



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

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



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

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



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

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



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

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



73
74
75
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 73

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

#occurrences_for(date_time) ⇒ Object



86
87
88
# File 'lib/ri_cal/property_value/recurrence_rule/occurrence_incrementer/list_incrementer.rb', line 86

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

#occurrences_within(date_time_range) ⇒ Object



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

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)


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

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