Class: EventCalendar::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/event_calendar/event.rb

Overview

Provides conveniece methods for calculating dates and spans for an event. Stores the real event object for proxying all other method calls to it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, options) ⇒ Event

Accepts a real event object which it stores to proxy method calls to, and a hash of options obtained from the EventCalendar instance.



14
15
16
# File 'lib/event_calendar/event.rb', line 14

def initialize(event, options)
  @event, @options = event, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Allows you to read options starting with :event_ using method notation.

For example: calling @event.title will return the value of @event.options[:event_title] if that option exists.

All other calls are delegated to the real :event object.



42
43
44
45
46
47
48
49
# File 'lib/event_calendar/event.rb', line 42

def method_missing(method, *args)
  option = "event_#{method}".to_sym
  if @options.has_key?(option)
    evaluate_option(option)
  else
    @event.send(method, *args)
  end
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



10
11
12
# File 'lib/event_calendar/event.rb', line 10

def event
  @event
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/event_calendar/event.rb', line 9

def options
  @options
end

Instance Method Details

#days(week_start = start_date, week_end = end_date) ⇒ Object

Calculates the number of days this day takes up on a calendar (rounding up). Optionally accepts week_start and week_end dates to calculate how many days an event takes up in a single week. For example, if an event started on a Friday and ended the following Wednesday, calling @event.days(the_sunday_after_the_event_starts) would return 4.



22
23
24
# File 'lib/event_calendar/event.rb', line 22

def days(week_start = start_date, week_end = end_date)
  (end_date > week_end ? week_end : end_date) - (start_date < week_start ? week_start : start_date) + 1
end

#end_dateObject

Returns the end date of the event. If the event stores its end as a datetime then it is converted to a date.



27
28
29
# File 'lib/event_calendar/event.rb', line 27

def end_date
  self.end.to_date
end

#start_dateObject

Returns the start date of the event. If the event stores its start as a datetime then it is converted to a date.



32
33
34
# File 'lib/event_calendar/event.rb', line 32

def start_date
  start.to_date
end