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 to-do, a journal entry, time zone information, or free/busy time information, or an alarm.

Direct Known Subclasses

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Component

Returns a new instance of Component.



14
15
16
17
18
19
20
# File 'lib/icalendar/component.rb', line 14

def initialize(name)
  @name = name
  @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_id, *args) ⇒ Object

Dynamically execute getters and setters for properties and property parameters. This lets us handle all the general text properties as well as custom app related properties in a natural way, but we don’t have to write a million getters and setters for every possible thing we want to support.



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

def method_missing(method_id, *args)
  method_name = method_id.id2name.upcase

  if method_name =~ /\w+_PARAMS/ # Its a parameter request
    hash = @property_params
    val = args
  else # Or its a property request
    hash = @properties
    val = args.first unless args.empty?
  end

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/icalendar/component.rb', line 11

def name
  @name
end

#propertiesObject

Returns the value of attribute properties.



12
13
14
# File 'lib/icalendar/component.rb', line 12

def properties
  @properties
end

#property_paramsObject

Returns the value of attribute property_params.



12
13
14
# File 'lib/icalendar/component.rb', line 12

def property_params
  @property_params
end

Instance Method Details

Yields:

  • (s)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/icalendar/component.rb', line 22

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

  # Then print the properties, possible parameters and potentially
  # multiple parameter values for each parameter.
  @properties.each do |key,value| 
    # Property name
    s << "#{key.upcase}"
    
    # Possible parameters
    if @property_params.has_key?(key) 
      params = @property_params[key]
      params.each do |key,val|
        s << ";#{key}"
        unless val.respond_to?(:to_ary)
          val = [ val ]
        end
        
        # Possible parameter values
        unless val.empty?
          s << "="
          sep = "" # First entry comes after = sign, but then we need commas
          val.each do |pval| 
            s << sep << pval
            sep = ","
          end
        end
      end
    end

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

  # Any custom body of the derived component
  yield(s)
  
  # End of this component
  s << "END:#{@name.upcase}\r\n"
end