class Montrose::Schedule
A schedule represents a group of recurrences
@author Ross Kaffenberger @since 0.0.1 @attr_reader [Array] rules the list of recurrences
Attributes
Public Class Methods
Source
# File lib/montrose/schedule.rb, line 27 def build schedule = new yield schedule if block_given? schedule end
Instantiates a schedule and yields the instance to an optional block for building recurrences inline
@example Build a schedule with multiple rules added in the given block
schedule = Montrose::Schedule.build do |s| s << { every: :day } s << { every: :year } end
@return [Montrose::Schedule]
Source
# File lib/montrose/schedule.rb, line 33 def dump(obj) return nil if obj.nil? return dump(load(obj)) if obj.is_a?(String) array = case obj when Array new(obj).to_a when self obj.to_a else fail SerializationError, "Object was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}" end JSON.dump(array) end
Source
# File lib/montrose/schedule.rb, line 50 def load(json) return nil if json.blank? new JSON.parse(json) rescue JSON::ParserError => e fail SerializationError, "Could not parse JSON: #{e}" end
Source
# File lib/montrose/schedule.rb, line 59 def initialize(rules = []) @rules = rules.map { |rule| Montrose::Recurrence.new(rule) } end
Public Instance Methods
Source
# File lib/montrose/schedule.rb, line 75 def <<(rule) @rules << Montrose::Recurrence.new(rule) self end
Add a recurrence rule to the schedule, either by hash or recurrence instance
@example Add a recurrence by hash
schedule = Montrose::Schedule.new schedule << { every: :day }
@example Add a recurrence by instance
schedule = Montrose::Schedule.new recurrence = Montrose.recurrence(every: :day) schedule << recurrence
Source
# File lib/montrose/schedule.rb, line 156 def as_json(*args) to_a.as_json(*args) end
Returns json array of options used to create the schedule
@return [Array] json of schedule recurrence options
Source
# File lib/montrose/schedule.rb, line 112 def each(&block) events.each(&block) end
Iterate over the events of a recurrence. Along with the Enumerable module, this makes Montrose
occurrences enumerable like other Ruby collections
@example Iterate over a finite recurrence
schedule = Montrose::Schedule.build do |s| s << { every: :day } end schedule.each do |event| puts event end
@example Iterate over an infinite recurrence
schedule = Montrose::Schedule.build do |s| s << { every: :day } end schedule.lazy.each do |event| puts event end
@return [Enumerator] an enumerator of recurrence timestamps
Source
# File lib/montrose/schedule.rb, line 126 def events(opts = {}) enums = @rules.map { |r| r.merge(opts).events } Enumerator.new do |y| loop do (enum = active_enums(enums).min_by(&:peek)) || break y << enum.next end end end
Returns an enumerator for iterating over timestamps in the schedule
@example Return the events
schedule = Montrose::Schedule.build do |s| s << { every: :day } end schedule.events
@return [Enumerator] an enumerator of recurrence timestamps
Source
# File lib/montrose/schedule.rb, line 87 def include?(timestamp) @rules.any? { |r| r.include?(timestamp) } end
Return true/false if given timestamp is included in any of the rules found in the schedule
@return [Boolean] whether or not timestamp is included in schedule
Source
# File lib/montrose/schedule.rb, line 168 def inspect "#<#{self.class}:#{object_id.to_s(16)} #{to_a.inspect}>" end
Source
# File lib/montrose/schedule.rb, line 140 def to_a @rules.map(&:to_hash) end
Returns an array of the options used to create the recurrence
@return [Array] array of hashes of recurrence options
Source
# File lib/montrose/schedule.rb, line 148 def to_json(*args) JSON.dump(to_a, *args) end
Returns json string of options used to create the schedule
@return [String] json of schedule recurrences
Source
# File lib/montrose/schedule.rb, line 164 def to_yaml(*args) YAML.dump(JSON.parse(to_json(*args))) end
Returns options used to create the schedule recurrences in YAML format
@return [String] YAML-formatted schedule recurrence options
Private Instance Methods
Source
# File lib/montrose/schedule.rb, line 174 def active_enums(enums) enums.select do |e| e.peek rescue StopIteration false end end