Sha256: 5018790784f89d8fa618e7fd2f16335f7efc74356c3eed12d9886c1eebf7fa4a

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Montrose
  # 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 Stack
    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

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

    # 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.
    #
    # @param [Time] time - time instance candidate for recurrence
    #
    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
montrose-0.18.0 lib/montrose/stack.rb
montrose-0.17.0 lib/montrose/stack.rb
montrose-0.16.0 lib/montrose/stack.rb
montrose-0.15.0 lib/montrose/stack.rb
montrose-0.14.0 lib/montrose/stack.rb
montrose-0.13.0 lib/montrose/stack.rb