Class: Runt::Schedule
- Inherits:
-
Object
- Object
- Runt::Schedule
- 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
-
#add(event, expression) ⇒ Object
Schedule event to occur using the given expression.
-
#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.
-
#include?(event, date) ⇒ Boolean
Return true or false depend on if the supplied event is scheduled to occur on the given date.
-
#initialize ⇒ Schedule
constructor
A new instance of Schedule.
Constructor Details
#initialize ⇒ Schedule
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.
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 |