Class: Icalendar::Recurrence::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/icalendar/recurrence/schedule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Schedule

Returns a new instance of Schedule.



11
12
13
# File 'lib/icalendar/recurrence/schedule.rb', line 11

def initialize(event)
  @event = event
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



9
10
11
# File 'lib/icalendar/recurrence/schedule.rb', line 9

def event
  @event
end

Instance Method Details

#all_occurrencesObject



43
44
45
46
47
48
49
# File 'lib/icalendar/recurrence/schedule.rb', line 43

def all_occurrences
  ice_cube_occurrences = ice_cube_schedule.all_occurrences

  ice_cube_occurrences.map do |occurrence|
    convert_ice_cube_occurrence(occurrence)
  end
end

#convert_duration_to_seconds(ical_duration) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/icalendar/recurrence/schedule.rb', line 87

def convert_duration_to_seconds(ical_duration)
  return 0 unless ical_duration

  conversion_rates = { seconds: 1, minutes: 60, hours: 3600, days: 86400, weeks: 604800 }
  seconds = conversion_rates.inject(0) { |sum, (unit, multiplier)| sum + ical_duration.send(unit) * multiplier }
  seconds * (ical_duration.past ? -1 : 1)
end

#convert_ice_cube_occurrence(ice_cube_occurrence) ⇒ Object



51
52
53
54
55
56
# File 'lib/icalendar/recurrence/schedule.rb', line 51

def convert_ice_cube_occurrence(ice_cube_occurrence)
  start_time = ice_cube_occurrence.start_time.utc
  end_time = ice_cube_occurrence.end_time.utc

  Icalendar::Recurrence::Occurrence.new(start_time, end_time, @event)
end

#end_timeObject



27
28
29
30
31
32
33
# File 'lib/icalendar/recurrence/schedule.rb', line 27

def end_time
  if event.end
    TimeUtil.to_time(event.end)
  else
    start_time + convert_duration_to_seconds(event.duration)
  end
end

#ice_cube_scheduleObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/icalendar/recurrence/schedule.rb', line 58

def ice_cube_schedule
  schedule = IceCube::Schedule.new
  schedule.start_time = start_time
  schedule.end_time = end_time

  rrules.each do |rrule|
    ice_cube_recurrence_rule = IceCube::Rule.from_ical(rrule.value_ical)
    schedule.add_recurrence_rule(ice_cube_recurrence_rule)
  end

  event.exdate.each do |exception_date_or_dates|
    Array(exception_date_or_dates).each do |exception_date|
      # exception times should have the same tz offset as the event start or they'll be ignored as different times
      # ignored if ActiveSupport::TimeWithZone is available
      schedule.add_exception_time(TimeUtil.to_time(exception_date, moment: start_time))
    end
  end

  event.rdate.each do |extra_date_or_dates|
    Array(extra_date_or_dates).each do |extra_date|
      # exception times should have the same tz offset as the event start or they'll be ignored as different times
      # ignored if ActiveSupport::TimeWithZone is available
      schedule.add_recurrence_time(TimeUtil.to_time(extra_date, moment: start_time))
    end
  end

  schedule
end

#occurrences_between(begin_time, closing_time, spans: false) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/icalendar/recurrence/schedule.rb', line 35

def occurrences_between(begin_time, closing_time, spans: false)
  ice_cube_occurrences = ice_cube_schedule.occurrences_between(TimeUtil.to_time(begin_time), TimeUtil.to_time(closing_time), spans: spans)

  ice_cube_occurrences.map do |occurrence|
    convert_ice_cube_occurrence(occurrence)
  end
end

#rrulesObject



19
20
21
# File 'lib/icalendar/recurrence/schedule.rb', line 19

def rrules
  event.rrule
end

#start_timeObject



23
24
25
# File 'lib/icalendar/recurrence/schedule.rb', line 23

def start_time
  TimeUtil.to_time(event.start)
end

#timezoneObject



15
16
17
# File 'lib/icalendar/recurrence/schedule.rb', line 15

def timezone
  event.tzid
end