Class: Kali::Component
- Inherits:
-
Object
- Object
- Kali::Component
- Extended by:
- Utils::Named
- Defined in:
- lib/kali/component.rb
Class Method Summary collapse
-
.component(type, method = type.method_name) ⇒ Object
Public: Add a family of components to this component.
-
.property(type, method = type.method_name) ⇒ Object
Public: Add a property to this component.
-
.property_list(type, method = type.method_name) ⇒ Object
Public: Add a family of properties to this component.
Instance Method Summary collapse
-
#components ⇒ Object
Internal: List of sub-components that form this component.
-
#initialize {|_self| ... } ⇒ Component
constructor
Public: Initialize the component.
-
#properties ⇒ Object
Internal: List of properties stored in this component.
-
#to_ics ⇒ Object
Public: Generate an iCalendar representation of the component.
Methods included from Utils::Named
Constructor Details
#initialize {|_self| ... } ⇒ Component
Public: Initialize the component.
Yields itself if a block is passed.
93 94 95 |
# File 'lib/kali/component.rb', line 93 def initialize yield self if block_given? end |
Class Method Details
.component(type, method = type.method_name) ⇒ Object
Public: Add a family of components to this component. This exposes a list to which you can add sub-components.
Example
class Calendar < Kali::Component
component Kali::Event, :events
end
Calendar.new do |cal|
cal.events.add do |event|
...
end
end
type - The class that defines the component. Must be a Kali::Component
subclass.
method - The name of the method under which to mount this property.
Returns nothing.
84 85 86 87 88 |
# File 'lib/kali/component.rb', line 84 def self.component(type, method = type.method_name) define_method method do components[type.name] ||= TypedList.new(type) end end |
.property(type, method = type.method_name) ⇒ Object
Public: Add a property to this component. This property will appear 0 or 1 times in this component. If you want to add a property that can appear multiple times in the component, look at ‘Component.property_list`.
Example
class Event < Kali::Component
property Kali::Property::Summary
property Kali::Property::Description, :desc
end
Event.new do |event|
event.summary = "Summary"
event.desc = "Description"
end
type - The class that defines the property. Must be a Kali::Property
subclass.
method - The name of the method under which to mount this property.
Returns nothing.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kali/component.rb', line 26 def self.property(type, method = type.method_name) define_method method do properties[type.name] ||= type.new end define_method "#{method}=" do |value| property = type === value ? value : type.new(value) properties[type.name] = property end end |
.property_list(type, method = type.method_name) ⇒ Object
Public: Add a family of properties to this component. This adds a property that can appear multiple times in this component. If you need a property that appears at most once in the component, look at ‘Component.property`.
Example
class Event < Kali::Component
property_list Kali::Property::Attendee, :attendees
end
Event.new do |event|
event.attendees.add do |attendee|
...
end
end
type - The class that defines the property. Must be a Kali::Property
subclass.
method - The name of the method under which to mount this property.
Returns nothing.
58 59 60 61 62 |
# File 'lib/kali/component.rb', line 58 def self.property_list(type, method = type.method_name) define_method method do properties[type.name] ||= TypedList.new(type) end end |
Instance Method Details
#components ⇒ Object
Internal: List of sub-components that form this component.
Returns a Hash.
124 125 126 |
# File 'lib/kali/component.rb', line 124 def components @components ||= {} end |
#properties ⇒ Object
Internal: List of properties stored in this component.
Returns a Hash.
117 118 119 |
# File 'lib/kali/component.rb', line 117 def properties @properties ||= {} end |
#to_ics ⇒ Object
Public: Generate an iCalendar representation of the component.
Returns a String.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/kali/component.rb', line 100 def to_ics properties = self.properties.map { |_, value| value.to_ics } components = self.components.map { |_, value| value.to_ics } lines = [ "BEGIN:#{self.class.name}", *properties.compact, *components.compact, "END:#{self.class.name}" ] lines.reject { |s| s.empty? }.join("\n") end |