Module: RiCal::OccurrenceEnumerator

Includes:
Enumerable
Included in:
Component::Event, Component::Journal, Component::Timezone::TimezonePeriod, Component::Todo
Defined in:
lib/ri_cal/occurrence_enumerator.rb

Overview

  • ©2009 Rick DeNatale

  • All rights reserved. Refer to the file README.txt for the license

OccurrenceEnumerator provides common methods for CalendarComponents that support recurrence i.e. Event, Journal, Todo, and TimezonePeriod

Defined Under Namespace

Classes: EmptyRulesEnumerator, EnumerationInstance, OccurrenceMerger

Instance Method Summary collapse

Instance Method Details

#after_range?(overlap_range) ⇒ Boolean

Returns:

  • (Boolean)

206
207
208
209
# File 'lib/ri_cal/occurrence_enumerator.rb', line 206

def after_range?(overlap_range)
  start = start_time
  !start || start > overlap_range.last
end

#before_range?(overlap_range) ⇒ Boolean

Returns:

  • (Boolean)

201
202
203
204
# File 'lib/ri_cal/occurrence_enumerator.rb', line 201

def before_range?(overlap_range)
  finish = finish_time
  !finish_time || finish_time < overlap_range.first
end

#bounded?Boolean

A predicate which determines whether the component has a bounded set of occurrences

Returns:

  • (Boolean)

217
218
219
# File 'lib/ri_cal/occurrence_enumerator.rb', line 217

def bounded?
  enumeration_instance.bounded?
end

#default_durationObject

:nodoc:


11
12
13
# File 'lib/ri_cal/occurrence_enumerator.rb', line 11

def default_duration # :nodoc:
  dtend && dtstart.to_ri_cal_date_time_value.duration_until(dtend.to_ri_cal_date_time_value)
end

#default_start_timeObject

:nodoc:


15
16
17
# File 'lib/ri_cal/occurrence_enumerator.rb', line 15

def default_start_time # :nodoc:
  dtstart && dtstart.to_ri_cal_date_time_value
end

#each(&block) ⇒ Object

execute the block for each occurrence


212
213
214
# File 'lib/ri_cal/occurrence_enumerator.rb', line 212

def each(&block) # :yields: Component
  enumeration_instance.each(&block)
end

#enumeration_instanceObject

TODO: Thread safe?


197
198
199
# File 'lib/ri_cal/occurrence_enumerator.rb', line 197

def enumeration_instance #:nodoc:
  EnumerationInstance.new(self)
end

#occurrences(options = {}) ⇒ Object

return an array of occurrences according to the options parameter. If a component is not bounded, and the number of occurrences to be returned is not constrained by either the :before, or :count options an ArgumentError will be raised.

The components returned will be the same type as the receiver, but will have any recurrence properties (rrule, rdate, exrule, exdate) removed since they are single occurrences, and will have the recurrence-id property set to the occurrences dtstart value. (see RFC 2445 sec 4.8.4.4 pp 107-109)

parameter options:

  • :starting

    a Date, Time, or DateTime, no occurrences starting before this argument will be returned

  • :before

    a Date, Time, or DateTime, no occurrences starting on or after this argument will be returned.

  • :count

    an integer which limits the number of occurrences returned.

  • :overlapping

    a two element array of Dates, Times, or DateTimes, assumed to be in chronological order. Only occurrences which are either totally or partially within the range will be returned.


192
193
194
# File 'lib/ri_cal/occurrence_enumerator.rb', line 192

def occurrences(options={})
  enumeration_instance.to_a(options)
end

#recurrence(occurrence) ⇒ Object

:nodoc:


256
257
258
# File 'lib/ri_cal/occurrence_enumerator.rb', line 256

def recurrence(occurrence) # :nodoc:
  result = self.dup.set_occurrence_properties!(occurrence)
end

#recurs?Boolean

Returns:

  • (Boolean)

260
261
262
# File 'lib/ri_cal/occurrence_enumerator.rb', line 260

def recurs?
  @rrule_property && @rrule_property.length > 0 || @rdate_property && @rdate_property.length > 0
end

#set_occurrence_properties!(occurrence) ⇒ Object

:nodoc:


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ri_cal/occurrence_enumerator.rb', line 240

def set_occurrence_properties!(occurrence) # :nodoc:
  occurrence_end = occurrence.dtend
  occurrence_start = occurrence.dtstart
  @rrule_property = nil
  @exrule_property = nil
  @rdate_property = nil
  @exdate_property = nil
  @recurrence_id_property = occurrence_start
  if @dtend_property && !occurrence_end
     occurrence_end = occurrence_start + (@dtend_property - @dtstart_property)
  end
  @dtstart_property = @dtstart_property.for_occurrence(occurrence_start)
  @dtend_property = (@dtend_property || @dtstart_property).for_occurrence(occurrence_end) if occurrence_end
  self
end

#zulu_occurrence_rangeObject

Return a array whose first element is a UTC DateTime representing the start of the first occurrence, and whose second element is a UTC DateTime representing the end of the last occurrence. If the receiver is not bounded then the second element will be nil.

The purpose of this method is to provide values which may be used as database attributes so that a query can find all occurence enumerating components which may have occurrences within a range of times.


229
230
231
232
233
234
235
236
237
238
# File 'lib/ri_cal/occurrence_enumerator.rb', line 229

def zulu_occurrence_range
  if bounded?
    all = occurrences
    first, last = all.first, all.last
  else
    first = occurrences(:count => 1).first
    last = nil
  end
  [first.zulu_occurrence_range_start_time, last ? last.zulu_occurrence_range_finish_time : nil]
end