Class: RiCal::OccurrenceEnumerator::EnumerationInstance

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ri_cal/occurrence_enumerator.rb

Overview

EnumerationInstance holds the values needed during the enumeration of occurrences for a component.

Instance Method Summary collapse

Constructor Details

#initialize(component, options = {}) ⇒ EnumerationInstance

Returns a new instance of EnumerationInstance.



76
77
78
79
80
81
82
83
# File 'lib/ri_cal/occurrence_enumerator.rb', line 76

def initialize(component, options = {})
  @component = component
  @start = options[:starting]
  @cutoff = options[:before]
  @count = options[:count]
  @rrules = OccurrenceMerger.for(@component, [@component.rrule_property, @component.rdate_property].flatten.compact)
  @exrules = OccurrenceMerger.for(@component, [@component.exrule_property, @component.exdate_property].flatten.compact)
end

Instance Method Details

#bounded?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/ri_cal/occurrence_enumerator.rb', line 129

def bounded?
  @rrules.bounded? || @count || @cutoff
end

#eachObject

yield each occurrence to a block some components may be open-ended, e.g. have no COUNT or DTEND



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ri_cal/occurrence_enumerator.rb', line 108

def each
  if @rrules.empty?
    yield @component
  else
    occurrence = @rrules.next_occurrence
    yielded = 0
    @next_exclusion = @exrules.next_occurrence
    while (occurrence)
      if (@cutoff && occurrence.dtstart.to_datetime >= @cutoff) || (@count && yielded >= @count)
        occurrence = nil
      else
        unless exclude?(occurrence)
          yielded += 1
          yield @component.recurrence(occurrence)
        end
        occurrence = @rrules.next_occurrence
      end
    end
  end
end

#exclude?(occurrence) ⇒ Boolean

Also exclude occurrences before the :starting date_time

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/ri_cal/occurrence_enumerator.rb', line 101

def exclude?(occurrence)
  exclusion_match?(occurrence, exclusion_for(occurrence)) ||
    (@start && occurrence.dtstart.to_datetime < @start)
end

#exclusion_for(occurrence) ⇒ Object

return the next exclusion which starts at the same time or after the start time of the occurrence return nil if this exhausts the exclusion rules



87
88
89
90
91
92
# File 'lib/ri_cal/occurrence_enumerator.rb', line 87

def exclusion_for(occurrence)
  while (@next_exclusion && @next_exclusion.dtstart < occurrence.dtstart)
    @next_exclusion = @exrules.next_occurrence
  end
  @next_exclusion
end

#exclusion_match?(occurrence, exclusion) ⇒ Boolean

TODO: Need to research this, I beleive that this should also take the end time into account,

but I need to research

Returns:

  • (Boolean)


96
97
98
# File 'lib/ri_cal/occurrence_enumerator.rb', line 96

def exclusion_match?(occurrence, exclusion)
  exclusion && (occurrence.dtstart == exclusion.dtstart)
end

#to_aObject Also known as: entries

Raises:

  • (ArgumentError)


133
134
135
136
# File 'lib/ri_cal/occurrence_enumerator.rb', line 133

def to_a
  raise ArgumentError.new("This component is unbounded, cannot produce an array of occurrences!") unless bounded?
  super
end