Class: EventCal::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/event_cal/calendar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date = Date.today, options = {}) ⇒ Calendar

Returns a new instance of Calendar.


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/event_cal/calendar.rb', line 6

def initialize(date = Date.today, options = {} )
  parse_initializer_arguments(date, options)
  # TODO: use beginning_of_week(:sunday) after migrates to rails 3.2
  @start_on = @base_date.beginning_of_month.beginning_of_week.advance(:days => -1)
  @end_on = @base_date.end_of_month.end_of_week.advance(:days => -1)

  if @start_on <= @base_date.beginning_of_month - 7.day
    @start_on = @base_date.beginning_of_month
  end

  if @end_on < @base_date.end_of_month
    @end_on = @base_date.end_of_month.end_of_week.advance(:days => 6)
  end

  fetch_events
end

Instance Attribute Details

#base_dateObject

Returns the value of attribute base_date


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def base_date
  @base_date
end

#end_onObject

Returns the value of attribute end_on


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def end_on
  @end_on
end

#eventsObject

Returns the value of attribute events


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def events
  @events
end

#except_eventsObject

Returns the value of attribute except_events


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def except_events
  @except_events
end

#only_eventsObject

Returns the value of attribute only_events


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def only_events
  @only_events
end

#ownerObject

Returns the value of attribute owner


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def owner
  @owner
end

#priority_eventsObject

Returns the value of attribute priority_events


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def priority_events
  @priority_events
end

#start_onObject

Returns the value of attribute start_on


4
5
6
# File 'lib/event_cal/calendar.rb', line 4

def start_on
  @start_on
end

Instance Method Details

#datesObject


23
24
25
# File 'lib/event_cal/calendar.rb', line 23

def dates
  (@start_on..@end_on).to_a
end

#events_on(date) ⇒ Object


46
47
48
# File 'lib/event_cal/calendar.rb', line 46

def events_on(date)
  @events.select{ |event| event.held_on == date }
end

#fetch_eventsObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/event_cal/calendar.rb', line 27

def fetch_events

  subclasses = if @only_events.present?
    if @priority_events.present?
      @priority_events & @only_events
    else
      @only_events
    end
  elsif @except_events.present?
    (@priority_events | ::EventCal::Event.subclasses) - @except_events
  else
    @priority_events | ::EventCal::Event.subclasses
  end

  @events = subclasses.map { |klass|
    klass.fetch_events(self)
    }.flatten
end

#to_paramObject


50
51
52
# File 'lib/event_cal/calendar.rb', line 50

def to_param
  "#{base_date.year}/#{base_date.month}/#{base_date.day}"
end