class RubyCalendar::Calendar

Constants

WEEK_DAY_NAME

Attributes

firstweekday[RW]

Public Class Methods

new(year = today_year, month = today_month, firstweekday: 0) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 15
def initialize(year = today_year, month = today_month, firstweekday: 0)
  @year = year
  @month = month
  @firstweekday = firstweekday
end

Public Instance Methods

set_firstweekday(firstweekday) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 31
def set_firstweekday(firstweekday)
  @firstweekday = firstweekday
  self
end
set_month(month) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 26
def set_month(month)
  @month = month
  self
end
set_year(year) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 21
def set_year(year)
  @year = year
  self
end

Private Instance Methods

format_day(day, width) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 66
def format_day(day, width)
  s = day.zero? ? "" : day.to_s.rjust(2)
  s.center(width)
end
format_month_name(year, month, width, withyear = true) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 46
def format_month_name(year, month, width, withyear = true)
  if withyear
    Date.new(year, month).strftime("%B #{year}").center(width)
  else
    Date.new(year, month).strftime("%B").center(width)
  end
end
format_week_day(width) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 82
def format_week_day(width)
  names = WEEK_DAY_NAME.rotate(@firstweekday)
  if width >= 9
    names
  else
    names.map { |name| name.slice(0, [3, width].min) }
  end
end
format_week_header(width) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 54
def format_week_header(width)
  format_week_day(width).map { |day| day.center(width) }.join(" ")
end
format_week_name(week, width) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 58
def format_week_name(week, width)
  if week.empty?
    Array.new(7) { 0 }.map { |day| format_day(day, width) }.join(" ")
  else
    week.map { |day| format_day(day, width) }.join(" ")
  end
end
month_days_calendar(year, month) click to toggle source
# File lib/ruby_calendar/calendar.rb, line 71
def month_days_calendar(year, month)
  firstday_wday = (Date.new(year, month, 1).wday - @firstweekday) % 7
  lastday_date = Date.new(year, month, -1).day
  lastday_wday = (6 + @firstweekday - Date.new(year, month, -1).wday) % 7

  days = Array.new(firstday_wday) { 0 }
  days.push(*1..lastday_date)
  lastday_wday.times { days.push(0) }
  days
end
today_month() click to toggle source
# File lib/ruby_calendar/calendar.rb, line 42
def today_month
  Date.today.month
end
today_year() click to toggle source
# File lib/ruby_calendar/calendar.rb, line 38
def today_year
  Date.today.year
end