tilia-vobject 4.0.0.pre.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rubocop.yml +32 -0
  4. data/.simplecov +4 -0
  5. data/.travis.yml +3 -0
  6. data/CHANGELOG.sabre.md +626 -0
  7. data/CONTRIBUTING.md +25 -0
  8. data/Gemfile +17 -0
  9. data/Gemfile.lock +68 -0
  10. data/LICENSE +27 -0
  11. data/LICENSE.sabre +27 -0
  12. data/README.md +63 -0
  13. data/Rakefile +17 -0
  14. data/bin/vobject +7 -0
  15. data/lib/tilia/v_object/birthday_calendar_generator.rb +142 -0
  16. data/lib/tilia/v_object/cli.rb +582 -0
  17. data/lib/tilia/v_object/component/available.rb +107 -0
  18. data/lib/tilia/v_object/component/v_alarm.rb +114 -0
  19. data/lib/tilia/v_object/component/v_availability.rb +128 -0
  20. data/lib/tilia/v_object/component/v_calendar.rb +468 -0
  21. data/lib/tilia/v_object/component/v_card.rb +457 -0
  22. data/lib/tilia/v_object/component/v_event.rb +127 -0
  23. data/lib/tilia/v_object/component/v_free_busy.rb +81 -0
  24. data/lib/tilia/v_object/component/v_journal.rb +75 -0
  25. data/lib/tilia/v_object/component/v_time_zone.rb +51 -0
  26. data/lib/tilia/v_object/component/v_todo.rb +147 -0
  27. data/lib/tilia/v_object/component.rb +591 -0
  28. data/lib/tilia/v_object/date_time_parser.rb +486 -0
  29. data/lib/tilia/v_object/document.rb +218 -0
  30. data/lib/tilia/v_object/element_list.rb +18 -0
  31. data/lib/tilia/v_object/eof_exception.rb +8 -0
  32. data/lib/tilia/v_object/free_busy_data.rb +149 -0
  33. data/lib/tilia/v_object/free_busy_generator.rb +465 -0
  34. data/lib/tilia/v_object/i_tip/broker.rb +909 -0
  35. data/lib/tilia/v_object/i_tip/i_tip_exception.rb +9 -0
  36. data/lib/tilia/v_object/i_tip/message.rb +109 -0
  37. data/lib/tilia/v_object/i_tip/same_organizer_for_all_components_exception.rb +13 -0
  38. data/lib/tilia/v_object/i_tip.rb +10 -0
  39. data/lib/tilia/v_object/node.rb +192 -0
  40. data/lib/tilia/v_object/parameter.rb +327 -0
  41. data/lib/tilia/v_object/parse_exception.rb +7 -0
  42. data/lib/tilia/v_object/parser/json.rb +149 -0
  43. data/lib/tilia/v_object/parser/mime_dir.rb +543 -0
  44. data/lib/tilia/v_object/parser/parser.rb +61 -0
  45. data/lib/tilia/v_object/parser/xml/element/key_value.rb +60 -0
  46. data/lib/tilia/v_object/parser/xml/element.rb +11 -0
  47. data/lib/tilia/v_object/parser/xml.rb +322 -0
  48. data/lib/tilia/v_object/parser.rb +10 -0
  49. data/lib/tilia/v_object/property/binary.rb +96 -0
  50. data/lib/tilia/v_object/property/boolean.rb +57 -0
  51. data/lib/tilia/v_object/property/flat_text.rb +52 -0
  52. data/lib/tilia/v_object/property/float_value.rb +107 -0
  53. data/lib/tilia/v_object/property/i_calendar/cal_address.rb +49 -0
  54. data/lib/tilia/v_object/property/i_calendar/date.rb +15 -0
  55. data/lib/tilia/v_object/property/i_calendar/date_time.rb +330 -0
  56. data/lib/tilia/v_object/property/i_calendar/duration.rb +65 -0
  57. data/lib/tilia/v_object/property/i_calendar/period.rb +124 -0
  58. data/lib/tilia/v_object/property/i_calendar/recur.rb +173 -0
  59. data/lib/tilia/v_object/property/i_calendar.rb +14 -0
  60. data/lib/tilia/v_object/property/integer_value.rb +60 -0
  61. data/lib/tilia/v_object/property/text.rb +352 -0
  62. data/lib/tilia/v_object/property/time.rb +85 -0
  63. data/lib/tilia/v_object/property/unknown.rb +30 -0
  64. data/lib/tilia/v_object/property/uri.rb +78 -0
  65. data/lib/tilia/v_object/property/utc_offset.rb +56 -0
  66. data/lib/tilia/v_object/property/v_card/date.rb +31 -0
  67. data/lib/tilia/v_object/property/v_card/date_and_or_time.rb +343 -0
  68. data/lib/tilia/v_object/property/v_card/date_time.rb +22 -0
  69. data/lib/tilia/v_object/property/v_card/language_tag.rb +41 -0
  70. data/lib/tilia/v_object/property/v_card/time_stamp.rb +74 -0
  71. data/lib/tilia/v_object/property/v_card.rb +13 -0
  72. data/lib/tilia/v_object/property.rb +532 -0
  73. data/lib/tilia/v_object/reader.rb +73 -0
  74. data/lib/tilia/v_object/recur/event_iterator.rb +417 -0
  75. data/lib/tilia/v_object/recur/no_instances_exception.rb +11 -0
  76. data/lib/tilia/v_object/recur/r_date_iterator.rb +138 -0
  77. data/lib/tilia/v_object/recur/r_rule_iterator.rb +717 -0
  78. data/lib/tilia/v_object/recur.rb +10 -0
  79. data/lib/tilia/v_object/settings.rb +32 -0
  80. data/lib/tilia/v_object/splitter/i_calendar.rb +95 -0
  81. data/lib/tilia/v_object/splitter/splitter_interface.rb +31 -0
  82. data/lib/tilia/v_object/splitter/v_card.rb +56 -0
  83. data/lib/tilia/v_object/splitter.rb +9 -0
  84. data/lib/tilia/v_object/string_util.rb +58 -0
  85. data/lib/tilia/v_object/time_zone_data/exchange_zones.rb +96 -0
  86. data/lib/tilia/v_object/time_zone_data/lotus_zones.rb +104 -0
  87. data/lib/tilia/v_object/time_zone_data/php_zones.rb +49 -0
  88. data/lib/tilia/v_object/time_zone_data/windows_zones.rb +121 -0
  89. data/lib/tilia/v_object/time_zone_data.rb +10 -0
  90. data/lib/tilia/v_object/time_zone_util.rb +213 -0
  91. data/lib/tilia/v_object/uuid_util.rb +51 -0
  92. data/lib/tilia/v_object/v_card_converter.rb +354 -0
  93. data/lib/tilia/v_object/version.rb +9 -0
  94. data/lib/tilia/v_object/writer.rb +56 -0
  95. data/lib/tilia/v_object.rb +45 -0
  96. data/lib/tilia/vobject.rb +1 -0
  97. data/resources/schema/xcal.rng +1192 -0
  98. data/resources/schema/xcard.rng +388 -0
  99. data/test/test_helper.rb +56 -0
  100. data/test/v_object/attach_issue_test.rb +19 -0
  101. data/test/v_object/birthday_calendar_generator_test.rb +463 -0
  102. data/test/v_object/cli_mock.rb +19 -0
  103. data/test/v_object/cli_test.rb +460 -0
  104. data/test/v_object/component/available_test.rb +59 -0
  105. data/test/v_object/component/v_alarm_test.rb +160 -0
  106. data/test/v_object/component/v_availability_test.rb +388 -0
  107. data/test/v_object/component/v_calendar_test.rb +646 -0
  108. data/test/v_object/component/v_card_test.rb +258 -0
  109. data/test/v_object/component/v_event_test.rb +85 -0
  110. data/test/v_object/component/v_free_busy_test.rb +59 -0
  111. data/test/v_object/component/v_journal_test.rb +85 -0
  112. data/test/v_object/component/v_time_zone_test.rb +47 -0
  113. data/test/v_object/component/v_todo_test.rb +172 -0
  114. data/test/v_object/component_test.rb +419 -0
  115. data/test/v_object/date_time_parser_test.rb +526 -0
  116. data/test/v_object/document_test.rb +71 -0
  117. data/test/v_object/element_list_test.rb +27 -0
  118. data/test/v_object/em_client_test.rb +53 -0
  119. data/test/v_object/empty_parameter_test.rb +65 -0
  120. data/test/v_object/empty_value_issue_test.rb +25 -0
  121. data/test/v_object/fake_component.rb +21 -0
  122. data/test/v_object/free_busy_data_test.rb +285 -0
  123. data/test/v_object/free_busy_generator_test.rb +637 -0
  124. data/test/v_object/google_colon_escaping_test.rb +27 -0
  125. data/test/v_object/i_calendar/attach_parse_test.rb +24 -0
  126. data/test/v_object/i_tip/broker_attendee_reply_test.rb +1042 -0
  127. data/test/v_object/i_tip/broker_delete_event_test.rb +175 -0
  128. data/test/v_object/i_tip/broker_new_event_test.rb +440 -0
  129. data/test/v_object/i_tip/broker_process_message_test.rb +153 -0
  130. data/test/v_object/i_tip/broker_process_reply_test.rb +402 -0
  131. data/test/v_object/i_tip/broker_tester.rb +71 -0
  132. data/test/v_object/i_tip/broker_update_event_test.rb +763 -0
  133. data/test/v_object/i_tip/evolution_test.rb +2644 -0
  134. data/test/v_object/i_tip/message_test.rb +25 -0
  135. data/test/v_object/issue153.vcf +352 -0
  136. data/test/v_object/issue153_test.rb +12 -0
  137. data/test/v_object/issue26_test.rb +25 -0
  138. data/test/v_object/issue36_work_around_test.rb +37 -0
  139. data/test/v_object/issue40_test.rb +26 -0
  140. data/test/v_object/issue64.vcf +351 -0
  141. data/test/v_object/issue64_test.rb +17 -0
  142. data/test/v_object/issue96_test.rb +22 -0
  143. data/test/v_object/issue_undefined_index_test.rb +24 -0
  144. data/test/v_object/j_cal_test.rb +150 -0
  145. data/test/v_object/j_card_test.rb +192 -0
  146. data/test/v_object/line_folding_issue_test.rb +19 -0
  147. data/test/v_object/mock_document.rb +6 -0
  148. data/test/v_object/parameter_test.rb +109 -0
  149. data/test/v_object/parser/json_test.rb +370 -0
  150. data/test/v_object/parser/mime_dir_test.rb +14 -0
  151. data/test/v_object/parser/quoted_printable_test.rb +78 -0
  152. data/test/v_object/parser/xml_test.rb +2563 -0
  153. data/test/v_object/property/binary_test.rb +12 -0
  154. data/test/v_object/property/boolean_test.rb +18 -0
  155. data/test/v_object/property/compound_test.rb +43 -0
  156. data/test/v_object/property/float_test.rb +20 -0
  157. data/test/v_object/property/i_calendar/cal_address_test.rb +26 -0
  158. data/test/v_object/property/i_calendar/date_time_test.rb +303 -0
  159. data/test/v_object/property/i_calendar/duration_test.rb +14 -0
  160. data/test/v_object/property/i_calendar/recur_test.rb +39 -0
  161. data/test/v_object/property/text_test.rb +81 -0
  162. data/test/v_object/property/v_card/date_and_or_time_test.rb +205 -0
  163. data/test/v_object/property/v_card/language_tag_test.rb +35 -0
  164. data/test/v_object/property_test.rb +338 -0
  165. data/test/v_object/reader_test.rb +403 -0
  166. data/test/v_object/recur/event_iterator/by_month_in_daily_test.rb +52 -0
  167. data/test/v_object/recur/event_iterator/by_set_pos_hang_test.rb +55 -0
  168. data/test/v_object/recur/event_iterator/expand_floating_times_test.rb +109 -0
  169. data/test/v_object/recur/event_iterator/fifth_tuesday_problem_test.rb +45 -0
  170. data/test/v_object/recur/event_iterator/incorrect_expand_test.rb +53 -0
  171. data/test/v_object/recur/event_iterator/infinite_loop_problem_test.rb +75 -0
  172. data/test/v_object/recur/event_iterator/issue48_test.rb +43 -0
  173. data/test/v_object/recur/event_iterator/issue50_test.rb +123 -0
  174. data/test/v_object/recur/event_iterator/main_test.rb +1222 -0
  175. data/test/v_object/recur/event_iterator/missing_overridden_test.rb +55 -0
  176. data/test/v_object/recur/event_iterator/no_instances_test.rb +32 -0
  177. data/test/v_object/recur/event_iterator/override_first_event_test.rb +106 -0
  178. data/test/v_object/recur/r_date_iterator_test.rb +44 -0
  179. data/test/v_object/recur/r_rule_iterator_test.rb +608 -0
  180. data/test/v_object/recurrence_iterator/UntilRespectsTimezoneTest.ics +39 -0
  181. data/test/v_object/slash_r_test.rb +15 -0
  182. data/test/v_object/splitter/i_calendar_test.rb +299 -0
  183. data/test/v_object/splitter/v_card_test.rb +173 -0
  184. data/test/v_object/string_util_test.rb +37 -0
  185. data/test/v_object/test_case.rb +42 -0
  186. data/test/v_object/time_zone_util_test.rb +271 -0
  187. data/test/v_object/uuid_util_test.rb +18 -0
  188. data/test/v_object/v_card21_test.rb +43 -0
  189. data/test/v_object/v_card_converter_test.rb +419 -0
  190. data/test/v_object/version_test.rb +15 -0
  191. data/test/v_object/writer_test.rb +33 -0
  192. data/tilia-vobject.gemspec +17 -0
  193. metadata +308 -0
@@ -0,0 +1,417 @@
1
+ module Tilia
2
+ module VObject
3
+ module Recur
4
+ # This class is used to determine new for a recurring event, when the next
5
+ # events occur.
6
+ #
7
+ # This iterator may loop infinitely in the future, therefore it is important
8
+ # that if you use this class, you set hard limits for the amount of iterations
9
+ # you want to handle.
10
+ #
11
+ # Note that currently there is not full support for the entire iCalendar
12
+ # specification, as it's very complex and contains a lot of permutations
13
+ # that's not yet used very often in software.
14
+ #
15
+ # For the focus has been on features as they actually appear in Calendaring
16
+ # software, but this may well get expanded as needed / on demand
17
+ #
18
+ # The following RRULE properties are supported
19
+ # * UNTIL
20
+ # * INTERVAL
21
+ # * COUNT
22
+ # * FREQ=DAILY
23
+ # * BYDAY
24
+ # * BYHOUR
25
+ # * BYMONTH
26
+ # * FREQ=WEEKLY
27
+ # * BYDAY
28
+ # * BYHOUR
29
+ # * WKST
30
+ # * FREQ=MONTHLY
31
+ # * BYMONTHDAY
32
+ # * BYDAY
33
+ # * BYSETPOS
34
+ # * FREQ=YEARLY
35
+ # * BYMONTH
36
+ # * BYMONTHDAY (only if BYMONTH is also set)
37
+ # * BYDAY (only if BYMONTH is also set)
38
+ #
39
+ # Anything beyond this is 'undefined', which means that it may get ignored, or
40
+ # you may get unexpected results. The effect is that in some applications the
41
+ # specified recurrence may look incorrect, or is missing.
42
+ #
43
+ # The recurrence iterator also does not yet support THISANDFUTURE.
44
+ class EventIterator
45
+ # Reference timeZone for floating dates and times.
46
+ #
47
+ # @var DateTimeZone
48
+ # RUBY: attr_accessor :time_zone
49
+
50
+ # True if we're iterating an all-day event.
51
+ #
52
+ # @var bool
53
+ # RUBY: attr_accessor :all_day
54
+
55
+ # Creates the iterator.
56
+ #
57
+ # There's three ways to set up the iterator.
58
+ #
59
+ # 1. You can pass a VCALENDAR component and a UID.
60
+ # 2. You can pass an array of VEVENTs (all UIDS should match).
61
+ # 3. You can pass a single VEVENT component.
62
+ #
63
+ # Only the second method is recomended. The other 1 and 3 will be removed
64
+ # at some point in the future.
65
+ #
66
+ # The uid parameter is only required for the first method.
67
+ #
68
+ # @param Component|array input
69
+ # @param string|null uid
70
+ # @param DateTimeZone time_zone Reference timezone for floating dates and
71
+ # times.
72
+ def initialize(input, uid = nil, time_zone = nil)
73
+ @overridden_events = []
74
+ @exceptions = {}
75
+ @all_day = false
76
+
77
+ time_zone = ActiveSupport::TimeZone.new('UTC') if time_zone.nil?
78
+
79
+ @time_zone = time_zone
80
+
81
+ if input.is_a?(Array)
82
+ events = input
83
+ elsif input.is_a?(Component::VEvent)
84
+ # Single instance mode.
85
+ events = [input]
86
+ else
87
+ # Calendar + UID mode.
88
+ uid = uid.to_s
89
+ if uid.blank?
90
+ fail ArgumentError, 'The UID argument is required when a VCALENDAR is passed to this constructor'
91
+ end
92
+ unless input.key?('VEVENT')
93
+ fail ArgumentError, 'No events found in this calendar'
94
+ end
95
+
96
+ events = input.by_uid(uid)
97
+ end
98
+
99
+ events.each do |vevent|
100
+ if !vevent.key?('RECURRENCE-ID')
101
+ @master_event = vevent
102
+ else
103
+ @exceptions[vevent['RECURRENCE-ID'].date_time(@time_zone).to_i] = true
104
+ @overridden_events << vevent
105
+ end
106
+ end
107
+
108
+ unless @master_event
109
+ # No base event was found. CalDAV does allow cases where only
110
+ # overridden instances are stored.
111
+ #
112
+ # In this particular case, we're just going to grab the first
113
+ # event and use that instead. This may not always give the
114
+ # desired result.
115
+ if @overridden_events.size == 0
116
+ fail ArgumentError, "This VCALENDAR did not have an event with UID: #{uid}"
117
+ end
118
+ @master_event = @overridden_events.shift
119
+ end
120
+
121
+ @start_date = @master_event['DTSTART'].date_time(@time_zone)
122
+ @all_day = !@master_event['DTSTART'].time?
123
+
124
+ if @master_event.key?('EXDATE')
125
+ @master_event['EXDATE'].each do |ex_date|
126
+ ex_date.date_times(@time_zone).each do |dt|
127
+ @exceptions[dt.to_i] = true
128
+ end
129
+ end
130
+ end
131
+
132
+ if @master_event.key?('DTEND')
133
+ @event_duration = (@master_event['DTEND'].date_time(@time_zone).to_i - @start_date.to_i).seconds
134
+ elsif @master_event.key?('DURATION')
135
+ @event_duration = @master_event['DURATION'].date_interval
136
+ elsif @all_day
137
+ @event_duration = 1.day
138
+ else
139
+ @event_duration = 0.seconds
140
+ end
141
+
142
+ if @master_event.key?('RDATE')
143
+ @recur_iterator = Recur::RDateIterator.new(
144
+ @master_event['RDATE'].parts,
145
+ @start_date
146
+ )
147
+ elsif @master_event.key?('RRULE')
148
+ @recur_iterator = Recur::RRuleIterator.new(
149
+ @master_event['RRULE'].parts,
150
+ @start_date
151
+ )
152
+ else
153
+ @recur_iterator = Recur::RRuleIterator.new(
154
+ {
155
+ 'FREQ' => 'DAILY',
156
+ 'COUNT' => 1
157
+ },
158
+ @start_date
159
+ )
160
+ end
161
+
162
+ rewind
163
+
164
+ unless valid
165
+ fail Recur::NoInstancesException, 'This recurrence rule does not generate any valid instances'
166
+ end
167
+ end
168
+
169
+ # Returns the date for the current position of the iterator.
170
+ #
171
+ # @return DateTimeImmutable
172
+ def current
173
+ @current_date.clone if @current_date
174
+ end
175
+
176
+ # This method returns the start date for the current iteration of the
177
+ # event.
178
+ #
179
+ # @return DateTimeImmutable
180
+ def dt_start
181
+ @current_date.clone if @current_date
182
+ end
183
+
184
+ # This method returns the end date for the current iteration of the
185
+ # event.
186
+ #
187
+ # @return DateTimeImmutable
188
+ def dt_end
189
+ return nil unless valid
190
+
191
+ @current_date + @event_duration
192
+ end
193
+
194
+ # Returns a VEVENT for the current iterations of the event.
195
+ #
196
+ # This VEVENT will have a recurrence id, and it's DTSTART and DTEND
197
+ # altered.
198
+ #
199
+ # @return VEvent
200
+ def event_object
201
+ return @current_overridden_event if @current_overridden_event
202
+
203
+ event = @master_event.clone
204
+
205
+ event.delete('RRULE')
206
+ event.delete('EXDATE')
207
+ event.delete('RDATE')
208
+ event.delete('EXRULE')
209
+ event.delete('RECURRENCE-ID')
210
+
211
+ floating = event['DTSTART'].floating?
212
+ event['DTSTART'].date_time = dt_start
213
+ event['DTSTART'].floating = floating
214
+ if event.key?('DTEND')
215
+ floating = event['DTEND'].floating?
216
+ event['DTEND'].date_time = dt_end
217
+ event['DTEND'].floating = floating
218
+ end
219
+
220
+ # Including a RECURRENCE-ID to the object, unless this is the first
221
+ # object.
222
+ #
223
+ # The inner recurIterator is always one step ahead, this is why we're
224
+ # checking for the key being higher than 1.
225
+ if @recur_iterator.key > 1
226
+ recurid = event['DTSTART'].clone
227
+ recurid.name = 'RECURRENCE-ID'
228
+ event.add(recurid)
229
+ end
230
+ event
231
+ end
232
+
233
+ # Returns the current position of the iterator.
234
+ #
235
+ # This is for us simply a 0-based index.
236
+ #
237
+ # @return int
238
+ def key
239
+ # The counter is always 1 ahead.
240
+ @counter - 1
241
+ end
242
+
243
+ # This is called after next, to see if the iterator is still at a valid
244
+ # position, or if it's at the end.
245
+ #
246
+ # @return bool
247
+ def valid
248
+ !!@current_date
249
+ end
250
+
251
+ # Sets the iterator back to the starting point.
252
+ def rewind
253
+ @recur_iterator.rewind
254
+ # re-creating overridden event index.
255
+ index = {}
256
+ @overridden_events.each_with_index do |event, key|
257
+ stamp = event['DTSTART'].date_time(@time_zone).to_i
258
+ index[stamp] = key
259
+ end
260
+ index = index.to_a.sort { |a, b| b[0] <=> a[0] }.to_h
261
+ @counter = 0
262
+ @overridden_events_index = index
263
+ @current_overridden_event = nil
264
+
265
+ @next_date = nil
266
+ @current_date = @start_date.clone
267
+
268
+ self.next
269
+ end
270
+
271
+ # Advances the iterator with one step.
272
+ #
273
+ # @return void
274
+ def next
275
+ @current_overridden_event = nil
276
+ @counter += 1
277
+ if @next_date
278
+ # We had a stored value.
279
+ next_date = @next_date
280
+ @next_date = nil
281
+ else
282
+ # We need to ask rruleparser for the next date.
283
+ # We need to do this until we find a date that's not in the
284
+ # exception list.
285
+ loop do
286
+ unless @recur_iterator.valid
287
+ next_date = nil
288
+ break
289
+ end
290
+ next_date = @recur_iterator.current
291
+ @recur_iterator.next
292
+ break unless @exceptions.key?(next_date.to_i)
293
+ end
294
+ end
295
+
296
+ # next_date now contains what rrule thinks is the next one, but an
297
+ # overridden event may cut ahead.
298
+ if @overridden_events_index.any?
299
+ timestamp = @overridden_events_index.keys[-1]
300
+ offset = @overridden_events_index[timestamp]
301
+ if !next_date || timestamp < next_date.to_i
302
+ # Overridden event comes first.
303
+ @current_overridden_event = @overridden_events[offset]
304
+ # Putting the rrule next date aside.
305
+ @next_date = next_date
306
+ @current_date = @current_overridden_event['DTSTART'].date_time(@time_zone)
307
+
308
+ # Ensuring that this item will only be used once.
309
+ @overridden_events_index.delete(timestamp)
310
+
311
+ # Exit point!
312
+ return nil
313
+ end
314
+ end
315
+
316
+ @current_date = next_date
317
+ end
318
+
319
+ # Quickly jump to a date in the future.
320
+ #
321
+ # @param DateTimeInterface date_time
322
+ def fast_forward(date_time)
323
+ self.next while valid && dt_end < date_time
324
+ end
325
+
326
+ # Returns true if this recurring event never ends.
327
+ #
328
+ # @return bool
329
+ def infinite?
330
+ @recur_iterator.infinite?
331
+ end
332
+
333
+ # RRULE parser.
334
+ #
335
+ # @var RRuleIterator
336
+ # RUBY: attr_accessor :recur_iterator
337
+
338
+ # The duration, in seconds, of the master event.
339
+ #
340
+ # We use this to calculate the DTEND for subsequent events.
341
+ # RUBY: attr_accessor :event_duration
342
+
343
+ # A reference to the main (master) event.
344
+ #
345
+ # @var VEVENT
346
+ # RUBY: attr_accessor :master_event
347
+
348
+ # List of overridden events.
349
+ #
350
+ # @var array
351
+ # RUBY: attr_accessor :overridden_events
352
+
353
+ # Overridden event index.
354
+ #
355
+ # Key is timestamp, value is the index of the item in the overridden_event
356
+ # property.
357
+ #
358
+ # @var array
359
+ # RUBY: attr_accessor :overridden_events_index
360
+
361
+ # A list of recurrence-id's that are either part of EXDATE, or are
362
+ # overridden.
363
+ #
364
+ # @var array
365
+ # RUBY: attr_accessor :exceptions
366
+
367
+ # Internal event counter.
368
+ #
369
+ # @var int
370
+ # RUBY: attr_accessor :counter
371
+
372
+ # The very start of the iteration process.
373
+ #
374
+ # @var DateTimeImmutable
375
+ # RUBY: attr_accessor :start_date
376
+
377
+ # Where we are currently in the iteration process.
378
+ #
379
+ # @var DateTimeImmutable
380
+ # RUBY: attr_accessor :current_date
381
+
382
+ # The next date from the rrule parser.
383
+ #
384
+ # Sometimes we need to temporary store the next date, because an
385
+ # overridden event came before.
386
+ #
387
+ # @var DateTimeImmutable
388
+ # RUBY: attr_accessor :next_date
389
+
390
+ def to_a
391
+ fail 'Can not convert infinite event to array!' if infinite?
392
+
393
+ list = []
394
+ to_enum.each do |date|
395
+ list << date
396
+ end
397
+ list
398
+ end
399
+
400
+ def each
401
+ to_enum.each { |i| yield(i) }
402
+ end
403
+
404
+ def to_enum
405
+ copy = clone
406
+ copy.rewind
407
+ Enumerator.new do |yielder|
408
+ while copy.valid
409
+ yielder << copy.dt_start
410
+ copy.next
411
+ end
412
+ end
413
+ end
414
+ end
415
+ end
416
+ end
417
+ end
@@ -0,0 +1,11 @@
1
+ module Tilia
2
+ module VObject
3
+ module Recur
4
+ # This exception gets thrown when a recurrence iterator produces 0 instances.
5
+ #
6
+ # This may happen when every occurence in a rrule is also in EXDATE.
7
+ class NoInstancesException < Exception
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,138 @@
1
+ module Tilia
2
+ module VObject
3
+ module Recur
4
+ # RRuleParser.
5
+ #
6
+ # This class receives an RRULE string, and allows you to iterate to get a list
7
+ # of dates in that recurrence.
8
+ #
9
+ # For instance, passing: FREQ=DAILY;LIMIT=5 will cause the iterator to contain
10
+ # 5 items, one for each day.
11
+ class RDateIterator
12
+ include Enumerable
13
+
14
+ # Creates the Iterator.
15
+ #
16
+ # @param string|array rrule
17
+ # @param DateTimeInterface start
18
+ def initialize(rrule, start)
19
+ @counter = 0
20
+ @dates = []
21
+ @start_date = start
22
+ parse_r_date(rrule)
23
+ @current_date = @start_date.clone
24
+ end
25
+
26
+ def current
27
+ return nil unless valid
28
+ @current_date.clone
29
+ end
30
+
31
+ # Returns the current item number.
32
+ #
33
+ # @return int
34
+ def key
35
+ @counter
36
+ end
37
+
38
+ # Returns whether the current item is a valid item for the recurrence
39
+ # iterator.
40
+ #
41
+ # @return bool
42
+ def valid
43
+ @counter <= @dates.size
44
+ end
45
+
46
+ # Resets the iterator.
47
+ #
48
+ # @return void
49
+ def rewind
50
+ @current_date = @start_date.clone
51
+ @counter = 0
52
+ end
53
+
54
+ # Goes on to the next iteration.
55
+ #
56
+ # @return void
57
+ def next
58
+ @counter += 1
59
+
60
+ return nil unless valid
61
+
62
+ @current_date = DateTimeParser.parse(
63
+ @dates[@counter - 1]
64
+ )
65
+ end
66
+
67
+ # Returns true if this recurring event never ends.
68
+ #
69
+ # @return bool
70
+ def infinite?
71
+ false
72
+ end
73
+
74
+ # This method allows you to quickly go to the next occurrence after the
75
+ # specified date.
76
+ #
77
+ # @param DateTimeInterface dt
78
+ #
79
+ # @return void
80
+ def fast_forward(dt)
81
+ self.next while valid && @current_date < dt
82
+ end
83
+
84
+ # The reference start date/time for the rrule.
85
+ #
86
+ # All calculations are based on this initial date.
87
+ #
88
+ # @var DateTimeInterface
89
+ # RUBY: attr_accessor :start_date
90
+
91
+ # The date of the current iteration. You can get this by calling
92
+ # .current.
93
+ #
94
+ # @var DateTimeInterface
95
+ # RUBY: attr_accessor :protected current_date
96
+
97
+ # The current item in the list.
98
+ #
99
+ # You can get this number with the key method.
100
+ #
101
+ # @var int
102
+ # RUBY: attr_accessor :counter
103
+
104
+ protected
105
+
106
+ # This method receives a string from an RRULE property, and populates this
107
+ # class with all the values.
108
+ #
109
+ # @param string|array rrule
110
+ #
111
+ # @return void
112
+ def parse_r_date(rdate)
113
+ rdate = rdate.split(',') if rdate.is_a?(String)
114
+
115
+ @dates = rdate
116
+ end
117
+
118
+ # TODO
119
+ #
120
+ # TODO
121
+ #
122
+ # @var TODO
123
+ # RUBY: attr_accessor :dates
124
+
125
+ def each
126
+ m = [@start_date]
127
+ n = @dates.map do |d|
128
+ DateTimeParser.parse(d)
129
+ end
130
+ m.concat n
131
+ m.each do |d|
132
+ yield(d)
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end