Class: Montrose::Schedule
- Inherits:
-
Object
- Object
- Montrose::Schedule
- Includes:
- Enumerable
- Defined in:
- lib/montrose/schedule.rb
Overview
A schedule represents a group of recurrences
Instance Attribute Summary collapse
-
#rules ⇒ Array
the list of recurrences.
Class Method Summary collapse
-
.build {|schedule| ... } ⇒ Montrose::Schedule
Instantiates a schedule and yields the instance to an optional block for building recurrences inline.
- .dump(obj) ⇒ Object
- .load(json) ⇒ Object
Instance Method Summary collapse
-
#<<(rule) ⇒ Object
(also: #add)
Add a recurrence rule to the schedule, either by hash or recurrence instance.
-
#as_json(*args) ⇒ Array
Returns json array of options used to create the schedule.
-
#each(&block) ⇒ Enumerator
Iterate over the events of a recurrence.
-
#events(opts = {}) ⇒ Enumerator
Returns an enumerator for iterating over timestamps in the schedule.
-
#include?(timestamp) ⇒ Boolean
Return true/false if given timestamp is included in any of the rules found in the schedule.
-
#initialize(rules = []) ⇒ Schedule
constructor
A new instance of Schedule.
- #inspect ⇒ Object
-
#to_a ⇒ Array
Returns an array of the options used to create the recurrence.
-
#to_json(*args) ⇒ String
Returns json string of options used to create the schedule.
-
#to_yaml(*args) ⇒ String
Returns options used to create the schedule recurrences in YAML format.
Constructor Details
#initialize(rules = []) ⇒ Schedule
Returns a new instance of Schedule.
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
#rules ⇒ Array
the list of recurrences
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
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
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
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
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
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
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
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
87 88 89 |
# File 'lib/montrose/schedule.rb', line 87 def include?() @rules.any? { |r| r.include?() } end |
#inspect ⇒ Object
168 169 170 |
# File 'lib/montrose/schedule.rb', line 168 def inspect "#<#{self.class}:#{object_id.to_s(16)} #{to_a.inspect}>" end |
#to_a ⇒ Array
Returns an array of the options used to create the recurrence
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
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
164 165 166 |
# File 'lib/montrose/schedule.rb', line 164 def to_yaml(*args) YAML.dump(JSON.parse(to_json(*args))) end |