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

rules[RW]

Public Class Methods

build() { |schedule| ... } click to toggle source

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]

# File lib/montrose/schedule.rb, line 27
def build
  schedule = new
  yield schedule if block_given?
  schedule
end
dump(obj) click to toggle 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
load(json) click to toggle 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
new(rules = []) click to toggle source
# File lib/montrose/schedule.rb, line 59
def initialize(rules = [])
  @rules = rules.map { |rule| Montrose::Recurrence.new(rule) }
end

Public Instance Methods

<<(rule) click to toggle source

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
# File lib/montrose/schedule.rb, line 75
def <<(rule)
  @rules << Montrose::Recurrence.new(rule)

  self
end
Also aliased as: add
add(rule)
Alias for: <<
as_json(*args) click to toggle source

Returns json array of options used to create the schedule

@return [Array] json of schedule recurrence options

# File lib/montrose/schedule.rb, line 156
def as_json(*args)
  to_a.as_json(*args)
end
each(&block) click to toggle source

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

# File lib/montrose/schedule.rb, line 112
def each(&block)
  events.each(&block)
end
events(opts = {}) click to toggle source

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

# 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
include?(timestamp) click to toggle source

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

# File lib/montrose/schedule.rb, line 87
def include?(timestamp)
  @rules.any? { |r| r.include?(timestamp) }
end
inspect() click to toggle source
# File lib/montrose/schedule.rb, line 168
def inspect
  "#<#{self.class}:#{object_id.to_s(16)} #{to_a.inspect}>"
end
to_a() click to toggle source

Returns an array of the options used to create the recurrence

@return [Array] array of hashes of recurrence options

# File lib/montrose/schedule.rb, line 140
def to_a
  @rules.map(&:to_hash)
end
to_json(*args) click to toggle source

Returns json string of options used to create the schedule

@return [String] json of schedule recurrences

# File lib/montrose/schedule.rb, line 148
def to_json(*args)
  JSON.dump(to_a, *args)
end
to_yaml(*args) click to toggle source

Returns options used to create the schedule recurrences in YAML format

@return [String] YAML-formatted schedule recurrence options

# File lib/montrose/schedule.rb, line 164
def to_yaml(*args)
  YAML.dump(JSON.parse(to_json(*args)))
end

Private Instance Methods

active_enums(enums) click to toggle source
# File lib/montrose/schedule.rb, line 174
def active_enums(enums)
  enums.select do |e|
    e.peek
  rescue StopIteration
    false
  end
end