Class: Schedulability::Schedule

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_periodsObject (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_periodsObject (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.


129
130
131
132
133
134
# File 'lib/schedulability/schedule.rb', line 129

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.


107
108
109
110
111
112
113
# File 'lib/schedulability/schedule.rb', line 107

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.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

98
99
100
# File 'lib/schedulability/schedule.rb', line 98

def exclusive?( other_schedule )
	return ( self & other_schedule ).empty?
end

#include?(time) ⇒ Boolean

Returns true if the specified time is in the schedule.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

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.

Returns:

  • (Boolean)

90
91
92
# File 'lib/schedulability/schedule.rb', line 90

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.

Returns:

  • (Boolean)

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

#to_sObject

Return a string from previously parsed Schedule period objects.


145
146
147
148
149
150
151
152
# File 'lib/schedulability/schedule.rb', line 145

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.


118
119
120
121
122
123
# File 'lib/schedulability/schedule.rb', line 118

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

#[email protected]Object

Return a new Schedulability::Schedule object that inverts the positive and negative period criteria.


139
140
141
# File 'lib/schedulability/schedule.rb', line 139

def [email protected]
	return self.class.new( self.negative_periods, self.positive_periods )
end