Skip to content

Commit

Permalink
Make duration dependent on end_time
Browse files Browse the repository at this point in the history
* schedule.end_time attribute controls duration.
* preference is given to :end_time over :duration for initializing.
* to_ical outputs only DTEND instead of DURATION.
* to_hash outputs only :end_time.

Issue ice-cube-ruby#120
  • Loading branch information
avit committed Dec 21, 2012
1 parent 2948e5c commit f8dd287
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
19 changes: 11 additions & 8 deletions lib/ice_cube/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ class Schedule
attr_reader :start_time
deprecated_alias :start_date, :start_time

# Get the duration
attr_accessor :duration

# Get the end time
attr_reader :end_time
deprecated_alias :end_date, :end_time

# Create a new schedule
def initialize(start_time = nil, options = {})
self.start_time = start_time || TimeUtil.now
self.end_time = options[:end_time]
@duration = options[:duration]
self.end_time = self.start_time + options[:duration] if options[:duration]
self.end_time = options[:end_time] if options[:end_time]
@all_recurrence_rules = []
@all_exception_rules = []
end
Expand All @@ -38,6 +35,14 @@ def end_time=(end_time)
end
deprecated_alias :end_date=, :end_time=

def duration
end_time - start_time if end_time
end

def duration=(seconds)
@end_time = start_time + seconds
end

# Add a recurrence time to the schedule
def add_recurrence_time(time)
return nil if time.nil?
Expand Down Expand Up @@ -273,7 +278,6 @@ def to_s
def to_ical(force_utc = false)
pieces = []
pieces << "DTSTART#{IcalBuilder.ical_format(start_time, force_utc)}"
pieces << "DURATION:#{IcalBuilder.ical_duration(duration)}" if duration
pieces.concat recurrence_rules.map { |r| "RRULE:#{r.to_ical}" }
pieces.concat exception_rules.map { |r| "EXRULE:#{r.to_ical}" }
pieces.concat recurrence_times.map { |t| "RDATE#{IcalBuilder.ical_format(t, force_utc)}" }
Expand All @@ -297,7 +301,6 @@ def to_hash
data = {}
data[:start_date] = TimeUtil.serialize_time(start_time)
data[:end_time] = TimeUtil.serialize_time(end_time) if end_time
data[:duration] = duration if duration
data[:rrules] = recurrence_rules.map(&:to_hash)
data[:exrules] = exception_rules.map(&:to_hash)
data[:rtimes] = recurrence_times.map do |rt|
Expand All @@ -315,7 +318,7 @@ def self.from_hash(original_hash, options = {})
# And then deserialize
data = IceCube::FlexibleHash.new(original_hash)
schedule = IceCube::Schedule.new TimeUtil.deserialize_time(data[:start_date])
schedule.duration = data[:duration] if data[:duration]
schedule.end_time = schedule.start_time + data[:duration] if data[:duration]
schedule.end_time = TimeUtil.deserialize_time(data[:end_time]) if data[:end_time]
data[:rrules] && data[:rrules].each { |h| schedule.rrule(IceCube::Rule.from_hash(h)) }
data[:exrules] && data[:exrules].each { |h| schedule.exrule(IceCube::Rule.from_hash(h)) }
Expand Down
19 changes: 19 additions & 0 deletions spec/examples/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@

include IceCube

describe :duration do

it 'should be based on end_time' do
start = Time.now
schedule = IceCube::Schedule.new(start)
schedule.duration.should be_nil
schedule.end_time = start + 3600
schedule.duration.should == 3600
end

it 'should give precedence to :end_time option' do
start = Time.now
conflicting_options = {:end_time => start + 600, :duration => 1200}
schedule = IceCube::Schedule.new(start, conflicting_options)
schedule.duration.should == 600
end

end

describe :conflicts_with? do

it 'should raise an error if both are not terminating' do
Expand Down
4 changes: 2 additions & 2 deletions spec/examples/to_ical_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@
it 'should be able to serialize a schedule with a duration' do
schedule = IceCube::Schedule.new(Time.utc(2010, 5, 10, 10), :duration => 3600)
expectation = "DTSTART:20100510T100000Z\n"
expectation << 'DURATION:PT1H'
expectation << 'DTEND:20100510T110000Z'
schedule.to_ical.should == expectation
end

it 'should be able to serialize a schedule with a duration - more odd duration' do
schedule = IceCube::Schedule.new(Time.utc(2010, 5, 10, 10), :duration => 3665)
expectation = "DTSTART:20100510T100000Z\n"
expectation << 'DURATION:PT1H1M5S'
expectation << 'DTEND:20100510T110105Z'
schedule.to_ical.should == expectation
end

Expand Down

0 comments on commit f8dd287

Please sign in to comment.