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. -
#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. -
#positive_periods_include?(time) ⇒ Boolean
Returns
trueif any of the schedule’s positive periods include the specifiedtime. -
#|(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.
113 114 115 116 117 118 |
# File 'lib/schedulability/schedule.rb', line 113 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.
91 92 93 94 95 96 97 |
# File 'lib/schedulability/schedule.rb', line 91 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? 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 71 |
# 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 ) 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 ) end |
#negative_periods_include?(time) ⇒ Boolean
Returns true if any of the schedule’s negative periods include the specified time.
84 85 86 |
# File 'lib/schedulability/schedule.rb', line 84 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 |
#positive_periods_include?(time) ⇒ Boolean
Returns true if any of the schedule’s positive periods include the specified time.
76 77 78 79 |
# File 'lib/schedulability/schedule.rb', line 76 def positive_periods_include?( time ) return self.positive_periods.empty? || find_matching_period_for( time, self.positive_periods ) end |
#|(other_schedule) ⇒ Object Also known as: +
Return a new Schedulability::Schedule object that is the union of the receiver and other_schedule.
102 103 104 105 106 107 |
# File 'lib/schedulability/schedule.rb', line 102 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.
123 124 125 |
# File 'lib/schedulability/schedule.rb', line 123 def ~@ return self.class.new( self.negative_periods, self.positive_periods ) end |