Class: Icalendar::Component

Inherits:
Base show all
Defined in:
lib/icalendar/component.rb

Overview

The body of the iCalendar object consists of a sequence of calendar properties and one or more calendar components. The calendar properties are attributes that apply to the calendar as a whole. The calendar components are collections of properties that express a particular calendar semantic. For example, the calendar component can specify an Event, a Todo, a Journal entry, Timezone information, or Freebusy time information, or an Alarm.

Direct Known Subclasses

Alarm, Calendar, Event, Freebusy, Journal, Timezone, Todo

Constant Summary collapse

@@multi_properties =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Component

Returns a new instance of Component.



26
27
28
29
30
31
32
33
# File 'lib/icalendar/component.rb', line 26

def initialize(name)
  @name = name
  @components = Hash.new([])
  @properties = {}
  @property_params = {}

  @@logger.info("New #{@name[1,@name.size].capitalize}...")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (protected)



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/icalendar/component.rb', line 301

def method_missing(method_name, *args)
  method_name = method_name.to_s.downcase

  unless method_name =~ /x_.*/
    raise NoMethodError
  end

  val = args.first

  unless val.respond_to?(:to_ical)
    raise(NotImplementedError, "Value of type (" + val.class.to_s + ") does not support to_ical method!")
  end

  if method_name =~ /(.*)(=)/ # Its a setter
    @properties[$1] = val
    @@logger.debug("Setting #{$1} => #{val}")
  else # Or its a getter
    @@logger.debug("Getting #{method_name} => #{@properties[method_name]}")
    return @properties[method_name]
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/icalendar/component.rb', line 21

def name
  @name
end

#propertiesObject

Returns the value of attribute properties.



22
23
24
# File 'lib/icalendar/component.rb', line 22

def properties
  @properties
end

#property_paramsObject

Returns the value of attribute property_params.



22
23
24
# File 'lib/icalendar/component.rb', line 22

def property_params
  @property_params
end

Instance Method Details

#add_component(component) ⇒ Object Also known as: add

Add a sub-component to the current component object.



36
37
38
39
40
41
42
43
44
# File 'lib/icalendar/component.rb', line 36

def add_component(component)
  key = (component.class.to_s.downcase + 's').gsub('icalendar::', '').to_sym

  unless @components.has_key? key
    @components[key] = []
  end

  @components[key] << component
end

#custom_property(name, value) ⇒ Object

TODO: Look into the x-property, x-param stuff…



118
119
120
# File 'lib/icalendar/component.rb', line 118

def custom_property(name, value)
  @properties[name] = value
end

#multi_property?(name) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/icalendar/component.rb', line 122

def multi_property?(name)
  @@multi_properties.has_key?(name.upcase)
end

Print this icalendar component

Yields:

  • (s)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/icalendar/component.rb', line 57

def print_component
  s = ""

  # Begin a new component
  s << "BEGIN:#{@name.upcase}\r\n"

  # Then the properties
  print_properties(s)

  # Any custom body of the derived component
  yield(s)

  # End of this component
  s << "END:#{@name.upcase}\r\n"
end

Print the parameters for a specific property



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/icalendar/component.rb', line 94

def print_parameter(s, key, value)
  if @property_params.has_key?(key) 
    params = @property_params[key]
    params.each do |key,val|
      s << ";#{key}"
      val = [ val ] unless val.respond_to?(:to_ary)

      # Possible parameter values
      unless val.empty?
        s << "="
        sep = "" # First entry comes after = sign, but then we need commas
        val.each do |pval| 
          if pval.respond_to? :to_ical 
            s << sep << pval.to_ical
            sep = ","
          end
        end
      end

    end
  end
end

Print this components properties



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/icalendar/component.rb', line 74

def print_properties(s)
  @properties.each do |key,value| 
    # Take out underscore for property names that conflicted
    # with built-in words.
    if key =~ /ip_.*/
      key = key[3..-1]
    end

    # Property name
    s << "#{key.upcase}"

    # Possible parameters
    print_parameter(s, key, value)

    # Property value
    s << ":#{value.to_ical}\r\n" 
  end
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


325
326
327
328
329
330
331
# File 'lib/icalendar/component.rb', line 325

def respond_to?(method_name)
  unless method_name.to_s.downcase =~ /x-.*/
    super
  end

  true
end

#to_icalObject



48
49
50
51
52
53
54
# File 'lib/icalendar/component.rb', line 48

def to_ical
  print_component do |s|
    @components.each_value do |comps|
      comps.each { |component| s << component.to_ical }
    end
  end
end