Class: Icalendar::Rrule::Occurrence

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/icalendar/rrule/occurrence.rb

Overview

An Occurrence represents the point of time where an event or any other component of an iCalendar happens.

A component with a repeat rule happens several times. Such a component is represented by a set of many Occurrence instances. All these occurrence instances refer to the same component called herein the base_component.

The base_component can be one of the following:

  • Icalendar::Event
  • Icalendar::Todo

furthermore it can also be one of the following:

  • Icalendar::Freebusy
  • Icalendar::Journal

but these are not tested in the current version of this GEM.

The Occurrence delegates reading of attributes to its underlying base_component.

Examples:

occurrence_of_event.description                      #=> "Meeting with Mr. X"
occurrence_of_event.not_an_attribute                 #=> Error
occurrence_of_event.description = 'new description'  #=> Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_calendar, base_component, start_time, end_time) ⇒ Occurrence

Create a new Occurrence instance.

Parameters:

  • base_calendar (Icalendar::Calendar)

    the calendar that holds the component.

  • base_component (Icalendar::Component)

    the underlying calendar-component.

  • start_time (ActiveSupport::TimeWithZone)

    the time when this occurrence starts. (might be different to the start time of the base_component)

  • end_time (ActiveSupport::TimeWithZone)

    the time when this occurrence starts. (might be different to the end time of the base_component)

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/icalendar/rrule/occurrence.rb', line 61

def initialize(base_calendar, base_component, start_time, end_time)
  raise ArgumentError, "'base_calendar' not of class 'Icalendar::Calendar'" unless
      base_calendar.nil? || base_calendar.is_a?(Icalendar::Calendar)
  raise ArgumentError, "'base_component' not of class 'Icalendar::Component'" unless
      base_component.is_a?(Icalendar::Component)
  raise ArgumentError, "'start_time' not of class 'ActiveSupport::TimeWithZone'" unless
      start_time.is_a?(ActiveSupport::TimeWithZone)
  raise ArgumentError, "'end_time' not of class 'ActiveSupport::TimeWithZone'" unless
      end_time.is_a?(ActiveSupport::TimeWithZone)

  @base_calendar  = base_calendar
  @base_component = base_component
  @start_time     = start_time
  @end_time       = end_time

  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Invoked by Ruby when the Occurrence-object is sent a message it cannot handle.

Reading of attributes will be delegated all to the base component.

An attempt to set an attribute will result in an error.

Parameters:

  • method_name (String)

    the symbol for the method called

  • arguments (*object)

    the arguments that were passed to the method.

  • []

    block the block that was passed to the method.



89
90
91
92
93
94
95
96
97
# File 'lib/icalendar/rrule/occurrence.rb', line 89

def method_missing(method_name, *arguments, &block)
  if method_name.to_s[-1, 1] == '='
    # do not allow for setter methods
    super
  else
    # delegate all other requests to the base component
    base_component.send(method_name, *arguments, &block)
  end
end

Instance Attribute Details

#base_calendarIcalendar::Calendar (readonly)

Returns the calendar this occurrence is taken from.

Returns:

  • (Icalendar::Calendar)

    the calendar this occurrence is taken from.



39
40
41
# File 'lib/icalendar/rrule/occurrence.rb', line 39

def base_calendar
  @base_calendar
end

#base_componentIcalendar::Component (readonly)

Returns the calendar-component (an event or a task) this occurrence refers to.

Returns:

  • (Icalendar::Component)

    the calendar-component (an event or a task) this occurrence refers to.



42
43
44
# File 'lib/icalendar/rrule/occurrence.rb', line 42

def base_component
  @base_component
end

#end_timeActiveSupport::TimeWithZone (readonly)

Returns the end of this occurrence.

Returns:

  • (ActiveSupport::TimeWithZone)

    the end of this occurrence.



48
49
50
# File 'lib/icalendar/rrule/occurrence.rb', line 48

def end_time
  @end_time
end

#start_timeActiveSupport::TimeWithZone (readonly)

Returns the start of this occurrence.

Returns:

  • (ActiveSupport::TimeWithZone)

    the start of this occurrence.



45
46
47
# File 'lib/icalendar/rrule/occurrence.rb', line 45

def start_time
  @start_time
end

Instance Method Details

#<=>(other) ⇒ Object

Compares this occurrence to the other. Comparison is on:

  1. @start_time
  2. @end_time


128
129
130
131
132
133
134
135
136
137
# File 'lib/icalendar/rrule/occurrence.rb', line 128

def <=>(other)
  return nil unless other.respond_to? :start_time
  return nil unless other.start_time.is_a?(ActiveSupport::TimeWithZone)
  start_compare = @start_time <=> other.start_time
  return start_compare unless start_compare.zero?

  return 0 unless other.respond_to? :end_time
  return 0 unless other.end_time.is_a?(ActiveSupport::TimeWithZone)
  @end_time <=> other.end_time
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns true if the Occurrence can respond to the given method, that is, the base component responds to the given method.

Parameters:

  • method_name (String)

    the symbol for the method called

  • include_private (defaults to: false)

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
# File 'lib/icalendar/rrule/occurrence.rb', line 111

def respond_to_missing?(method_name, include_private = false)
  if method_name.to_s[-1, 1] == '='
    # do not allow to set attributes
    super ## throws no method error
  else
    # delegate all read requests to the base component
    base_component.respond_to?(method_name, include_private)
  end
end

#time_zoneActiveSupport::TimeZone

Returns the timezone used in this component.

Returns:

  • (ActiveSupport::TimeZone)

    the timezone used in this component



101
102
103
# File 'lib/icalendar/rrule/occurrence.rb', line 101

def time_zone
  base_component.component_timezone
end