A schedule object representing one or more abstract ranges of times.
The periods that express which times are not in the schedule
The periods that express which times are in the schedule
Create a new Schedule using the specified
periods
.
# File lib/schedulability/schedule.rb, line 28
def initialize( positive_periods=[], negative_periods=[] )
positive_periods ||= []
negative_periods ||= []
@positive_periods = positive_periods.flatten.uniq
@positive_periods.freeze
@negative_periods = negative_periods.flatten.uniq
@negative_periods.freeze
end
Parse one or more periods from the specified expression
and
return a Schedule created with them.
# File lib/schedulability/schedule.rb, line 21
def self::parse( expression )
positive, negative = Schedulability::Parser.extract_periods( expression )
return new( positive, negative )
end
Return a new Schedulability::Schedule object
that is the intersection of the receiver and other_schedule
.
# File lib/schedulability/schedule.rb, line 127
def &( other_schedule )
positive = intersect_periods( self.positive_periods, other_schedule.positive_periods )
negative = self.negative_periods + other_schedule.negative_periods
return self.class.new( positive, negative )
end
Returns true
if the time periods for
other_schedule
are the same as those for the receiver.
# File lib/schedulability/schedule.rb, line 105
def ==( other_schedule )
other_schedule.is_a?( self.class ) &&
self.positive_periods.all? {|period| other_schedule.positive_periods.include?(period) } &&
other_schedule.positive_periods.all? {|period| self.positive_periods.include?(period) } &&
self.negative_periods.all? {|period| other_schedule.negative_periods.include?(period) } &&
other_schedule.negative_periods.all? {|period| self.negative_periods.include?(period) }
end
Returns true
if the schedule doesn't have any time
periods.
# File lib/schedulability/schedule.rb, line 47
def empty?
return self.positive_periods.empty? && self.negative_periods.empty?
end
Returns true
if the schedule does not have any times which
overlap those of other_schedule
.
# File lib/schedulability/schedule.rb, line 96
def exclusive?( other_schedule )
return ( self & other_schedule ).empty?
end
Returns true
if the specified time
is in the
schedule.
# File lib/schedulability/schedule.rb, line 59
def include?( time )
time_obj = if time.respond_to?( :to_time )
time.to_time
else
time_obj = Time.parse( time.to_s )
time_obj
end
return ! self.negative_periods_include?( time_obj ) &&
self.positive_periods_include?( time_obj )
end
Returns true
if any of the schedule's negative periods
include the specified time
.
# File lib/schedulability/schedule.rb, line 82
def negative_periods_include?( time )
return find_matching_period_for( time, self.negative_periods )
end
Returns true
if the current time is within one of the
Schedule's periods.
# File lib/schedulability/schedule.rb, line 53
def now?
return self.include?( Time.now )
end
Returns true
if the schedule has any times which overlap those
of other_schedule
.
# File lib/schedulability/schedule.rb, line 88
def overlaps?( other_schedule )
return ! self.exclusive?( other_schedule )
end
Returns true
if any of the schedule's positive periods
include the specified time
.
# File lib/schedulability/schedule.rb, line 74
def positive_periods_include?( time )
return self.positive_periods.empty? ||
find_matching_period_for( time, self.positive_periods )
end
Return a string from previously parsed Schedule period objects.
# File lib/schedulability/schedule.rb, line 143
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
Return a new Schedulability::Schedule object
that is the union of the receiver and other_schedule
.
# File lib/schedulability/schedule.rb, line 116
def |( other_schedule )
positive = self.positive_periods + other_schedule.positive_periods
negative = intersect_periods( self.negative_periods, other_schedule.negative_periods )
return self.class.new( positive, negative )
end
Return a new Schedulability::Schedule object that inverts the positive and negative period criteria.
# File lib/schedulability/schedule.rb, line 137
def ~
return self.class.new( self.negative_periods, self.positive_periods )
end