Class: Icalendar2::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/icalendar2/calendar.rb

Overview

Constant Summary collapse

VALUE =
"VCALENDAR"

Instance Method Summary collapse

Constructor Details

#initializeCalendar

Returns a new instance of Calendar.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/icalendar2/calendar.rb', line 6

def initialize
  @components = {
    Event::VALUE => []
  }
  @properties = {
    "calscale" => Property::Nil.new,
    "method" =>   Property::Nil.new,
    "prodid" =>   Property::Nil.new,
    "version" =>  Property::Nil.new
  }
end

Instance Method Details

#add_component(component) ⇒ Object



72
73
74
# File 'lib/icalendar2/calendar.rb', line 72

def add_component(component)
  @components[component.class::VALUE] << component
end

#add_event(event) ⇒ Object



59
60
61
# File 'lib/icalendar2/calendar.rb', line 59

def add_event(event)
  events << event
end

#calscale(value = nil) ⇒ Object



18
19
20
# File 'lib/icalendar2/calendar.rb', line 18

def calscale(value = nil)
  set_property(:calscale, value)
end

#event(&block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/icalendar2/calendar.rb', line 51

def event(&block)
  e = Event.new
  e.instance_eval(&block)
  events << e

  e
end

#eventsObject



47
48
49
# File 'lib/icalendar2/calendar.rb', line 47

def events
  @components[Event::VALUE]
end

#method_property(value = nil) ⇒ Object



22
23
24
# File 'lib/icalendar2/calendar.rb', line 22

def method_property(value = nil)
  set_property(:method, value)
end

#prodid(value = nil) ⇒ Object



26
27
28
# File 'lib/icalendar2/calendar.rb', line 26

def prodid(value = nil)
  set_property(:prodid, value)
end

#set_property(property_name, value, parameters = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/icalendar2/calendar.rb', line 34

def set_property(property_name, value, parameters = {})
  property = property_name.to_s.downcase
  if value.nil?
    @properties[property].value.to_s
  elsif (factory = CalendarProperty.get_factory(property_name))
    if value.is_a? factory
      @properties[property] = value
    else
      @properties[property] = factory.new(value)
    end
  end
end

#to_icalObject



63
64
65
66
67
68
69
70
# File 'lib/icalendar2/calendar.rb', line 63

def to_ical
  str = "#{Tokens::COMPONENT_BEGIN}:#{VALUE}#{Tokens::CRLF}"
  str << body_to_ical
  str << "#{Tokens::COMPONENT_END}:#{VALUE}#{Tokens::CRLF}"
  str = str.encode("UTF-8") if str.respond_to?(:encode)
  
  str
end

#valid?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/icalendar2/calendar.rb', line 76

def valid?
  events.all?(&:valid?)
end

#version(value = nil) ⇒ Object



30
31
32
# File 'lib/icalendar2/calendar.rb', line 30

def version(value = nil)
  set_property(:version, value)
end