Class: Cal::MonthlyCalendar

Inherits:
Object
  • Object
show all
Defined in:
lib/cal/monthly_calendar.rb

Constant Summary collapse

DAY_NAMES =
{
  :sunday => 0,
  :monday => 1,
  :tuesday => 2,
  :wednesday => 3,
  :thursday => 4,
  :friday => 5,
  :saturday => 6
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dateable, options = {}) ⇒ MonthlyCalendar

Returns a new instance of MonthlyCalendar.



17
18
19
20
21
22
# File 'lib/cal/monthly_calendar.rb', line 17

def initialize(dateable, options = {})
  @date = dateable.to_date
  @start_week_on = options[:start_week_on] || :sunday
  @month = Month.new self
  @year = Year.new self
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



24
25
26
# File 'lib/cal/monthly_calendar.rb', line 24

def date
  @date
end

#monthObject (readonly)

Returns the value of attribute month.



24
25
26
# File 'lib/cal/monthly_calendar.rb', line 24

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



24
25
26
# File 'lib/cal/monthly_calendar.rb', line 24

def year
  @year
end

Instance Method Details

#==(other) ⇒ Object



26
27
28
29
30
# File 'lib/cal/monthly_calendar.rb', line 26

def ==(other)
  other.is_a?(MonthlyCalendar) &&
    other.date.year == date.year &&
    other.date.month == date.month
end

#day_namesObject



56
57
58
# File 'lib/cal/monthly_calendar.rb', line 56

def day_names
  %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].rotate DAY_NAMES[@start_week_on]
end

#daysObject



40
41
42
# File 'lib/cal/monthly_calendar.rb', line 40

def days
  @days ||= first_day..last_day
end

#first_dayObject



32
33
34
# File 'lib/cal/monthly_calendar.rb', line 32

def first_day
  @first_day ||= Day.new date.beginning_of_month.beginning_of_week(@start_week_on), self
end

#last_dayObject



36
37
38
# File 'lib/cal/monthly_calendar.rb', line 36

def last_day
  @last_day ||= Day.new date.end_of_month.end_of_week(@start_week_on), self
end

#nextObject



52
53
54
# File 'lib/cal/monthly_calendar.rb', line 52

def next
  self.class.new date.next_month
end

#previousObject



48
49
50
# File 'lib/cal/monthly_calendar.rb', line 48

def previous
  self.class.new date.prev_month
end

#weeksObject



44
45
46
# File 'lib/cal/monthly_calendar.rb', line 44

def weeks
  @weeks ||= days.to_a.in_groups_of 7
end