Sha256: 0cf0bf76d5bf572cb333c9a0c42b19e8166538e523cd21840f67fcb9597cb6cc

Contents?: true

Size: 634 Bytes

Versions: 3

Compression:

Stored size: 634 Bytes

Contents

module Repeatable
  module Expression
    class WeekdayInMonth < Base
      def initialize(weekday:, count:)
        @weekday = weekday
        @count = count
      end

      def include?(date)
        day_matches?(date) && week_matches?(date)
      end

      def to_h
        { weekday_in_month: { weekday: weekday, count: count } }
      end

      private

      attr_reader :weekday, :count

      def day_matches?(date)
        date.wday == weekday
      end

      def week_matches?(date)
        week_in_month(date.day) == count
      end

      def week_in_month(day)
        ((day - 1) / 7) + 1
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
repeatable-0.3.0 lib/repeatable/expression/weekday_in_month.rb
repeatable-0.2.1 lib/repeatable/expression/weekday_in_month.rb
repeatable-0.2.0 lib/repeatable/expression/weekday_in_month.rb