Class: Mcalendar::Calendar
- Inherits:
-
Object
- Object
- Mcalendar::Calendar
- Defined in:
- lib/mcalendar/calendar.rb
Instance Method Summary collapse
- #days ⇒ Object
- #end_of_month ⇒ Object
- #first_of_month ⇒ Object
-
#initialize(year, month) ⇒ Calendar
constructor
A new instance of Calendar.
- #month_title ⇒ Object
- #reverse_color(day_str) ⇒ Object
- #reverse_todays_date ⇒ Object
- #this_month_calendar? ⇒ Boolean
- #to_s ⇒ Object
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
#days ⇒ Object
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_month ⇒ Object
15 16 17 |
# File 'lib/mcalendar/calendar.rb', line 15 def end_of_month Date.new(@year, @month, -1) end |
#first_of_month ⇒ Object
11 12 13 |
# File 'lib/mcalendar/calendar.rb', line 11 def first_of_month Date.new(@year, @month, 1) end |
#month_title ⇒ Object
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_date ⇒ Object
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
29 30 31 |
# File 'lib/mcalendar/calendar.rb', line 29 def this_month_calendar? (Date.today.year == @year) && (Date.today.month == @month) end |
#to_s ⇒ Object
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 |