Class: Schedulability::Schedule
- Inherits:
-
Object
- Object
- Schedulability::Schedule
- Extended by:
- Loggability
- Defined in:
- lib/schedulability/schedule.rb
Overview
A schedule object representing one or more abstract ranges of times.
Instance Attribute Summary collapse
-
#negative_periods ⇒ Object
readonly
The periods that express which times are not in the schedule.
-
#positive_periods ⇒ Object
readonly
The periods that express which times are in the schedule.
Class Method Summary collapse
-
.parse(expression) ⇒ Object
Parse one or more periods from the specified
expressionand return a Schedule created with them.
Instance Method Summary collapse
-
#&(other_schedule) ⇒ Object
Return a new Schedulability::Schedule object that is the intersection of the receiver and
other_schedule. -
#==(other_schedule) ⇒ Object
Returns
trueif the time periods forother_scheduleare the same as those for the receiver. -
#empty? ⇒ Boolean
Returns
trueif the schedule doesn’t have any time periods. -
#exclusive?(other_schedule) ⇒ Boolean
(also: #exclusive_of?, #is_exclusive_of?)
Returns
trueif the schedule does not have any times which overlap those ofother_schedule. -
#include?(time) ⇒ Boolean
Returns
trueif the specifiedtimeis in the schedule. -
#initialize(positive_periods = [], negative_periods = []) ⇒ Schedule
constructor
Create a new Schedule using the specified
periods. -
#negative_periods_include?(time) ⇒ Boolean
Returns
trueif any of the schedule’s negative periods include the specifiedtime. -
#now? ⇒ Boolean
Returns
trueif the current time is within one of the Schedule’s periods. -
#overlaps?(other_schedule) ⇒ Boolean
(also: #overlaps_with?)
Returns
trueif the schedule has any times which overlap those ofother_schedule. -
#positive_periods_include?(time) ⇒ Boolean
Returns
trueif any of the schedule’s positive periods include the specifiedtime. -
#to_s ⇒ Object
Return a string from previously parsed Schedule period objects.
-
#|(other_schedule) ⇒ Object
(also: #+)
Return a new Schedulability::Schedule object that is the union of the receiver and
other_schedule. -
#~@ ⇒ Object
Return a new Schedulability::Schedule object that inverts the positive and negative period criteria.
Constructor Details
#initialize(positive_periods = [], negative_periods = []) ⇒ Schedule
Create a new Schedule using the specified periods.
29 30 31 32 33 34 35 36 37 |
# File 'lib/schedulability/schedule.rb', line 29 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 |
Instance Attribute Details
#negative_periods ⇒ Object (readonly)
The periods that express which times are not in the schedule
44 45 46 |
# File 'lib/schedulability/schedule.rb', line 44 def negative_periods @negative_periods end |
#positive_periods ⇒ Object (readonly)
The periods that express which times are in the schedule
41 42 43 |
# File 'lib/schedulability/schedule.rb', line 41 def positive_periods @positive_periods end |
Class Method Details
.parse(expression) ⇒ Object
Parse one or more periods from the specified expression and return a Schedule created with them.
22 23 24 25 |
# File 'lib/schedulability/schedule.rb', line 22 def self::parse( expression ) positive, negative = Schedulability::Parser.extract_periods( expression ) return new( positive, negative ) end |
Instance Method Details
#&(other_schedule) ⇒ Object
Return a new Schedulability::Schedule object that is the intersection of the receiver and other_schedule.
128 129 130 131 132 133 |
# File 'lib/schedulability/schedule.rb', line 128 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 |
#==(other_schedule) ⇒ Object
Returns true if the time periods for other_schedule are the same as those for the receiver.
106 107 108 109 110 111 112 |
# File 'lib/schedulability/schedule.rb', line 106 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 |
#empty? ⇒ Boolean
Returns true if the schedule doesn’t have any time periods.
48 49 50 |
# File 'lib/schedulability/schedule.rb', line 48 def empty? return self.positive_periods.empty? && self.negative_periods.empty? end |
#exclusive?(other_schedule) ⇒ Boolean Also known as: exclusive_of?, is_exclusive_of?
Returns true if the schedule does not have any times which overlap those of other_schedule.
97 98 99 |
# File 'lib/schedulability/schedule.rb', line 97 def exclusive?( other_schedule ) return ( self & other_schedule ).empty? end |
#include?(time) ⇒ Boolean
Returns true if the specified time is in the schedule.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/schedulability/schedule.rb', line 60 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 |
#negative_periods_include?(time) ⇒ Boolean
Returns true if any of the schedule’s negative periods include the specified time.
83 84 85 |
# File 'lib/schedulability/schedule.rb', line 83 def negative_periods_include?( time ) return find_matching_period_for( time, self.negative_periods ) end |
#now? ⇒ Boolean
Returns true if the current time is within one of the Schedule’s periods.
54 55 56 |
# File 'lib/schedulability/schedule.rb', line 54 def now? return self.include?( Time.now ) end |
#overlaps?(other_schedule) ⇒ Boolean Also known as: overlaps_with?
Returns true if the schedule has any times which overlap those of other_schedule.
89 90 91 |
# File 'lib/schedulability/schedule.rb', line 89 def overlaps?( other_schedule ) return ! self.exclusive?( other_schedule ) end |
#positive_periods_include?(time) ⇒ Boolean
Returns true if any of the schedule’s positive periods include the specified time.
75 76 77 78 |
# File 'lib/schedulability/schedule.rb', line 75 def positive_periods_include?( time ) return self.positive_periods.empty? || find_matching_period_for( time, self.positive_periods ) end |
#to_s ⇒ Object
Return a string from previously parsed Schedule period objects.
144 145 146 147 148 149 150 151 |
# File 'lib/schedulability/schedule.rb', line 144 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 |
#|(other_schedule) ⇒ Object Also known as: +
Return a new Schedulability::Schedule object that is the union of the receiver and other_schedule.
117 118 119 120 121 122 |
# File 'lib/schedulability/schedule.rb', line 117 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 |
#~@ ⇒ Object
Return a new Schedulability::Schedule object that inverts the positive and negative period criteria.
138 139 140 |
# File 'lib/schedulability/schedule.rb', line 138 def ~@ return self.class.new( self.negative_periods, self.positive_periods ) end |