Permalink
Browse files

occurrence enumeration producing hashes

  • Loading branch information...
1 parent 046e179 commit fd5e54878df94ada6be5215f6690ad59b18d3386 @rubyredrick committed Feb 25, 2009
View
@@ -66,11 +66,13 @@ def valid?
!mutual_exclusion_violation
end
- # return the value of a property if it exists
- # otherwise return nil
- def value_of_property(property)
- property ? property.value : nil
+ def recurrence(occurrence)
+ result = self.copy
end
+
+ def initialize_copy(original)
+ end
+
end
end
@@ -5,6 +5,10 @@ module Conversions
def to_rfc2445_string
to_s
end
+
+ def to_ruby_value
+ self
+ end
end
end
end
@@ -2,6 +2,8 @@ module RiCal
# OccurrenceEnumerator provides common methods for CalendarComponents that support recurrence
# i.e. Event, Journal, Todo, and TimezonePeriod
module OccurrenceEnumerator
+
+ include Enumerable
def default_duration
dtend && dtstart.to_ri_cal_date_time_value.duration_until(dtend)
@@ -33,6 +35,7 @@ def self.for(component, rules)
def initialize(component, rules)
self.enumerators = rules.map {|rrule| rrule.enumerator(component)}
+ @bounded = enumerators.all? {|enumerator| enumerator.bounded?}
self.nexts = @enumerators.map {|enumerator| enumerator.next_occurrence}
end
@@ -44,22 +47,74 @@ def next_occurrence
end
result
end
+
+ def bounded?
+ @bounded
+ end
end
# EnumerationInstance holds the values needed during the enumeration of occurrences for a component.
class EnumerationInstance
- def initialize(options, component)
+ include Enumerable
+
+ def initialize(component, options = {})
@component = component
@start = options[:starting]
@cutoff = options[:before]
- @rrules = OccurrenceMerger.for(@component, @component.rrule)
- @exrules = OccurrenceMerger.for(@component, @component.exrule)
+ @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
+
+ # 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
+ def exclusion_for(occurrence)
+ while (@next_exclusion && @next_exclusion[:start] < occurrence[:start])
+ @next_exclusion = @exrules.next_occurrence
+ end
+ @next_exclusion
+ end
+
+ # TODO: Need to research this, I beleive that this should also take the end time into account,
+ # but I need to research
+ def exclusion_match?(occurrence, exclusion)
+ exclusion && occurrence[:start] == occurrence[:start]
+ end
+
+ def exclude?(occurrence)
+ exclusion_match?(occurrence, exclusion_for(occurrence))
end
# yield each occurrence to a block
# some components may be open-ended, e.g. have no COUNT or DTEND
- def each_occurrence
+ def each
+ occurrence = @rrules.next_occurrence
+ yielded = 0
+ @next_exclusion = @exrules.next_occurrence
+ while (occurrence)
+ if (@cutoff && occurrence[:start] >= @cutoff) || (@count && yielded >= @count)
+ occurrence = nil
+ else
+ unless exclude?(occurrence)
+ yielded += 1
+ yield occurrence
+# yield @component.recurrence(occurrence)
+ end
+ occurrence = @rrules.next_occurrence
+ end
+ end
+ end
+
+ def bounded?
+ @rrules.bounded? || @count || @cutoff
end
+
+ def to_a
+ raise ArgumentError.new("This component is unbounded, cannot produce an array of occurrences!") unless bounded?
+ super
+ end
+
+ alias_method :entries, :to_a
end
# return an array of occurrences according to the options parameter
@@ -68,7 +123,11 @@ def each_occurrence
# * starting
# * before
def occurrences(options={})
- EnumerationInstance.new(options, self).occurrences
+ EnumerationInstance.new(self, options).to_a
end
+
+ def each(&block)
+ EnumerationInstance.new(self).each(&block)
+ end
end
end
@@ -29,7 +29,7 @@ def action=(ruby_value)
# return the value of the ACTION property
# which will be an instance of String
def action
- action_property ? action_property.value : property
+ action_property ? action_property.value : nil
end
def action_property_from_string(line) # :nodoc:
@@ -62,7 +62,7 @@ def description=(ruby_value)
# return the value of the DESCRIPTION property
# which will be an instance of String
def description
- description_property ? description_property.value : property
+ description_property ? description_property.value : nil
end
def description_property_from_string(line) # :nodoc:
@@ -95,7 +95,7 @@ def trigger=(ruby_value)
# return the value of the TRIGGER property
# which will be an instance of duration_or_utc_date_time
def trigger
- trigger_property ? trigger_property.value : property
+ trigger_property ? trigger_property.value : nil
end
def trigger_property_from_string(line) # :nodoc:
@@ -128,7 +128,7 @@ def duration=(ruby_value)
# return the value of the DURATION property
# which will be an instance of Duration
def duration
- duration_property ? duration_property.value : property
+ duration_property ? duration_property.value : nil
end
def duration_property_from_string(line) # :nodoc:
@@ -161,7 +161,7 @@ def repeat=(ruby_value)
# return the value of the REPEAT property
# which will be an instance of Integer
def repeat
- repeat_property ? repeat_property.value : property
+ repeat_property ? repeat_property.value : nil
end
def repeat_property_from_string(line) # :nodoc:
@@ -194,7 +194,7 @@ def summary=(ruby_value)
# return the value of the SUMMARY property
# which will be an instance of String
def summary
- summary_property ? summary_property.value : property
+ summary_property ? summary_property.value : nil
end
def summary_property_from_string(line) # :nodoc:
@@ -18,7 +18,7 @@ def calscale_property
# return the value of the CALSCALE property
# which will be an instance of String
def calscale
- calscale_property ? calscale_property.value : property
+ calscale_property ? calscale_property.value : nil
end
def calscale_property_from_string(line) # :nodoc:
@@ -51,7 +51,7 @@ def icalendar_method=(ruby_value)
# return the value of the METHOD property
# which will be an instance of String
def icalendar_method
- method_property ? method_property.value : property
+ method_property ? method_property.value : nil
end
def method_property_from_string(line) # :nodoc:
@@ -84,7 +84,7 @@ def prodid=(ruby_value)
# return the value of the PRODID property
# which will be an instance of String
def prodid
- prodid_property ? prodid_property.value : property
+ prodid_property ? prodid_property.value : nil
end
def prodid_property_from_string(line) # :nodoc:
@@ -106,7 +106,7 @@ def version_property
# return the value of the VERSION property
# which will be an instance of String
def version
- version_property ? version_property.value : property
+ version_property ? version_property.value : nil
end
def version_property_from_string(line) # :nodoc:
@@ -29,7 +29,7 @@ def security_class=(ruby_value)
# return the value of the CLASS property
# which will be an instance of String
def security_class
- class_property ? class_property.value : property
+ class_property ? class_property.value : nil
end
def class_property_from_string(line) # :nodoc:
@@ -62,7 +62,7 @@ def created=(ruby_value)
# return the value of the CREATED property
# which will be an instance of DateTime
def created
- created_property ? created_property.value : property
+ created_property ? created_property.value : nil
end
def created_property_from_string(line) # :nodoc:
@@ -95,7 +95,7 @@ def description=(ruby_value)
# return the value of the DESCRIPTION property
# which will be an instance of String
def description
- description_property ? description_property.value : property
+ description_property ? description_property.value : nil
end
def description_property_from_string(line) # :nodoc:
@@ -128,7 +128,7 @@ def dtstart=(ruby_value)
# return the value of the DTSTART property
# which will be an instance of either DateTime or Date
def dtstart
- dtstart_property ? dtstart_property.value : property
+ dtstart_property ? dtstart_property.value : nil
end
def dtstart_property_from_string(line) # :nodoc:
@@ -161,7 +161,7 @@ def geo=(ruby_value)
# return the value of the GEO property
# which will be an instance of Geo
def geo
- geo_property ? geo_property.value : property
+ geo_property ? geo_property.value : nil
end
def geo_property_from_string(line) # :nodoc:
@@ -194,7 +194,7 @@ def last_modified=(ruby_value)
# return the value of the LAST-MODIFIED property
# which will be an instance of DateTime
def last_modified
- last_modified_property ? last_modified_property.value : property
+ last_modified_property ? last_modified_property.value : nil
end
def last_modified_property_from_string(line) # :nodoc:
@@ -227,7 +227,7 @@ def location=(ruby_value)
# return the value of the LOCATION property
# which will be an instance of String
def location
- location_property ? location_property.value : property
+ location_property ? location_property.value : nil
end
def location_property_from_string(line) # :nodoc:
@@ -260,7 +260,7 @@ def organizer=(ruby_value)
# return the value of the ORGANIZER property
# which will be an instance of CalAddress
def organizer
- organizer_property ? organizer_property.value : property
+ organizer_property ? organizer_property.value : nil
end
def organizer_property_from_string(line) # :nodoc:
@@ -293,7 +293,7 @@ def priority=(ruby_value)
# return the value of the PRIORITY property
# which will be an instance of Integer
def priority
- priority_property ? priority_property.value : property
+ priority_property ? priority_property.value : nil
end
def priority_property_from_string(line) # :nodoc:
@@ -326,7 +326,7 @@ def dtstamp=(ruby_value)
# return the value of the DTSTAMP property
# which will be an instance of DateTime
def dtstamp
- dtstamp_property ? dtstamp_property.value : property
+ dtstamp_property ? dtstamp_property.value : nil
end
def dtstamp_property_from_string(line) # :nodoc:
@@ -359,7 +359,7 @@ def sequence=(ruby_value)
# return the value of the SEQUENCE property
# which will be an instance of Integer
def sequence
- sequence_property ? sequence_property.value : property
+ sequence_property ? sequence_property.value : nil
end
def sequence_property_from_string(line) # :nodoc:
@@ -392,7 +392,7 @@ def status=(ruby_value)
# return the value of the STATUS property
# which will be an instance of String
def status
- status_property ? status_property.value : property
+ status_property ? status_property.value : nil
end
def status_property_from_string(line) # :nodoc:
@@ -425,7 +425,7 @@ def summary=(ruby_value)
# return the value of the SUMMARY property
# which will be an instance of String
def summary
- summary_property ? summary_property.value : property
+ summary_property ? summary_property.value : nil
end
def summary_property_from_string(line) # :nodoc:
@@ -458,7 +458,7 @@ def transp=(ruby_value)
# return the value of the TRANSP property
# which will be an instance of String
def transp
- transp_property ? transp_property.value : property
+ transp_property ? transp_property.value : nil
end
def transp_property_from_string(line) # :nodoc:
@@ -491,7 +491,7 @@ def uid=(ruby_value)
# return the value of the UID property
# which will be an instance of String
def uid
- uid_property ? uid_property.value : property
+ uid_property ? uid_property.value : nil
end
def uid_property_from_string(line) # :nodoc:
@@ -524,7 +524,7 @@ def url=(ruby_value)
# return the value of the URL property
# which will be an instance of Uri
def url
- url_property ? url_property.value : property
+ url_property ? url_property.value : nil
end
def url_property_from_string(line) # :nodoc:
@@ -557,7 +557,7 @@ def recurrence_id=(ruby_value)
# return the value of the RECURRENCE-ID property
# which will be an instance of either DateTime or Date
def recurrence_id
- recurrence_id_property ? recurrence_id_property.value : property
+ recurrence_id_property ? recurrence_id_property.value : nil
end
def recurrence_id_property_from_string(line) # :nodoc:
@@ -591,7 +591,7 @@ def dtend=(ruby_value)
# return the value of the DTEND property
# which will be an instance of either DateTime or Date
def dtend
- dtend_property ? dtend_property.value : property
+ dtend_property ? dtend_property.value : nil
end
def dtend_property_from_string(line) # :nodoc:
@@ -625,7 +625,7 @@ def duration=(ruby_value)
# return the value of the DURATION property
# which will be an instance of Duration
def duration
- duration_property ? duration_property.value : property
+ duration_property ? duration_property.value : nil
end
def duration_property_from_string(line) # :nodoc:
Oops, something went wrong.

0 comments on commit fd5e548

Please sign in to comment.