class MonthOrdinals

Constants

ORDINALS
VERSION
WEEKDAYS

Public Class Methods

for(years) click to toggle source
# File lib/month_ordinals.rb, line 8
def self.for years
  years = years..years unless years.is_a? Range
  ORDINALS.map { |ordinal| { ordinal => years_map(years, ORDINALS.index(ordinal)) } }.reduce Hash.new, :merge
end

Private Class Methods

last_day_of_month(day_of_week, year, month) click to toggle source
# File lib/month_ordinals.rb, line 35
def self.last_day_of_month day_of_week, year, month
  date = Date.new year, month, -1
  offset = date.wday - day_of_week
  date -= offset % 7
end
months_map(year, ordinal, day_of_week, months=1..12) click to toggle source
# File lib/month_ordinals.rb, line 23
def self.months_map year, ordinal, day_of_week, months=1..12
  months.map do |month|
    date =
      if ordinal == 5
        last_day_of_month day_of_week, year, month
      else
        ordinal_day_of_month ordinal, day_of_week, year, month
      end
    date if date.month == month
  end.compact
end
ordinal_day_of_month(ordinal, day_of_week, year, month) click to toggle source
# File lib/month_ordinals.rb, line 41
def self.ordinal_day_of_month ordinal, day_of_week, year, month
  date = Date.new year, month, 1
  offset = day_of_week - date.wday
  date += offset % 7
  date += 7 * ordinal
end
weekdays_map(year, ordinal) click to toggle source
# File lib/month_ordinals.rb, line 19
def self.weekdays_map year, ordinal
  WEEKDAYS.map { |weekday| [weekday, months_map(year, ordinal, WEEKDAYS.index(weekday))] }.to_h
end
years_map(years, ordinal) click to toggle source
# File lib/month_ordinals.rb, line 15
def self.years_map years, ordinal
  years.map { |year| weekdays_map year, ordinal }.reduce Hash.new, :deep_array_merge
end