Class: Montrose::Schedule

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/montrose/schedule.rb

Overview

A schedule represents a group of recurrences

Author:

  • Ross Kaffenberger

Since:

  • 0.0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules = []) ⇒ Schedule

Returns a new instance of Schedule.

Since:

  • 0.0.1



59
60
61
# File 'lib/montrose/schedule.rb', line 59

def initialize(rules = [])
  @rules = rules.map { |rule| Montrose::Recurrence.new(rule) }
end

Instance Attribute Details

#rulesArray

the list of recurrences

Returns:

  • (Array)

    the current value of rules

Since:

  • 0.0.1



10
11
12
# File 'lib/montrose/schedule.rb', line 10

def rules
  @rules
end

Class Method Details

.build {|schedule| ... } ⇒ Montrose::Schedule

Instantiates a schedule and yields the instance to an optional block for building recurrences inline

Examples:

Build a schedule with multiple rules added in the given block

schedule = Montrose::Schedule.build do |s|
  s << { every: :day }
  s << { every: :year }
end

Yields:

  • (schedule)

Returns:

Since:

  • 0.0.1



27
28
29
30
31
# File 'lib/montrose/schedule.rb', line 27

def build
  schedule = new
  yield schedule if block_given?
  schedule
end

.dump(obj) ⇒ Object

Since:

  • 0.0.1



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object

Since:

  • 0.0.1



50
51
52
53
54
55
56
# 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

Instance Method Details

#<<(rule) ⇒ Object Also known as: add

Add a recurrence rule to the schedule, either by hash or recurrence instance

Examples:

Add a recurrence by hash

schedule = Montrose::Schedule.new
schedule << { every: :day }

Add a recurrence by instance

schedule = Montrose::Schedule.new
recurrence = Montrose.recurrence(every: :day)
schedule << recurrence

Since:

  • 0.0.1



75
76
77
78
79
# File 'lib/montrose/schedule.rb', line 75

def <<(rule)
  @rules << Montrose::Recurrence.new(rule)

  self
end

#as_json(*args) ⇒ Array

Returns json array of options used to create the schedule

Returns:

  • (Array)

    json of schedule recurrence options

Since:

  • 0.0.1



156
157
158
# File 'lib/montrose/schedule.rb', line 156

def as_json(*args)
  to_a.as_json(*args)
end

#each(&block) ⇒ Enumerator

Iterate over the events of a recurrence. Along with the Enumerable module, this makes Montrose occurrences enumerable like other Ruby collections

Examples:

Iterate over a finite recurrence

schedule = Montrose::Schedule.build do |s|
  s << { every: :day }
end
schedule.each do |event|
  puts event
end

Iterate over an infinite recurrence

schedule = Montrose::Schedule.build do |s|
  s << { every: :day }
end
schedule.lazy.each do |event|
  puts event
end

Returns:

  • (Enumerator)

    an enumerator of recurrence timestamps

Since:

  • 0.0.1



112
113
114
# File 'lib/montrose/schedule.rb', line 112

def each(&block)
  events.each(&block)
end

#events(opts = {}) ⇒ Enumerator

Returns an enumerator for iterating over timestamps in the schedule

Examples:

Return the events

schedule = Montrose::Schedule.build do |s|
  s << { every: :day }
end
schedule.events

Returns:

  • (Enumerator)

    an enumerator of recurrence timestamps

Since:

  • 0.0.1



126
127
128
129
130
131
132
133
134
# 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) ⇒ Boolean

Return true/false if given timestamp is included in any of the rules found in the schedule

Returns:

  • (Boolean)

    whether or not timestamp is included in schedule

Since:

  • 0.0.1



87
88
89
# File 'lib/montrose/schedule.rb', line 87

def include?(timestamp)
  @rules.any? { |r| r.include?(timestamp) }
end

#inspectObject

Since:

  • 0.0.1



168
169
170
# File 'lib/montrose/schedule.rb', line 168

def inspect
  "#<#{self.class}:#{object_id.to_s(16)} #{to_a.inspect}>"
end

#to_aArray

Returns an array of the options used to create the recurrence

Returns:

  • (Array)

    array of hashes of recurrence options

Since:

  • 0.0.1



140
141
142
# File 'lib/montrose/schedule.rb', line 140

def to_a
  @rules.map(&:to_hash)
end

#to_json(*args) ⇒ String

Returns json string of options used to create the schedule

Returns:

  • (String)

    json of schedule recurrences

Since:

  • 0.0.1



148
149
150
# File 'lib/montrose/schedule.rb', line 148

def to_json(*args)
  JSON.dump(to_a, *args)
end

#to_yaml(*args) ⇒ String

Returns options used to create the schedule recurrences in YAML format

Returns:

  • (String)

    YAML-formatted schedule recurrence options

Since:

  • 0.0.1



164
165
166
# File 'lib/montrose/schedule.rb', line 164

def to_yaml(*args)
  YAML.dump(JSON.parse(to_json(*args)))
end