Class: Spud::Events::Calendar
- Inherits:
-
Object
- Object
- Spud::Events::Calendar
- Defined in:
- lib/spud_events/calendar_builder.rb
Instance Attribute Summary collapse
-
#first_weekday ⇒ Object
Returns the value of attribute first_weekday.
-
#last_weekday ⇒ Object
Returns the value of attribute last_weekday.
-
#month ⇒ Object
Returns the value of attribute month.
Instance Method Summary collapse
- #days ⇒ Object
- #each_day ⇒ Object
- #first_day ⇒ Object
- #first_day_of_week(day) ⇒ Object
-
#initialize(options = {}) ⇒ Calendar
constructor
A new instance of Calendar.
- #last_day ⇒ Object
- #last_day_of_week(day) ⇒ Object
- #mjdays ⇒ Object
- #objects_for_days(objects, day_method) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Calendar
Returns a new instance of Calendar.
58 59 60 61 62 63 64 65 66 |
# File 'lib/spud_events/calendar_builder.rb', line 58 def initialize(={}) @year = [:year] || Time.now.year @month = [:month] || Time.now.month @first_day_of_week = [:first_day_of_week] || 0 @first_weekday = first_day_of_week(@first_day_of_week) @last_weekday = last_day_of_week(@first_day_of_week) @first = Date.civil(@year, @month, 1) @last = Date.civil(@year, @month, -1) end |
Instance Attribute Details
#first_weekday ⇒ Object
Returns the value of attribute first_weekday.
57 58 59 |
# File 'lib/spud_events/calendar_builder.rb', line 57 def first_weekday @first_weekday end |
#last_weekday ⇒ Object
Returns the value of attribute last_weekday.
57 58 59 |
# File 'lib/spud_events/calendar_builder.rb', line 57 def last_weekday @last_weekday end |
#month ⇒ Object
Returns the value of attribute month.
57 58 59 |
# File 'lib/spud_events/calendar_builder.rb', line 57 def month @month end |
Instance Method Details
#days ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/spud_events/calendar_builder.rb', line 104 def days unless @days @days = [] each_day{|day| @days << day} end @days end |
#each_day ⇒ Object
68 69 70 71 72 |
# File 'lib/spud_events/calendar_builder.rb', line 68 def each_day first_day.upto(last_day) do |day| yield(day) end end |
#first_day ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/spud_events/calendar_builder.rb', line 82 def first_day first = @first - 6 while(first.wday % 7 != (@first_weekday) % 7) first = first.next end first end |
#first_day_of_week(day) ⇒ Object
120 121 122 |
# File 'lib/spud_events/calendar_builder.rb', line 120 def first_day_of_week(day) day end |
#last_day ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/spud_events/calendar_builder.rb', line 74 def last_day last = @last while(last.wday % 7 != @last_weekday % 7) last = last.next end last end |
#last_day_of_week(day) ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/spud_events/calendar_builder.rb', line 124 def last_day_of_week(day) if day > 0 day - 1 else 6 end end |
#mjdays ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/spud_events/calendar_builder.rb', line 112 def mjdays unless @mjdays @mdays = [] each_day{|day| @days << day} end @days end |
#objects_for_days(objects, day_method) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/spud_events/calendar_builder.rb', line 90 def objects_for_days(objects, day_method) unless @objects_for_days @objects_for_days = {} days.each{|day| @objects_for_days[day.strftime("%Y-%m-%d")] = [day, []]} objects.each do |o| date = o.send(day_method.to_sym).strftime("%Y-%m-%d") if @objects_for_days[date] @objects_for_days[date][1] << o end end end @objects_for_days end |