Class: Montrose::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/montrose/stack.rb

Overview

Maintains stack of recurrences rules that apply to an associated recurrence; manages advancing state on each rule in stack as time instances are iterated.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Stack

Returns a new instance of Stack.



31
32
33
# File 'lib/montrose/stack.rb', line 31

def initialize(opts = {})
  @stack = self.class.build(opts)
end

Class Method Details

.build(opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/montrose/stack.rb', line 9

def self.build(opts = {})
  [
    Frequency,
    Rule::After,
    Rule::Until,
    Rule::Covering,
    Rule::During,
    Rule::Except,
    Rule::Total,
    Rule::TimeOfDay,
    Rule::MinuteOfHour,
    Rule::HourOfDay,
    Rule::NthDayOfMonth,
    Rule::NthDayOfYear,
    Rule::DayOfWeek,
    Rule::DayOfMonth,
    Rule::DayOfYear,
    Rule::WeekOfYear,
    Rule::MonthOfYear
  ].map { |r| r.from_options(opts) }.compact
end

Instance Method Details

#advance(time) ⇒ Object

Given a time instance, advances state of when all recurrence rules on the stack match, and yielding time to the block, otherwise, invokes break? on non-matching rules.

Parameters:

  • time (Time)
    • time instance candidate for recurrence



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/montrose/stack.rb', line 42

def advance(time)
  yes, no = @stack.partition { |rule| rule.include?(time) }

  if no.empty?
    yes.all? { |rule| rule.advance!(time) } || (return false)
    puts time if ENV["DEBUG"]
    yield time if block_given?
    true
  else
    no.any? { |rule| rule.continue?(time) }
  end
end