Class: Montrose::Schedule

Inherits:
Object
  • Object
show all
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

#initializeSchedule

Returns a new instance of Schedule

Since:

  • 0.0.1


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

def initialize
  @rules = []
end

Instance Attribute Details

#rulesArray

the list of recurrences

Since:

  • 0.0.1


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

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)

Since:

  • 0.0.1


23
24
25
26
27
# File 'lib/montrose/schedule.rb', line 23

def self.build
  schedule = new
  yield schedule if block_given?
  schedule
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


45
46
47
48
49
# File 'lib/montrose/schedule.rb', line 45

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

  self
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

Since:

  • 0.0.1


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

def events(opts = {})
  enums = @rules.map { |r| r.merge(opts).events }
  Enumerator.new do |y|
    loop do
      enum = active_enums(enums).min_by(&:peek) or 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

Since:

  • 0.0.1


57
58
59
# File 'lib/montrose/schedule.rb', line 57

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