Class: Hiccup::Serializers::Ical

Inherits:
Object
  • Object
show all
Defined in:
lib/hiccup/serializers/ical.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Ical

Returns a new instance of Ical.



8
9
10
# File 'lib/hiccup/serializers/ical.rb', line 8

def initialize(klass)
  @klass = klass
end

Instance Method Details

#dump(obj) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hiccup/serializers/ical.rb', line 14

def dump(obj)
  @component = RiCal::Component::Event.new
  @component.dtstart = obj.start_date.to_time
  @obj = obj
  
  case obj.kind
  when :weekly;   add_weekly_rule
  when :monthly;  add_monthly_rule
  when :annually; add_yearly_rule
  end
  
  StringIO.new.tap {|io|
    @component.export_properties_to(io)
    @component.export_x_properties_to(io)
  }.string
end

#load(ics) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hiccup/serializers/ical.rb', line 33

def load(ics)
  return unless ics # Required for now, for ActiveRecord
  
  component_ics = "BEGIN:VEVENT\n#{ics}\nEND:VEVENT"
  component = RiCal.parse_string(component_ics).first
  
  @obj = @klass.new
  @obj.start_date = component.dtstart_property.try(:to_datetime)
  component.rrule_property.each do |rule|
    case rule.freq
    when "WEEKLY";    parse_weekly_rule(rule)
    when "MONTHLY";   parse_monthly_rule(rule)
    when "YEARLY";    parse_yearly_rule(rule)
    end
  end
  
  @obj
end