Class Icalendar::Component
In: lib/icalendar/component.rb
Parent: Icalendar::Base

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.

Methods

Attributes

name  [R] 
properties  [RW] 

Public Class methods

[Source]

# File lib/icalendar/component.rb, line 46
    def initialize(name)
      @name = name
      @components = Hash.new([])
      @properties = {}

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

Protected Class methods

Make it protected so we can monitor usage…

[Source]

# File lib/icalendar/component.rb, line 213
    def Component.ical_component(*syms)
      hash_accessor :@components, *syms
    end

Define a set of methods defining a new property, which supports multiple values for the same property name.

[Source]

# File lib/icalendar/component.rb, line 231
    def Component.ical_multi_property(property, singular, plural)
      property = "#{property}".strip.downcase.gsub(/-/, '_')
      plural = "#{plural}".strip.downcase

      # Set this key so the parser knows to use an array for
      # storing this property type.
      @@multi_properties["#{property}"] = true

      generate_multi_getter(property, plural)
      generate_multi_setter(property, plural)
      generate_multi_query(property, plural)
      generate_multi_adder(property, singular)
      generate_multi_remover(property, singular)
    end

Define a set of methods defining a new property, which supports multiple values in multiple lines with same property name

[Source]

# File lib/icalendar/component.rb, line 248
    def Component.ical_multiline_property(property, singular, plural)
      @@multiline_properties["#{property}"] = true
      ical_multi_property(property, singular, plural)
    end

Define a set of methods supporting a new property

[Source]

# File lib/icalendar/component.rb, line 218
    def Component.ical_property(property, alias_name = nil, prop_name = nil)
      property = "#{property}".strip.downcase
      alias_name = "#{alias_name}".strip.downcase unless alias_name.nil?
      # If a prop_name was given then we use that for the actual storage
      property = "#{prop_name}".strip.downcase unless prop_name.nil?

      generate_getter(property, alias_name)
      generate_setter(property, alias_name)
      generate_query(property, alias_name)
    end

Public Instance methods

add(component)

Alias for add_component

Add a sub-component to the current component object.

[Source]

# File lib/icalendar/component.rb, line 55
    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
add_event(component)

Alias for add_component

add_journal(component)

Alias for add_component

[Source]

# File lib/icalendar/component.rb, line 165
    def add_sliced_text(add_to,escaped)
      escaped = escaped.split('') # split is unicdoe-aware when `$KCODE = 'u'`
      add_to << escaped.slice!(0,MAX_LINE_LENGTH).to_s << "\r\n " while escaped.length != 0 # shift(MAX_LINE_LENGTH) does not work with ruby 1.8.6
      add_to.gsub!(/ *$/, '')
    end
add_todo(component)

Alias for add_component

TODO: Look into the x-property, x-param stuff… This would really only be needed for subclassing to add additional properties to an application using the API.

[Source]

# File lib/icalendar/component.rb, line 198
    def custom_property(name, value)
      @properties[name] = value
    end

[Source]

# File lib/icalendar/component.rb, line 161
    def escape_chars(value)
      value.gsub("\\", "\\\\").gsub("\n", "\\n").gsub(",", "\\,").gsub(";", "\\;")
    end

[Source]

# File lib/icalendar/component.rb, line 202
    def multi_property?(name)
      @@multi_properties.has_key?(name.downcase)
    end

[Source]

# File lib/icalendar/component.rb, line 206
    def multiline_property?(name)
      @@multiline_properties.has_key?(name.downcase)
    end

Used to generate unique component ids

[Source]

# File lib/icalendar/component.rb, line 98
    def new_uid
      "#{DateTime.now}_#{rand(999999999)}@#{Socket.gethostname}"
    end

Print this icalendar component

[Source]

# File lib/icalendar/component.rb, line 114
    def print_component
      # Begin a new component
      "BEGIN:#{@name.upcase}\r\n" +

      # Then the properties
      print_properties +

      # sub components
      yield +

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

Print the parameters for a specific property

[Source]

# File lib/icalendar/component.rb, line 172
    def print_parameters(val)
      s = ""
      return s unless val.respond_to?(:ical_params) and not val.ical_params.nil?

      val.ical_params.each do |key,val|
        s << ";#{key}"
        val = [ val ] unless val.is_a?(Array)

        # 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
      s
    end

Print this components properties

[Source]

# File lib/icalendar/component.rb, line 129
    def print_properties
      s = ""

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

        # Property name
        unless multiline_property?(key)
           prelude = "#{key.gsub(/_/, '-').upcase}" + 

           # Possible parameters
           print_parameters(val) 

           # Property value
           value = ":#{val.to_ical}" 
           add_sliced_text(s,prelude+escape_chars(value))
         else 
           prelude = "#{key.gsub(/_/, '-').upcase}" 
            val.each do |v| 
               params = print_parameters(v)
               value = ":#{v.to_ical}"
               add_sliced_text(s,prelude + params + escape_chars(value))
            end
         end
      end
      s
    end
remove(component)

Alias for remove_component

[Source]

# File lib/icalendar/component.rb, line 77
    def remove_component(component)
      key = (component.class.to_s.downcase + 's').gsub('icalendar::', '').to_sym

      if @components.has_key? key
        @components[key].delete(component)
      end
    end
remove_event(component)

Alias for remove_component

remove_journal(component)

Alias for remove_component

remove_todo(component)

Alias for remove_component

[Source]

# File lib/icalendar/component.rb, line 441
    def respond_to?(method_name)
      if method_name.to_s.downcase =~ /x_.*/
       true
      else
        super
      end
    end

Output in the icalendar format

[Source]

# File lib/icalendar/component.rb, line 103
    def to_ical
      print_component do
        s = ""
        @components.each_value do |comps|
          comps.each { |component| s << component.to_ical }
        end
        s
      end
    end

[Validate]