Class: CalendarHelper::Calendar
- Inherits:
-
Object
- Object
- CalendarHelper::Calendar
- Defined in:
- lib/table_builder/calendar_helper.rb
Instance Attribute Summary collapse
-
#first_day ⇒ Object
readonly
Returns the value of attribute first_day.
-
#first_weekday ⇒ Object
readonly
Returns the value of attribute first_weekday.
-
#last_day ⇒ Object
readonly
Returns the value of attribute last_day.
-
#last_weekday ⇒ Object
readonly
Returns the value of attribute last_weekday.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
Instance Method Summary collapse
- #days ⇒ Object
- #each_day(&block) ⇒ Object
-
#initialize(options = {}) ⇒ Calendar
constructor
A new instance of Calendar.
- #objects_for_days(objects, &day_method) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Calendar
Returns a new instance of Calendar.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/table_builder/calendar_helper.rb', line 55 def initialize = {} @year = [:year] || Time.now.year @month = [:month] || Time.now.month @first_weekday = ([:first_weekday] || 0) % 7 @last_weekday = (@first_weekday + 6) % 7 @first = Date.civil @year, @month, 1 @last = Date.civil @year, @month, -1 @first_day = @first - (@first.wday - @first_weekday + 7) % 7 @last_day = @last + (@last_weekday - @last.wday + 7) % 7 end |
Instance Attribute Details
#first_day ⇒ Object (readonly)
Returns the value of attribute first_day.
54 55 56 |
# File 'lib/table_builder/calendar_helper.rb', line 54 def first_day @first_day end |
#first_weekday ⇒ Object (readonly)
Returns the value of attribute first_weekday.
54 55 56 |
# File 'lib/table_builder/calendar_helper.rb', line 54 def first_weekday @first_weekday end |
#last_day ⇒ Object (readonly)
Returns the value of attribute last_day.
54 55 56 |
# File 'lib/table_builder/calendar_helper.rb', line 54 def last_day @last_day end |
#last_weekday ⇒ Object (readonly)
Returns the value of attribute last_weekday.
54 55 56 |
# File 'lib/table_builder/calendar_helper.rb', line 54 def last_weekday @last_weekday end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
54 55 56 |
# File 'lib/table_builder/calendar_helper.rb', line 54 def month @month end |
Instance Method Details
#days ⇒ Object
75 76 77 |
# File 'lib/table_builder/calendar_helper.rb', line 75 def days @days ||= each_day end |
#each_day(&block) ⇒ Object
66 67 68 |
# File 'lib/table_builder/calendar_helper.rb', line 66 def each_day &block (first_day..last_day).map(&block) end |
#objects_for_days(objects, &day_method) ⇒ Object
70 71 72 73 |
# File 'lib/table_builder/calendar_helper.rb', line 70 def objects_for_days objects, &day_method objects = objects.group_by(&day_method) each_day { |day| [day, objects[day] || []] } end |