lib/schedulability/schedule.rb in schedulability-0.1.0 vs lib/schedulability/schedule.rb in schedulability-0.2.0
- old
+ new
@@ -44,11 +44,11 @@
attr_reader :negative_periods
### Returns +true+ if the schedule doesn't have any time periods.
def empty?
- return self.positive_periods.empty?
+ return self.positive_periods.empty? && self.negative_periods.empty?
end
### Returns +true+ if the current time is within one of the Schedule's periods.
def now?
@@ -60,11 +60,10 @@
def include?( time )
time_obj = if time.respond_to?( :to_time )
time.to_time
else
time_obj = Time.parse( time.to_s )
- self.log.debug "Parsed %p to time %p" % [ time, time_obj ]
time_obj
end
return ! self.negative_periods_include?( time_obj ) &&
self.positive_periods_include?( time_obj )
@@ -84,10 +83,26 @@
def negative_periods_include?( time )
return find_matching_period_for( time, self.negative_periods )
end
+ ### Returns +true+ if the schedule has any times which overlap those of +other_schedule+.
+ def overlaps?( other_schedule )
+ return ! self.exclusive?( other_schedule )
+ end
+ alias_method :overlaps_with?, :overlaps?
+
+
+ ### Returns +true+ if the schedule does not have any times which overlap those
+ ### of +other_schedule+.
+ def exclusive?( other_schedule )
+ return ( self & other_schedule ).empty?
+ end
+ alias_method :exclusive_of?, :exclusive?
+ alias_method :is_exclusive_of?, :exclusive?
+
+
### Returns +true+ if the time periods for +other_schedule+ are the same as those for the
### receiver.
def ==( other_schedule )
other_schedule.is_a?( self.class ) &&
self.positive_periods.all? {|period| other_schedule.positive_periods.include?(period) } &&
@@ -123,10 +138,21 @@
def ~@
return self.class.new( self.negative_periods, self.positive_periods )
end
+ ### Return a string from previously parsed Schedule period objects.
+ def to_s
+ str = Schedulability::Parser.stringify( self.positive_periods )
+ unless self.negative_periods.empty?
+ str << ", not %s" % [ Schedulability::Parser.stringify(self.negative_periods) ]
+ end
+
+ return str
+ end
+
+
#######
private
#######
### Returns true if any of the specified +periods+ contains the specified +time+.
@@ -159,10 +185,9 @@
when :yd
return time.yday
when :wk
return ( time.day / 7.0 ).ceil
when :yr
- self.log.debug "Year match: %p" % [ time.year ]
return time.year
else
# If this happens, it's likely a bug in the parser.
raise ScriptError, "unknown scale %p" % [ scale ]
end