class Runt::Schedule

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

Public Class Methods

new() click to toggle source
# File lib/runt/schedule.rb, line 10
def initialize
  @elems = Hash.new
  self
end

Public Instance Methods

add(event, expression) click to toggle source

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.

# File lib/runt/schedule.rb, line 20
def add(event, expression)
  @elems[event]=expression
end
dates(event, date_range) click to toggle source

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

# 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) click to toggle source

Returns all Events whose Temporal Expression includes the given date/expression

# File lib/runt/schedule.rb, line 44
def events(date)
  self.select{|ev,xpr| xpr.include?(date);}
end
include?(event, date) click to toggle source

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

# File lib/runt/schedule.rb, line 36
def include?(event, date)
  return false unless @elems.include?(event)
  return 0<(self.select{|ev,xpr| ev.eql?(event)&&xpr.include?(date);}).size
end
select(&block) click to toggle source

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.

# File lib/runt/schedule.rb, line 55
def select(&block)
  result=[]
  @elems.each_pair{|event,xpr| result.push(event) if block.call(event,xpr);}
  result
end
update(event,&block) click to toggle source

Call the supplied block/Proc with the currently configured TemporalExpression associated with the supplied Event.

# File lib/runt/schedule.rb, line 65
def update(event,&block)
  block.call(@elems[event])
end