Class: Cal::MonthlyCalendar
- Inherits:
-
Object
- Object
- Cal::MonthlyCalendar
- 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
-
#month ⇒ Object
readonly
Returns the value of attribute month.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #day_names ⇒ Object
- #days ⇒ Object
- #first_day ⇒ Object
-
#initialize(year, month_number, options = {}) ⇒ MonthlyCalendar
constructor
A new instance of MonthlyCalendar.
- #last_day ⇒ Object
- #next ⇒ Object
- #previous ⇒ Object
- #to_param ⇒ Object
- #weeks ⇒ Object
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, = {}) @start_week_on = [:start_week_on] || :sunday @month = Month.new year, month_number end |
Instance Attribute Details
#month ⇒ Object (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, = {}) month = month.to_month new month.year, month.number, 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_names ⇒ Object
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 |
#days ⇒ Object
65 66 67 |
# File 'lib/cal/monthly_calendar.rb', line 65 def days @days ||= first_day..last_day end |
#first_day ⇒ Object
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_day ⇒ Object
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 |
#next ⇒ Object
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 |
#previous ⇒ Object
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_param ⇒ Object
85 86 87 |
# File 'lib/cal/monthly_calendar.rb', line 85 def to_param "#{year}-#{month.to_i}" end |
#weeks ⇒ Object
69 70 71 |
# File 'lib/cal/monthly_calendar.rb', line 69 def weeks @weeks ||= days.to_a.in_groups_of 7 end |