Class: Icalendar::Component
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.
Constant Summary collapse
- CAL_EXTENSION_REGEX =
/\Ax_[a-z_]+=?\Z/
- @@multi_properties =
{}
- @@multiline_properties =
{}
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#properties ⇒ Object
Returns the value of attribute properties.
Instance Method Summary collapse
-
#add_component(component) ⇒ Object
(also: #add, #add_event, #add_todo, #add_journal)
Add a sub-component to the current component object.
- #chunk_lines(str, length = MAX_LINE_LENGTH, separator = "\r\n ") ⇒ Object
-
#custom_property(name, value) ⇒ Object
TODO: Look into the x-property, x-param stuff…
- #escape_chars(value) ⇒ Object
-
#fix_conflict_with_built_in(key) ⇒ Object
Take out underscore for property names that conflicted with built-in words.
-
#initialize(name) ⇒ Component
constructor
A new instance of Component.
- #method_missing(method_name, *args, &block) ⇒ Object
- #multi_property?(name) ⇒ Boolean
- #multiline_property?(name) ⇒ Boolean
-
#new_uid ⇒ Object
Used to generate unique component ids.
-
#print_component ⇒ Object
Print this icalendar component.
- #print_headers ⇒ Object
-
#print_parameters(value) ⇒ Object
Print the parameters for a specific property.
- #print_properties(properties = properties_to_print) ⇒ Object
- #print_subcomponents ⇒ Object
- #printer ⇒ Object
- #properties_to_print ⇒ Object
- #remove_component(component) ⇒ Object (also: #remove, #remove_event, #remove_todo, #remove_journal)
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_ical ⇒ Object
Output in the icalendar format.
Methods inherited from Base
Constructor Details
#initialize(name) ⇒ Component
Returns a new instance of Component.
50 51 52 53 54 55 56 |
# File 'lib/icalendar/component.rb', line 50 def initialize(name) @name = name @components = Hash.new { |h, k| h[k] = [] } @properties = {} @@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, &block) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/icalendar/component.rb', line 408 def method_missing(method_name, *args, &block) # Allow proprietary calendar extensions to be set # # Example: # cal.x_wr_calname = "iCalendar Calendar" if method_name =~ CAL_EXTENSION_REGEX # Make sure to remove '=' from the end of the method_name so we can # define it name = method_name.to_s.chomp '=' self.class.class_eval do ical_multiline_property name, name, name end send method_name, *args else super end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
44 45 46 |
# File 'lib/icalendar/component.rb', line 44 def name @name end |
#properties ⇒ Object
Returns the value of attribute properties.
45 46 47 |
# File 'lib/icalendar/component.rb', line 45 def properties @properties end |
Instance Method Details
#add_component(component) ⇒ Object Also known as: add, add_event, add_todo, add_journal
Add a sub-component to the current component object.
59 60 61 |
# File 'lib/icalendar/component.rb', line 59 def add_component(component) @components[component.key_name] << component end |
#chunk_lines(str, length = MAX_LINE_LENGTH, separator = "\r\n ") ⇒ Object
153 154 155 156 157 |
# File 'lib/icalendar/component.rb', line 153 def chunk_lines(str, length = MAX_LINE_LENGTH, separator = "\r\n ") chunks = str.scan(/.{1,#{length}}/) lines = chunks.join(separator) << separator lines.gsub(/ *$/, '') end |
#custom_property(name, value) ⇒ Object
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.
183 184 185 |
# File 'lib/icalendar/component.rb', line 183 def custom_property(name, value) @properties[name] = value end |
#escape_chars(value) ⇒ Object
149 150 151 |
# File 'lib/icalendar/component.rb', line 149 def escape_chars(value) value.gsub("\\", "\\\\").gsub("\r\n", "\n").gsub("\r", "\n").gsub("\n", "\\n").gsub(",", "\\,").gsub(";", "\\;") end |
#fix_conflict_with_built_in(key) ⇒ Object
Take out underscore for property names that conflicted with built-in words.
145 146 147 |
# File 'lib/icalendar/component.rb', line 145 def fix_conflict_with_built_in(key) key.sub(/\Aip_/, '') end |
#multi_property?(name) ⇒ Boolean
187 188 189 |
# File 'lib/icalendar/component.rb', line 187 def multi_property?(name) @@multi_properties.has_key?(name.downcase) end |
#multiline_property?(name) ⇒ Boolean
191 192 193 |
# File 'lib/icalendar/component.rb', line 191 def multiline_property?(name) @@multiline_properties.has_key?(name.downcase) end |
#new_uid ⇒ Object
Used to generate unique component ids
92 93 94 |
# File 'lib/icalendar/component.rb', line 92 def new_uid "#{DateTime.now}_#{rand(999999999)}@#{Socket.gethostname}" end |
#print_component ⇒ Object
Print this icalendar component
106 107 108 |
# File 'lib/icalendar/component.rb', line 106 def print_component to_ical end |
#print_headers ⇒ Object
176 177 178 |
# File 'lib/icalendar/component.rb', line 176 def print_headers "" # subclasses can specify headers end |
#print_parameters(value) ⇒ Object
Print the parameters for a specific property.
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/icalendar/component.rb', line 160 def print_parameters(value) return "" unless value.respond_to?(:ical_params) Array(value.ical_params).map do |key, val| val = Array(val) next if val.empty? escaped = val.map { |v| Parser.escape(v.to_ical) }.join(',') ";#{key}=" << escaped end.join end |
#print_properties(properties = properties_to_print) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/icalendar/component.rb', line 122 def print_properties(properties = properties_to_print) excludes = %w(geo rrule categories exdate) properties.sort.map do |key, val| property = fix_conflict_with_built_in(key) prelude = property.gsub(/_/, '-').upcase if multiline_property? property val.map do |part| params = print_parameters part value = escape_chars ":#{part.to_ical}" chunk_lines "#{prelude}#{params}#{value}" end.join else params = print_parameters val value = ":#{val.to_ical}" value = escape_chars(value) unless excludes.include? property chunk_lines "#{prelude}#{params}#{value}" end end.join end |
#print_subcomponents ⇒ Object
110 111 112 113 114 |
# File 'lib/icalendar/component.rb', line 110 def print_subcomponents @components.values.map do |component_parts| Array(component_parts).map &:to_ical end.join end |
#printer ⇒ Object
116 117 118 119 120 |
# File 'lib/icalendar/component.rb', line 116 def printer ["BEGIN:#{@name.upcase}\r\n", yield, "END:#{@name.upcase}\r\n"].join end |
#properties_to_print ⇒ Object
172 173 174 |
# File 'lib/icalendar/component.rb', line 172 def properties_to_print @properties # subclasses can exclude properties end |
#remove_component(component) ⇒ Object Also known as: remove, remove_event, remove_todo, remove_journal
75 76 77 |
# File 'lib/icalendar/component.rb', line 75 def remove_component(component) @components[component.key_name].delete(component) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
428 429 430 |
# File 'lib/icalendar/component.rb', line 428 def respond_to_missing?(method_name, include_private = false) method_name.to_s =~ CAL_EXTENSION_REGEX || super end |
#to_ical ⇒ Object
Output in the icalendar format
97 98 99 100 101 102 103 |
# File 'lib/icalendar/component.rb', line 97 def to_ical printer do [print_headers, print_properties, print_subcomponents].join end end |