Class: Cal::MonthlyCalendar

Inherits:
Object
  • Object
show all
Includes:
Comparable
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month_number, options = {}) ⇒ MonthlyCalendar

Returns a new instance of MonthlyCalendar.



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

def initialize(year, month_number, options = {})
  @start_week_on = options[:start_week_on] || :sunday
  @month = Month.new year, month_number
end

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



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

def month
  @month
end

Class Method Details

.from_month(month, options = {}) ⇒ Object



20
21
22
23
# File 'lib/cal/monthly_calendar.rb', line 20

def from_month(month, options = {})
  month = month.to_month
  new month.year, month.number, options
end

.from_param(param) ⇒ Object



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

def from_param(param)
  year, month_number = if param.present?
    param.split '-'
  else
    now = Date.current
    [now.year, now.month]
  end
  new year, month_number
end

Instance Method Details

#<=>(other) ⇒ Object



45
46
47
48
49
# File 'lib/cal/monthly_calendar.rb', line 45

def <=>(other)
  if other.is_a?(MonthlyCalendar) && start_week_on == other.send(:start_week_on)
    month <=> other.month
  end
end

#day_namesObject



81
82
83
# File 'lib/cal/monthly_calendar.rb', line 81

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

#daysObject



65
66
67
# File 'lib/cal/monthly_calendar.rb', line 65

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

#first_dayObject



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

def first_day
  @first_day ||= begin
    date = Date.new(year, month.to_i).beginning_of_month.beginning_of_week(start_week_on)
    Day.new date, self
  end
end

#last_dayObject



58
59
60
61
62
63
# File 'lib/cal/monthly_calendar.rb', line 58

def last_day
  @last_day ||= begin
    date = Date.new(year, month.to_i).end_of_month.end_of_week(start_week_on)
    Day.new date, self
  end
end

#nextObject



77
78
79
# File 'lib/cal/monthly_calendar.rb', line 77

def next
  self.class.from_month month.succ, :start_week_on => start_week_on
end

#previousObject



73
74
75
# File 'lib/cal/monthly_calendar.rb', line 73

def previous
  self.class.from_month month.previous, :start_week_on => start_week_on
end

#to_paramObject



85
86
87
# File 'lib/cal/monthly_calendar.rb', line 85

def to_param
  "#{year}-#{month.to_i}"
end

#weeksObject



69
70
71
# File 'lib/cal/monthly_calendar.rb', line 69

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