Permalink
Browse files

Remove nils when adding recurrence_dates

  • Loading branch information...
1 parent f9e7725 commit 07722f7485ce09c92a402eb1772ace4e569ff57d John Crepezzi committed Apr 26, 2010
Showing with 22 additions and 7 deletions.
  1. +12 −6 lib/ice_cube/schedule.rb
  2. +10 −1 spec/examples/to_ical_spec.rb
View
@@ -12,6 +12,7 @@ def initialize(start_date)
@start_date = start_date
end
+ # Convert the schedule to a hash, reverse of Schedule.from_hash
def to_hash
hash = Hash.new
hash[:start_date] = @start_date
@@ -21,11 +22,13 @@ def to_hash
hash[:exdates] = @exdates
hash
end
-
+
+ # Convert the schedule to yaml, reverse of Schedule.from_yaml
def to_yaml
to_hash.to_yaml
end
-
+
+ # Create a schedule from a hash created by instance.to_hash
def self.from_hash(hash)
schedule = Schedule.new(hash[:start_date])
hash[:rrules].each { |rr| schedule.add_recurrence_rule Rule.from_hash(rr) }
@@ -34,7 +37,8 @@ def self.from_hash(hash)
hash[:exdates].each { |ed| schedule.add_exception_date ed }
schedule
end
-
+
+ # Create a schedule from a yaml string created by instance.to_yaml
def self.from_yaml(str)
from_hash(YAML::load(str))
end
@@ -63,7 +67,9 @@ def all_occurrences
def occurrences(end_date)
find_occurrences { |head| head.upto(end_date) }
end
-
+
+ # Retrieve the first (n) occurrences of the schedule. May return less than
+ # n results, if the rules end before n results are reached.
def first(n)
dates = find_occurrences { |head| head.first(n) }
dates.slice(0, n)
@@ -91,12 +97,12 @@ def exrules
# Add an individual date to this schedule
def add_recurrence_date(date)
- @rdates << date
+ @rdates << date unless date.nil?
end
# Add an individual date exception to this schedule
def add_exception_date(date)
- @exdates << date
+ @exdates << date unless date.nil?
end
attr_reader :rdates, :exdates
@@ -86,5 +86,14 @@
rule = Rule.daily.day_of_week(:monday => [1, -1], :tuesday => [2]).day(:wednesday)
['FREQ=DAILY;BYDAY=WE;BYDAY=1MO,-1MO,2TU', 'FREQ=DAILY;BYDAY=1MO,-1MO,2TU;BYDAY=WE'].include?(rule.to_ical).should be true
end
-
+
+ it 'should be able to ignore nil dates that are inserted as part of a collection to add_recurrence_date' do
+ start_time = Time.now
+ schedule = Schedule.new(start_time)
+ schedule.add_recurrence_date start_time
+ schedule.add_recurrence_date start_time + ONE_DAY
+ schedule.add_recurrence_date nil
+ schedule.all_occurrences.should == [start_time, start_time + ONE_DAY]
+ end
+
end

0 comments on commit 07722f7

Please sign in to comment.