Class: Runt::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/runt/schedule.rb

Overview

Implementation of a pattern[http://martinfowler.com/apsupp/recurring.pdf] for recurring calendar events created by Martin Fowler.

Instance Method Summary collapse

Constructor Details

#initializeSchedule

Returns a new instance of Schedule.



10
11
12
13
# File 'lib/runt/schedule.rb', line 10

def initialize
  @elems = Hash.new
  self
end

Instance Method Details

#add(event, expression) ⇒ Object

Schedule event to occur using the given expression.



16
17
18
19
20
21
22
23
24
# File 'lib/runt/schedule.rb', line 16

def add(event, expression)

  if @elems.include?(event)
    @elems[event].push(ScheduleElement.new(event, expression))        
  else
    @elems[event] = [ScheduleElement.new(event, expression)]
  end

end

#dates(event, date_range) ⇒ Object

For the given date range, returns an Array of PDate objects at which the supplied event is scheduled to occur.



28
29
30
31
32
33
34
# File 'lib/runt/schedule.rb', line 28

def dates(event, date_range)
  result = Array.new
  date_range.each do |date|
    result.push date if include?(event,date)
  end
  result
end

#include?(event, date) ⇒ Boolean

Return true or false depend on if the supplied event is scheduled to occur on the given date.

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/runt/schedule.rb', line 38

def include?(event, date)
  return false unless @elems.include?(event)
  result = Array.new
  @elems[event].each{|element| result << element.include?(event, date) }
  result.inject{|x,y| x && y}
end