Class: Selene::EventBuilder

Inherits:
ComponentBuilder show all
Defined in:
lib/selene/event_builder.rb

Instance Attribute Summary

Attributes inherited from ComponentBuilder

#component, #errors, #name, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ComponentBuilder

#add, #contains?, #parse, #to_ical

Methods included from ComponentValidator

#can_contain?, #error, included, inherited, #multiple?, #properties, #required?

Constructor Details

#initializeEventBuilder

Returns a new instance of EventBuilder.



48
49
50
# File 'lib/selene/event_builder.rb', line 48

def initialize
  super('vevent')
end

Class Method Details

.update(component, properties) ⇒ Object

TODO: if dtstart is a date, dtend must be as well TODO: multi-day durations must be ‘dur-day’ or ‘dur-week’



44
45
46
# File 'lib/selene/event_builder.rb', line 44

def self.update(component, properties)
  new('vevent', component).update_properties(properties)
end

Instance Method Details

#can_add?(property) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/selene/event_builder.rb', line 88

def can_add?(property)
  if property.name == 'dtend' && contains?('duration')
    error('dtend', "The 'dtend' property cannot be set if the 'duration' property already exists")
  elsif property.name == 'duration' && contains?('dtend')
    error('duration', "The 'duration' property cannot be set if the 'dtend' property already exists")
  end
  super(property)
end

#parent=(builder) ⇒ Object

Raises:

  • (Exception)


83
84
85
86
# File 'lib/selene/event_builder.rb', line 83

def parent=(builder)
  raise Exception.new("Event components cannot be nested inside anything but a calendar component") unless builder.is_a?(CalendarBuilder)
  super(builder)
end

#update_properties(properties) ⇒ Object



52
53
54
55
56
57
# File 'lib/selene/event_builder.rb', line 52

def update_properties(properties)
  properties.inject(self.component) do |component, (name, value)|
    component = update_property(component, name, value)
    component
  end
end

#update_property(component, name, value) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/selene/event_builder.rb', line 59

def update_property(component, name, value)
  case name
  when 'dtstart'
    component.merge('dtstart' => [value.strftime('%Y%m%dT%H%M%S'), component['dtstart'][1]])
  else
    component
  end
end

#valid?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/selene/event_builder.rb', line 97

def valid?
  if !contains?('dtstart') && parent && !parent.contains?('method')
    error('dtstart', "The 'dtstart' property is required if the calendar does not have a 'method' property")
  end
  super
end

#value(property) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/selene/event_builder.rb', line 68

def value(property)
  case property.name
  when 'dtstamp', 'dtstart', 'dtend'
    property.value_with_params
  when 'exdate'
    property.values_with_params
  when 'geo'
    property.values
  when 'rrule'
    property.rrule
  else
    super
  end
end