class Repeatable::Expression::WeekdayInMonth

Attributes

count[R]
weekday[R]

Public Class Methods

new(weekday:, count:) click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 6
def initialize(weekday:, count:)
  @weekday = weekday
  @count = count
end

Public Instance Methods

include?(date) click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 11
def include?(date)
  day_matches?(date) && week_matches?(date)
end

Private Instance Methods

day_matches?(date) click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 19
def day_matches?(date)
  date.wday == weekday
end
negative_count?() click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 43
def negative_count?
  count < 0
end
week_from_beginning(date) click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 31
def week_from_beginning(date)
  week_in_month(date.day - 1)
end
week_from_end(date) click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 35
def week_from_end(date)
  -week_in_month(last_date_of_month(date).day - date.day)
end
week_in_month(zero_indexed_day) click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 39
def week_in_month(zero_indexed_day)
  (zero_indexed_day / 7) + 1
end
week_matches?(date) click to toggle source
# File lib/repeatable/expression/weekday_in_month.rb, line 23
def week_matches?(date)
  if negative_count?
    week_from_end(date) == count
  else
    week_from_beginning(date) == count
  end
end