Class: EventCalendar::Week
- Inherits:
-
Array
- Object
- Array
- EventCalendar::Week
- Extended by:
- ActiveSupport::Memoizable
- Defined in:
- lib/event_calendar/week.rb
Overview
Contains an array of days representing a calendar week
Instance Method Summary collapse
-
#events ⇒ Object
Returns an array of arrays containing hashes of events to fit in an HTML calendar week row.
-
#initialize(days, events) ⇒ Week
constructor
Accepts two arguments, an array of days and an array of events.
Constructor Details
#initialize(days, events) ⇒ Week
Accepts two arguments, an array of days and an array of events
9 10 11 12 |
# File 'lib/event_calendar/week.rb', line 9 def initialize(days, events) super days add_associated_events_to_days(events) end |
Instance Method Details
#events ⇒ Object
Returns an array of arrays containing hashes of events to fit in an HTML calendar week row.
Each hash in the array represents a table cell td
when the calendar is generated. If the hash is empty, an empty td
would be generated. Otherwise, the td
will contain information related to the associated event. An event hash contains:
:event => The event object.
:span => The number of days this event takes up in the current week row.
:continued => A boolean determining if this event starts before or ends after the current week.
For example:
puts @week.events.inspect
# [
# [{}, {}, { :event => ..., :span => 1, :continued => false }, { :event => ..., :span => 1, :continued => false}, {}, {}, {}],
# [{}, {}, { :event => ..., :span => 1, :continued => false }, {}, {}, {}, { :event => ..., :span => 1, :continued => true }],
# [{}, {}, { :event => ..., :span => 1, :continued => false }, {}, {}, {}, {}],
# [{}, {}, { :event => ..., :span => 1, :continued => false }, {}, {}, {}, {}],
# [{}, {}, { :event => ..., :span => 4, :continued => false }, {}]
# ]
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/event_calendar/week.rb', line 35 def events events = [] day_events_index = inject({}) { |hash, day| hash.merge! day => 0 } until all? { |day| day_events_index[day] == day.events.size } row = [] each_with_index do |day, index| cell_count = row.inject(0) { |sum, cell| sum += (cell.empty? ? 1 : cell[:span]) } next if cell_count > index || cell_count >= 7 cell = {} unless day_events_index[day] == day.events.size cell[:event] = day.events[day_events_index[day]] cell[:span] = cell[:event].days(first, last) cell[:continued] = cell[:event].days != cell[:span] day_events_index[day] += 1 end row << cell end events << row end events end |