Sha256: 565dde459cd8b9bfe7d5e855c607c442466f2c9fa05d134446f98c29118ce6c6

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

require "montrose/rule"

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::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

    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) } or return false
        puts time if ENV["DEBUG"]
        yield time if block_given?
        true
      else
        no.any?(&:continue?)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
montrose-0.6.0 lib/montrose/stack.rb
montrose-0.5.0 lib/montrose/stack.rb
montrose-0.4.3 lib/montrose/stack.rb
montrose-0.4.2 lib/montrose/stack.rb
montrose-0.4.1 lib/montrose/stack.rb
montrose-0.4.0 lib/montrose/stack.rb
montrose-0.3.0 lib/montrose/stack.rb