Class: Kali::TypedList

Inherits:
Object
  • Object
show all
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

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.

Yields:

  • (item)


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_icsObject

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