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) ⇒ EnumerationInstance

Returns a new instance of EnumerationInstance.



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

def initialize(component)
  @component = component
  @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

#before_start?(occurrence) ⇒ Boolean

Also exclude occurrences before the :starting date_time

Returns:

  • (Boolean)


98
99
100
# File 'lib/ri_cal/occurrence_enumerator.rb', line 98

def before_start?(occurrence)
  (@start && occurrence.dtstart.to_datetime < @start)
end

#bounded?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/ri_cal/occurrence_enumerator.rb', line 147

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

#each(options = nil) ⇒ Object

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ri_cal/occurrence_enumerator.rb', line 127

def each(options = nil)
  process_options(options) if options
  if @rrules.empty?
    yield @component
  else
    occurrence = next_occurrence
    while (occurrence)
      if options_stop(occurrence)
        occurrence = nil
      else
        unless before_start?(occurrence)
          @yielded += 1
          yield @component.recurrence(occurrence)
        end
        occurrence = next_occurrence
      end
    end
  end
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



84
85
86
87
88
89
# File 'lib/ri_cal/occurrence_enumerator.rb', line 84

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)


93
94
95
# File 'lib/ri_cal/occurrence_enumerator.rb', line 93

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

#next_occurrenceObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ri_cal/occurrence_enumerator.rb', line 102

def next_occurrence
  @yielded ||= 0
  @next_exclusion ||= @exrules.next_occurrence
  occurrence = nil

  until occurrence
    if (occurrence = @rrules.next_occurrence)
      if exclusion_match?(occurrence, exclusion_for(occurrence))
        occurrence = nil # Look for the next one
      end
    else
      break
    end
  end
  occurrence
end

#options_stop(occurrence) ⇒ Object



119
120
121
122
# File 'lib/ri_cal/occurrence_enumerator.rb', line 119

def options_stop(occurrence)
  occurrence != :excluded &&
  (@cutoff && occurrence.dtstart.to_datetime >= @cutoff) || (@count && @yielded >= @count)
end

#process_options(options) ⇒ Object



151
152
153
154
155
# File 'lib/ri_cal/occurrence_enumerator.rb', line 151

def process_options(options)
  @start = options[:starting]
  @cutoff = options[:before]
  @count = options[:count]
end

#to_a(options = {}) ⇒ Object Also known as: entries

Raises:

  • (ArgumentError)


157
158
159
160
161
# File 'lib/ri_cal/occurrence_enumerator.rb', line 157

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