Sha256: d4cbd9406d8ac66e2c4c4026e9c528f6116842cd6095f7d83ccace877060cfa1

Contents?: true

Size: 745 Bytes

Versions: 16

Compression:

Stored size: 745 Bytes

Contents

# frozen_string_literal: true

require "forwardable"

module Montrose
  module Rule
    class NthDayMatcher
      extend Forwardable

      def_delegators :@period_day, :nth_day, :first_wday, :total_days

      def initialize(wday, period_day)
        @wday = wday
        @period_day = period_day
      end

      def matches?(nth_occ)
        nth_occ == current_occ || (nth_occ < 0 && (total_occ + nth_occ + 1) == current_occ)
      end

      private

      def current_occ
        @current_occ ||= (nth_day - first_occ) / 7 + 1
      end

      def total_occ
        @total_occ ||= ((total_days - first_occ + 1) / 7.0).ceil
      end

      def first_occ
        @first_occ ||= ((7 - first_wday) + @wday) % 7 + 1
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
montrose-0.18.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.17.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.16.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.15.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.14.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.13.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.12.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.11.2 lib/montrose/rule/nth_day_matcher.rb
montrose-0.11.1 lib/montrose/rule/nth_day_matcher.rb
montrose-0.11.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.10.1 lib/montrose/rule/nth_day_matcher.rb
montrose-0.10.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.9.0 lib/montrose/rule/nth_day_matcher.rb
montrose-0.8.2 lib/montrose/rule/nth_day_matcher.rb
montrose-0.8.1 lib/montrose/rule/nth_day_matcher.rb
montrose-0.8.0 lib/montrose/rule/nth_day_matcher.rb