Class: TerminalCalendar::Month

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal_calendar/month.rb,
lib/terminal_calendar/month/calendar_day.rb

Defined Under Namespace

Classes: CalendarDay, NullDay

Constant Summary collapse

DAYS_IN_THE_WEEK =
7

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(month, year) ⇒ Month

Returns a new instance of Month.



32
33
34
35
36
37
38
# File 'lib/terminal_calendar/month.rb', line 32

def initialize(month, year)
  @month = month.to_i
  @year = year.to_i
  @start_of_month = Date.new(year, month, 1)
  @end_of_month = @start_of_month.end_of_month
  @as_rows ||= build_rows
end

Instance Attribute Details

#as_rowsArray<Array> (readonly)

Returns an array of rows representing the data.

Returns:

  • (Array<Array>)

    an array of arrays, where each inner array represents a row of data



8
9
10
# File 'lib/terminal_calendar/month.rb', line 8

def as_rows
  @as_rows
end

#end_of_monthObject (readonly)

Returns the value of attribute end_of_month.



10
11
12
# File 'lib/terminal_calendar/month.rb', line 10

def end_of_month
  @end_of_month
end

#monthObject (readonly)

Returns the value of attribute month.



10
11
12
# File 'lib/terminal_calendar/month.rb', line 10

def month
  @month
end

#start_of_monthObject (readonly)

Returns the value of attribute start_of_month.



10
11
12
# File 'lib/terminal_calendar/month.rb', line 10

def start_of_month
  @start_of_month
end

#yearObject (readonly)

Returns the value of attribute year.



10
11
12
# File 'lib/terminal_calendar/month.rb', line 10

def year
  @year
end

Class Method Details

.new(*arguments, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/terminal_calendar/month.rb', line 18

def self.new(*arguments, &block)
  month = arguments[0].to_i
  fail ArgumentError.new('Month number must be 1-12') unless month >= 1 && month <= 12

  year = arguments[1].to_i
  key = [year, month]

  TerminalCalendar.all_months.fetch(key) do
    instance = allocate
    instance.send(:initialize, *arguments, &block)
    TerminalCalendar.all_months[key] = instance.freeze
  end
end

.this_monthObject



14
15
16
# File 'lib/terminal_calendar/month.rb', line 14

def self.this_month
  new(Date.today.month, Date.today.year)
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
# File 'lib/terminal_calendar/month.rb', line 52

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/terminal_calendar/month.rb', line 56

def eql?(other)
  other.month == month && other.year == year
end

#hashObject



60
61
62
# File 'lib/terminal_calendar/month.rb', line 60

def hash
  [month, year].hash
end

#next_monthObject



40
41
42
43
44
# File 'lib/terminal_calendar/month.rb', line 40

def next_month
  new_month = month == 12 ? 1 : month + 1
  new_year = new_month == 1 ? year + 1 : year
  self.class.new(new_month, new_year)
end

#previous_monthObject



46
47
48
49
50
# File 'lib/terminal_calendar/month.rb', line 46

def previous_month
  new_month = month == 1 ? 12 : month - 1
  new_year = new_month == 12 ? year - 1 : year
  self.class.new(new_month, new_year)
end

#renderObject



64
65
66
# File 'lib/terminal_calendar/month.rb', line 64

def render
  TerminalCalendar::Selection::MonthPage.new(self).render
end