Class: Mcalendar::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/mcalendar/calendar.rb

Instance Method Summary collapse

Constructor Details

#initialize(year, month) ⇒ Calendar

Returns a new instance of Calendar.



6
7
8
9
# File 'lib/mcalendar/calendar.rb', line 6

def initialize(year, month)
  @year = year
  @month = month
end

Instance Method Details

#daysObject



19
20
21
22
23
# File 'lib/mcalendar/calendar.rb', line 19

def days
  days = (1..end_of_month.day).map {|m| "%2d" % m}
  first_of_month.wday.times {days.unshift("  ")}
  days
end

#end_of_monthObject



15
16
17
# File 'lib/mcalendar/calendar.rb', line 15

def end_of_month
  Date.new(@year, @month, -1)
end

#first_of_monthObject



11
12
13
# File 'lib/mcalendar/calendar.rb', line 11

def first_of_month
  Date.new(@year, @month, 1)
end

#month_titleObject



25
26
27
# File 'lib/mcalendar/calendar.rb', line 25

def month_title
  first_of_month.strftime("%B %Y")
end

#reverse_color(day_str) ⇒ Object



33
34
35
# File 'lib/mcalendar/calendar.rb', line 33

def reverse_color(day_str)
  "\e[7m" + day_str.to_s + "\e[0m"
end

#reverse_todays_dateObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mcalendar/calendar.rb', line 37

def reverse_todays_date
  today_str = format("%2s", Date.today.day.to_s)

  if this_month_calendar?
    month_days = days.map {|x| x.sub(today_str, reverse_color(today_str))}
  else
    month_days = days
  end

  month_days
end

#this_month_calendar?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/mcalendar/calendar.rb', line 29

def this_month_calendar?
  (Date.today.year == @year) && (Date.today.month == @month) 
end

#to_sObject



49
50
51
52
53
54
55
# File 'lib/mcalendar/calendar.rb', line 49

def to_s
  week_header = Mcalendar::DAY_OF_WEEK.join(" ")
  month_header = month_title.center(week_header.size).rstrip
  calendar = [[week_header]]
  reverse_todays_date.each_slice(7) {|x| calendar << [x.join("  ")]}
  [month_header, calendar]
end