Class: Kali::TypedList
- Inherits:
-
Object
- Object
- Kali::TypedList
- Includes:
- Enumerable
- Defined in:
- lib/kali/typed_list.rb
Overview
Public: List of objects that have a specific type in them. Useful for lists of properties (like ATTENDEE or COMMENT, that can have multiple instances of the same property in a component) or sub-components.
Instance Method Summary collapse
-
#add(value = nil) {|item| ... } ⇒ Object
(also: #<<)
Public: Add a new item to the list.
-
#each(&block) ⇒ Object
Internal: Iterate over the properties in this list.
-
#initialize(type) ⇒ TypedList
constructor
Internal: Initialize the List.
-
#to_ics ⇒ Object
Public: Get an iCalendar representation of this list of properties.
Constructor Details
#initialize(type) ⇒ TypedList
Internal: Initialize the List.
type - The type of objects that this list contains.
11 12 13 14 |
# File 'lib/kali/typed_list.rb', line 11 def initialize(type) @type = type @items = [] end |
Instance Method Details
#add(value = nil) {|item| ... } ⇒ Object Also known as: <<
Public: Add a new item to the list.
value - The item to be added. If it’s an instance of the type of the list,
then it adds it directly, otherwise it passes this to the
constructor of that type. If it's `nil` it's ignored and just an
empty new instance of the correct type is added to the list.
Returns the list. Yields the Property if a block is passed.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kali/typed_list.rb', line 25 def add(value = nil) item = case value when @type; value when nil; @type.new else @type.new(value) end yield item if block_given? @items << item self end |
#each(&block) ⇒ Object
Internal: Iterate over the properties in this list.
Yields each property in turn.
48 49 50 |
# File 'lib/kali/typed_list.rb', line 48 def each(&block) @items.each(&block) end |
#to_ics ⇒ Object
Public: Get an iCalendar representation of this list of properties.
Returns a String.
41 42 43 |
# File 'lib/kali/typed_list.rb', line 41 def to_ics @items.map(&:to_ics).join("\n") end |