Schedule

class
Superclass
Object
Extended With
Loggability

A schedule object representing one or more abstract ranges of times.

Attributes

negative_periods[R]

The periods that express which times are not in the schedule

positive_periods[R]

The periods that express which times are in the schedule

Public Class Methods

anchor
new( positive_periods=[], negative_periods=[] )

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
anchor
parse( expression )

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

Public Instance Methods

anchor
&( other_schedule )

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
anchor
+( other_schedule )
Alias for: |
anchor
==( other_schedule )

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
anchor
empty?()

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
anchor
exclusive?( other_schedule )

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
Also aliased as: exclusive_of? , is_exclusive_of?
anchor
exclusive_of?( other_schedule )
Alias for: exclusive?
anchor
include?( time )

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
anchor
is_exclusive_of?( other_schedule )
Alias for: exclusive?
anchor
negative_periods_include?( time )

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
anchor
now?()

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
anchor
overlaps?( other_schedule )

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
Also aliased as: overlaps_with?
anchor
overlaps_with?( other_schedule )
Alias for: overlaps?
anchor
positive_periods_include?( time )

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
anchor
to_s()

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
anchor
|( other_schedule )

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
Also aliased as: +
anchor
~()

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