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.
- #date_to_event_hash(event_attribute = :id) ⇒ Object
-
#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.
-
#events(date) ⇒ Object
Returns all Events whose Temporal Expression includes the given date/expression.
-
#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.
- #scheduled_dates(date_range) ⇒ Object
-
#select(&block) ⇒ Object
Selects events using the user supplied block/Proc.
-
#update(event, &block) ⇒ Object
Call the supplied block/Proc with the currently configured TemporalExpression associated with the supplied Event.
Constructor Details
#initialize ⇒ 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.
NOTE: version 0.5.0 no longer uses an Array of ScheduleElements
internally to hold data. This would only matter to clients if they
they depended on the ability to call add multiple times for the same
event. Use the update method instead.
20 21 22 |
# File 'lib/runt/schedule.rb', line 20 def add(event, expression) @elems[event]=expression end |
#date_to_event_hash(event_attribute = :id) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/runt/schedule.rb', line 73 def date_to_event_hash(event_attribute=:id) start_date = end_date = nil @elems.keys.each do |event| start_date = event.start_date if start_date.nil? || start_date > event.start_date end_date = event.end_date if end_date.nil? || end_date < event.end_date end scheduled_dates(DateRange.new(start_date, end_date)).inject({}) do |h, date| h[date] = events(date).collect{|e| e.send(event_attribute)} h 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.
26 27 28 29 30 31 32 |
# File 'lib/runt/schedule.rb', line 26 def dates(event, date_range) result=[] date_range.each do |date| result.push date if include?(event,date) end result end |
#events(date) ⇒ Object
Returns all Events whose Temporal Expression includes the given date/expression
48 49 50 |
# File 'lib/runt/schedule.rb', line 48 def events(date) self.select{|ev,xpr| xpr.include?(date);} end |
#include?(event, date) ⇒ Boolean
Return true or false depend on if the supplied event is scheduled to occur on the given date.
40 41 42 43 |
# File 'lib/runt/schedule.rb', line 40 def include?(event, date) return false unless @elems.include?(event) return 0<(self.select{|ev,xpr| ev.eql?(event)&&xpr.include?(date);}).size end |
#scheduled_dates(date_range) ⇒ Object
34 35 36 |
# File 'lib/runt/schedule.rb', line 34 def scheduled_dates(date_range) @elems.values.collect{|expr| expr.dates(date_range)}.flatten.sort.uniq end |
#select(&block) ⇒ Object
Selects events using the user supplied block/Proc. The Proc must accept two parameters: an Event and a TemporalExpression. It will be called with each existing Event-expression pair at which point it can choose to include the Event in the final result by returning true or to filter it by returning false.
59 60 61 62 63 |
# File 'lib/runt/schedule.rb', line 59 def select(&block) result=[] @elems.each_pair{|event,xpr| result.push(event) if block.call(event,xpr);} result end |
#update(event, &block) ⇒ Object
Call the supplied block/Proc with the currently configured TemporalExpression associated with the supplied Event.
69 70 71 |
# File 'lib/runt/schedule.rb', line 69 def update(event,&block) block.call(@elems[event]) end |