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


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

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

Class Method Details

.build(opts = {}) ⇒ Object


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

def self.build(opts = {})
  [
    Frequency,
    Rule::After,
    Rule::Before,
    Rule::Except,
    Rule::Total,
    Rule::TimeOfDay,
    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


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

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

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