module RubyCalendar::Year

Constants

MONTH_NAMES

Public Instance Methods

year(year = @year, w: 2, l: 1, c: 6, m: 3) click to toggle source
# File lib/ruby_calendar/year.rb, line 7
def year(year = @year, w: 2, l: 1, c: 6, m: 3)
  w = [2, w].max
  l = [1, l].max
  c = [2, c].max
  colwidth = 7 * (w + 1) - 1
  calendar = []
  calendar.push(year.to_s.center(colwidth * m + c * (m - 1)).rstrip)
  calendar.push("\n" * l)
  MONTH_NAMES.each_slice(m).to_a.each do |months|
    calendar.push(months.map do |month|
                    format_month_name(year, month_to_i(month), colwidth, false) << (" " * c)
                  end.join.rstrip)
    calendar.push("\n" * l)
    calendar.push(months.map { format_week_header(w) << (" " * c) }.join.rstrip)
    calendar.push("\n" * l)
    month_array = months.map { |month| month_days_calendar(year, month_to_i(month)) }
    slice_month_array = month_array.map { |array| array.each_slice(7).to_a }
    max_element_count = slice_month_array.map(&:length).max - 1
    replace_array = []
    (0..max_element_count).each do |num|
      replace_array.push(slice_month_array.map { |array| array[num].nil? ? [] : array[num] })
    end
    replace_array.each do |weeks|
      calendar.push(weeks.map { |week| format_week_name(week, w) << (" " * c) }.join.rstrip)
      calendar.push("\n" * l)
    end
  end
  calendar.join.to_s
end

Private Instance Methods

month_to_i(month) click to toggle source
# File lib/ruby_calendar/year.rb, line 39
def month_to_i(month)
  MONTH_NAMES.index(month) + 1
end