Permalink
Browse files

Allow passing of Time, Date, or DateTime all over [#103]

  • Loading branch information...
1 parent f0604db commit 7de4837c90a3a76375720f66e8fdaed39bd1ae96 @seejohnrun committed Oct 13, 2012
View
@@ -7,27 +7,37 @@ class Schedule
extend ::Deprecated
# Get the start time
- attr_accessor :start_time
+ attr_reader :start_time
deprecated_alias :start_date, :start_time
- deprecated_alias :start_date=, :start_time=
# Get the duration
attr_accessor :duration
# Get the end time
- attr_accessor :end_time
+ attr_reader :end_time
deprecated_alias :end_date, :end_time
- deprecated_alias :end_date=, :end_time=
# Create a new schedule
def initialize(start_time = nil, options = {})
- @start_time = start_time || TimeUtil.now
- @end_time = options[:end_time]
+ self.start_time = start_time || TimeUtil.now
+ self.end_time = options[:end_time]
@duration = options[:duration]
@all_recurrence_rules = []
@all_exception_rules = []
end
+ # Set start_time
+ def start_time=(start_time)
+ @start_time = TimeUtil.ensure_time start_time
+ end
+ deprecated_alias :start_date=, :start_time=
+
+ # Set end_time
+ def end_time=(end_time)
+ @end_time = TimeUtil.ensure_time end_time
+ end
+ deprecated_alias :end_date=, :end_time=
+
# Add a recurrence time to the schedule
def add_recurrence_time(time)
return nil if time.nil?
@@ -179,6 +189,7 @@ def occurring_between?(begin_time, closing_time)
# Return a boolean indicating if an occurrence falls on a certain date
def occurs_on?(date)
+ date = TimeUtil.ensure_date date
begin_time = TimeUtil.beginning_of_date(date)
closing_time = TimeUtil.end_of_date(date)
occurs_between?(begin_time, closing_time)
@@ -199,6 +210,7 @@ def occurring_at?(time)
# @param [Time] closing_time - the last time to consider
# @return [Boolean] whether or not the schedules conflict at all
def conflicts_with?(other_schedule, closing_time = nil)
+ closing_time = TimeUtil.ensure_time closing_time
unless terminating? || other_schedule.terminating? || closing_time
raise ArgumentError.new 'At least one schedule must be terminating to use #conflicts_with?'
end
@@ -345,6 +357,8 @@ def reset
# Find all of the occurrences for the schedule between opening_time
# and closing_time
def find_occurrences(opening_time, closing_time = nil, limit = nil, &block)
+ opening_time = TimeUtil.ensure_time opening_time
+ closing_time = TimeUtil.ensure_time closing_time
reset
answers = []
opening_time = start_time if opening_time < start_time
@@ -5,7 +5,7 @@ class SingleOccurrenceRule < Rule
attr_reader :time
def initialize(time)
- @time = time
+ @time = TimeUtil.ensure_time time
end
# Always terminating
View
@@ -23,6 +23,22 @@ def self.now
Time.at Time.now.to_i
end
+ # Ensure that this is either nil, or a time
+ def self.ensure_time(time)
+ case time
+ when Date, DateTime then time.to_time
+ else time
+ end
+ end
+
+ # Ensure that this is either nil, or a date
+ def self.ensure_date(date)
+ case date
+ when Date then date
+ else date.to_time
+ end
+ end
+
# Serialize a time appropriate for storing
def self.serialize_time(time)
if defined?(:ActiveSupport) && const_defined?(:ActiveSupport) && time.is_a?(ActiveSupport::TimeWithZone)
@@ -11,7 +11,7 @@ def until_time
deprecated_alias :until_date, :until_time
def until(time)
- @until = time
+ @until = TimeUtil.ensure_time time
replace_validations_for(:until, [Validation.new(time)])
self
end

0 comments on commit 7de4837

Please sign in to comment.