recurrence 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ class Recurrence
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 2
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -1,32 +1,60 @@
1
- # WARNING : RAKE AUTO-GENERATED FILE. DO NOT MANUALLY EDIT!
2
- # RUN : 'rake gem:update_gemspec'
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
3
5
 
4
6
  Gem::Specification.new do |s|
5
- s.homepage = "http://github.com/fnando/recurrence"
6
- s.bindir = "bin"
7
- s.name = "recurrence"
8
- s.summary = "A simple library to handle recurring events"
9
- s.add_dependency "activesupport", ">= 2.1.1"
10
- s.require_paths = ["lib"]
11
- s.files = ["init.rb",
12
- "Rakefile",
13
- "recurrence.gemspec",
14
- "History.txt",
15
- "License.txt",
16
- "README.markdown",
17
- "lib/recurrence",
18
- "lib/recurrence/base.rb",
19
- "lib/recurrence/event",
20
- "lib/recurrence/event/daily.rb",
21
- "lib/recurrence/event/monthly.rb",
22
- "lib/recurrence/event/weekly.rb",
23
- "lib/recurrence/event/yearly.rb",
24
- "lib/recurrence/event.rb",
25
- "lib/recurrence/shortcuts.rb",
26
- "lib/recurrence.rb"]
7
+ s.name = %q{recurrence}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
27
11
  s.authors = ["Nando Vieira"]
28
- s.required_rubygems_version = ">= 0"
29
- s.version = "0.1.1"
30
- s.has_rdoc = true
31
- s.email = ["fnando.vieira@gmail.com"]
32
- end
12
+ s.date = %q{2010-09-09}
13
+ s.description = %q{}
14
+ s.email = %q{fnando.vieira@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "History.txt",
20
+ "License.txt",
21
+ "README.markdown",
22
+ "Rakefile",
23
+ "init.rb",
24
+ "lib/recurrence.rb",
25
+ "lib/recurrence/event.rb",
26
+ "lib/recurrence/event/base.rb",
27
+ "lib/recurrence/event/daily.rb",
28
+ "lib/recurrence/event/monthly.rb",
29
+ "lib/recurrence/event/weekly.rb",
30
+ "lib/recurrence/event/yearly.rb",
31
+ "lib/recurrence/version.rb",
32
+ "recurrence.gemspec",
33
+ "spec/recurrence_spec.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/fnando/recurrence}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{A simple library to handle recurring events}
40
+ s.test_files = [
41
+ "spec/recurrence_spec.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
50
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
53
+ s.add_dependency(%q<activesupport>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
57
+ s.add_dependency(%q<activesupport>, [">= 0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,523 @@
1
+ require "recurrence"
2
+
3
+ Date::DATE_FORMATS[:date] = "%d/%m/%Y"
4
+ Time::DATE_FORMATS[:date] = "%d/%m/%Y"
5
+
6
+ describe "recurrence" do
7
+ it "should require :every option" do
8
+ expect { recurrence({}) }.to raise_error(ArgumentError)
9
+ end
10
+
11
+ it "should require valid :every option" do
12
+ expect { recurrence(:every => "invalid") }.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it "should require :interval to be greater than zero when provided" do
16
+ expect { recurrence(:every => :day, :interval => 0) }.to raise_error(ArgumentError)
17
+ end
18
+
19
+ Recurrence::Event::Monthly::INTERVALS.each do |interval|
20
+ it "should accept valid :interval symbol for monthly recurrence (#{interval[0]})" do
21
+ expect { recurrence(:every => :month, :on => 10, :interval => interval[0]) }.to_not raise_error(ArgumentError)
22
+ end
23
+ end
24
+
25
+ Recurrence::Event::Yearly::MONTHS.each do |month_name, month_number|
26
+ it "should accept month as symbol for yearly recurrence (#{month_name})" do
27
+ expect { recurrence(:every => :year, :on => [month_name, 10]) }.to_not raise_error(ArgumentError)
28
+ end
29
+ end
30
+
31
+ it "should require month to be a valid symbol for yearly recurrence" do
32
+ expect { recurrence(:every => :year, :on => [:invalid, 10]) }.to raise_error(ArgumentError)
33
+ end
34
+
35
+ it "should require :interval to be a valid symbol for monthly recurrence" do
36
+ expect { recurrence(:every => :month, :on => 10, :interval => :invalid) }.to raise_error(ArgumentError)
37
+ end
38
+
39
+ context "with daily recurring" do
40
+ it "should recur until limit date" do
41
+ @recurrence = Recurrence.daily
42
+ @recurrence.events[-1].should == Date.parse("2037-12-31")
43
+ end
44
+
45
+ it "should repeat until 1 month from now" do
46
+ date = 1.month.from_now
47
+ @recurrence = recurrence(:every => :day, :until => date.to_date)
48
+ @recurrence.events[-1].should == date.to_date
49
+ end
50
+
51
+ it "should start 2 months ago (#{2.months.ago.to_s(:date)})" do
52
+ date = 2.months.ago
53
+ @recurrence = recurrence(:every => :day, :starts => date.to_date)
54
+ @recurrence.events[0].should == date.to_date
55
+ @recurrence.events[1].should == (date + 1.day).to_date
56
+ @recurrence.events[2].should == (date + 2.day).to_date
57
+ end
58
+
59
+ it "should start at 2008-03-19 and repeat until 2008-04-24" do
60
+ @recurrence = recurrence(:every => :day, :starts => "2008-03-19", :until => "2008-04-24")
61
+ @recurrence.events[0].to_s.should == "2008-03-19"
62
+ @recurrence.events[1].to_s.should == "2008-03-20"
63
+ @recurrence.events[-1].to_s.should == "2008-04-24"
64
+ end
65
+
66
+ it "should use interval" do
67
+ @recurrence = recurrence(:every => :day, :interval => 2, :starts => "2008-09-21")
68
+ @recurrence.events[0].to_s.should == "2008-09-21"
69
+ @recurrence.events[1].to_s.should == "2008-09-23"
70
+ @recurrence.events[2].to_s.should == "2008-09-25"
71
+ end
72
+
73
+ it "should have a lacking day if the interval does not match the last day" do
74
+ @recurrence = recurrence(:every => :day, :starts => "2008-03-19", :until => "2008-04-25", :interval => 2)
75
+ @recurrence.events[-1].to_s.should == "2008-04-24"
76
+ end
77
+ end
78
+
79
+ context "with weekly recurring" do
80
+ it "should recur until limit date" do
81
+ @recurrence = Recurrence.weekly(:on => :thursday)
82
+ @recurrence.events[-1].should == Date.parse("2037-12-31")
83
+ end
84
+
85
+ it "should repeat 6 weeks from now" do
86
+ date = 6.weeks.from_now
87
+ @recurrence = recurrence(:every => :week, :on => date.wday, :until => date.to_date)
88
+ @recurrence.events[-1].should == date.to_date
89
+ end
90
+
91
+ it "should start 3 months ago (#{3.months.ago.to_s(:date)})" do
92
+ date = 3.months.ago
93
+
94
+ @recurrence = recurrence(:every => :week, :on => date.wday, :starts => date.to_date)
95
+ @recurrence.events[0].should == date.to_date
96
+ @recurrence.events[1].should == (date + 1.week).to_date
97
+ @recurrence.events[2].should == (date + 2.weeks).to_date
98
+ @recurrence.events[3].should == (date + 3.weeks).to_date
99
+ @recurrence.events[4].should == (date + 4.weeks).to_date
100
+ @recurrence.events[5].should == (date + 5.weeks).to_date
101
+ @recurrence.events[6].should == (date + 6.weeks).to_date
102
+ end
103
+
104
+ it "should start at 2008-02-29 and repeat until 2008-03-14" do
105
+ starts = Date.parse("2008-02-29")
106
+ ends = Date.parse("2008-03-14")
107
+
108
+ @recurrence = recurrence(:every => :week, :on => :friday, :starts => starts, :until => ends.to_date)
109
+ @recurrence.events[0].to_s.should == "2008-02-29"
110
+ @recurrence.events[1].to_s.should == "2008-03-07"
111
+ @recurrence.events[-1].to_s.should == ends.to_s
112
+ end
113
+
114
+ it "should use interval" do
115
+ starts = Date.parse("2008-09-21")
116
+ @recurrence = recurrence(:every => :week, :on => starts.wday, :interval => 2, :starts => starts, :until => "2009-01-01")
117
+ @recurrence.events[0].to_s.should == "2008-09-21"
118
+ @recurrence.events[1].to_s.should == "2008-10-05"
119
+ @recurrence.events[2].to_s.should == "2008-10-19"
120
+ @recurrence.events[3].to_s.should == "2008-11-02"
121
+ @recurrence.events[4].to_s.should == "2008-11-16"
122
+ @recurrence.events[5].to_s.should == "2008-11-30"
123
+ @recurrence.events[6].to_s.should == "2008-12-14"
124
+ end
125
+
126
+ it "should occur several times per week" do
127
+ starts = Date.parse("2008-09-21") #=> sunday
128
+ @recurrence = recurrence(:every => :week, :on => [ :saturday, :sunday ], :interval => 2, :starts => starts, :until => "2009-01-01")
129
+ @recurrence.events[0].to_s.should == "2008-09-21"
130
+ @recurrence.events[1].to_s.should == "2008-09-27"
131
+ @recurrence.events[2].to_s.should == "2008-10-05"
132
+ @recurrence.events[3].to_s.should == "2008-10-11"
133
+ @recurrence.events[4].to_s.should == "2008-10-19"
134
+ @recurrence.events[5].to_s.should == "2008-10-25"
135
+ @recurrence.events[6].to_s.should == "2008-11-02"
136
+ @recurrence.events[7].to_s.should == "2008-11-08"
137
+
138
+ starts = Date.parse("2008-09-21") #=> sunday
139
+ @recurrence = recurrence(:every => :week, :on => [ :monday, :wednesday, :friday ], :starts => starts, :until => "2009-01-01")
140
+ @recurrence.events[0].to_s.should == "2008-09-22"
141
+ @recurrence.events[1].to_s.should == "2008-09-24"
142
+ @recurrence.events[2].to_s.should == "2008-09-26"
143
+ @recurrence.events[3].to_s.should == "2008-09-29"
144
+ @recurrence.events[4].to_s.should == "2008-10-01"
145
+ @recurrence.events[5].to_s.should == "2008-10-03"
146
+ end
147
+
148
+ it "should run until next available saturday" do
149
+ starts = Date.parse("2008-09-21") # => sunday
150
+ @recurrence = recurrence(:every => :week, :on => :saturday, :starts => starts, :until => "2009-01-01")
151
+ @recurrence.events[0].to_s.should == "2008-09-27"
152
+ end
153
+ end
154
+
155
+ context "with monthly recurring" do
156
+ context "using day" do
157
+ it "should recur until limit date" do
158
+ @recurrence = Recurrence.monthly(:on => 31)
159
+ @recurrence.events[-1].should == Date.parse("2037-12-31")
160
+ end
161
+
162
+ it "should repeat until 8 months from now" do
163
+ date = 8.months.from_now
164
+ @recurrence = recurrence(:every => :month, :on => date.day, :until => date.to_date)
165
+ @recurrence.events[-1].should == date.to_date
166
+ end
167
+
168
+ it "should start 9 months ago" do
169
+ date = 9.months.ago
170
+
171
+ @recurrence = recurrence(:every => :month, :on => date.day, :starts => date.to_date)
172
+ @recurrence.events[0].should == date.to_date
173
+ end
174
+
175
+ it "should start at 2008-06-07 and repeat until 2008-11-07" do
176
+ starts = Date.parse("2008-06-07")
177
+ ends = Date.parse("2008-11-07")
178
+
179
+ @recurrence = recurrence(:every => :month, :on => starts.day, :starts => starts, :until => ends)
180
+ @recurrence.events[0].to_s.should == "2008-06-07"
181
+ @recurrence.events[-1].to_s.should == "2008-11-07"
182
+ end
183
+
184
+ it "should run until next available 27th" do
185
+ starts = Date.parse("2008-09-28")
186
+
187
+ @recurrence = recurrence(:every => :month, :on => 27, :starts => starts, :until => "2009-01-01")
188
+ @recurrence.events[0].to_s.should == "2008-10-27"
189
+ end
190
+
191
+ it "should use interval" do
192
+ starts = Date.parse("2008-01-31")
193
+ @recurrence = recurrence(:every => :month, :on => 31, :interval => 2, :starts => starts, :until => "2010-01-01")
194
+ @recurrence.events[0].to_s.should == "2008-01-31"
195
+ @recurrence.events[1].to_s.should == "2008-03-31"
196
+ @recurrence.events[2].to_s.should == "2008-05-31"
197
+ @recurrence.events[3].to_s.should == "2008-07-31"
198
+ @recurrence.events[4].to_s.should == "2008-09-30"
199
+ @recurrence.events[5].to_s.should == "2008-11-30"
200
+ @recurrence.events[6].to_s.should == "2009-01-31"
201
+
202
+ starts = Date.parse("2008-01-31")
203
+ @recurrence = recurrence(:every => :month, :on => 29, :interval => 3, :starts => starts, :until => "2010-01-01")
204
+ @recurrence.events[0].to_s.should == "2008-04-29"
205
+ @recurrence.events[1].to_s.should == "2008-07-29"
206
+ @recurrence.events[2].to_s.should == "2008-10-29"
207
+ @recurrence.events[3].to_s.should == "2009-01-29"
208
+ @recurrence.events[4].to_s.should == "2009-04-29"
209
+ @recurrence.events[5].to_s.should == "2009-07-29"
210
+
211
+ starts = Date.parse("2008-02-29")
212
+ @recurrence = recurrence(:every => :month, :on => 31, :interval => 4, :starts => starts, :until => "2010-01-01")
213
+ @recurrence.events[0].to_s.should == "2008-02-29"
214
+ @recurrence.events[1].to_s.should == "2008-06-30"
215
+ @recurrence.events[2].to_s.should == "2008-10-31"
216
+ @recurrence.events[3].to_s.should == "2009-02-28"
217
+ end
218
+ end
219
+
220
+ context "using weekday" do
221
+ it "should recur until limit date" do
222
+ @recurrence = Recurrence.daily(:on => 5, :weekday => :thursday)
223
+ @recurrence.events[-1].should == Date.parse("2037-12-31")
224
+ end
225
+
226
+ it "should repeat until 8 months from now" do
227
+ date = 8.months.from_now
228
+ week = (date.day - 1) / 7 + 1
229
+ @recurrence = recurrence(:every => :month, :on => week, :weekday => date.wday, :until => date.to_date)
230
+ @recurrence.events[-1].should == date.to_date
231
+ end
232
+
233
+ it "should start 9 months ago" do
234
+ date = 9.months.ago
235
+ week = (date.day - 1) / 7 + 1
236
+ @recurrence = recurrence(:every => :month, :on => week, :weekday => date.wday, :starts => date.to_date)
237
+ @recurrence.events[0].should == date.to_date
238
+ end
239
+
240
+ it "should start at 2008-06-07 and repeat until 2008-11-01 (first saturday)" do
241
+ starts = Date.parse("2008-06-07")
242
+ ends = Date.parse("2008-11-01")
243
+
244
+ @recurrence = recurrence(:every => :month, :on => :first, :weekday => :saturday, :starts => starts, :until => ends)
245
+ @recurrence.events[0].to_s.should == "2008-06-07"
246
+ @recurrence.events[-1].to_s.should == "2008-11-01"
247
+ end
248
+
249
+ it "should start at 2008-06-29 and repeat until 2008-11-30 (last sunday)" do
250
+ starts = Date.parse("2008-06-29")
251
+ ends = Date.parse("2008-11-30")
252
+
253
+ @recurrence = recurrence(:every => :month, :on => :last, :weekday => :sunday, :starts => starts, :until => ends)
254
+ @recurrence.events[0].to_s.should == "2008-06-29"
255
+ @recurrence.events[-1].to_s.should == "2008-11-30"
256
+ end
257
+
258
+ it "should use interval" do
259
+ starts = Date.parse("2009-01-01")
260
+ @recurrence = recurrence(:every => :month, :on => :third, :weekday => :sunday, :interval => 2, :starts => starts, :until => "2010-02-01")
261
+ @recurrence.events[0].to_s.should == "2009-01-18"
262
+ @recurrence.events[1].to_s.should == "2009-03-15"
263
+ @recurrence.events[2].to_s.should == "2009-05-17"
264
+ @recurrence.events[3].to_s.should == "2009-07-19"
265
+ @recurrence.events[4].to_s.should == "2009-09-20"
266
+ @recurrence.events[5].to_s.should == "2009-11-15"
267
+ @recurrence.events[6].to_s.should == "2010-01-17"
268
+ end
269
+ end
270
+
271
+ context "using interval" do
272
+ before(:each) do
273
+ @starts = Date.parse("2008-09-03")
274
+ end
275
+
276
+ it "should use numeric interval" do
277
+ @recurrence = recurrence(:every => :month, :on => 21, :interval => 2, :starts => @starts)
278
+ @recurrence.events[0].to_s.should == "2008-09-21"
279
+ @recurrence.events[1].to_s.should == "2008-11-21"
280
+ @recurrence.events[2].to_s.should == "2009-01-21"
281
+ @recurrence.events[3].to_s.should == "2009-03-21"
282
+ end
283
+
284
+ it "should accept monthly symbol" do
285
+ @recurrence = recurrence(:every => :month, :on => 10, :starts => @starts, :interval => :monthly)
286
+ @recurrence.events[0].to_s.should == "2008-09-10"
287
+ @recurrence.events[1].to_s.should == "2008-10-10"
288
+ end
289
+
290
+ it "should accept bimonthly symbol" do
291
+ @recurrence = recurrence(:every => :month, :on => 10, :starts => @starts, :interval => :bimonthly)
292
+ @recurrence.events[0].to_s.should == "2008-09-10"
293
+ @recurrence.events[1].to_s.should == "2008-11-10"
294
+ end
295
+
296
+ it "should accept quarterly symbol" do
297
+ @recurrence = recurrence(:every => :month, :on => 10, :starts => @starts, :interval => :quarterly)
298
+ @recurrence.events[0].to_s.should == "2008-09-10"
299
+ @recurrence.events[1].to_s.should == "2008-12-10"
300
+ end
301
+
302
+ it "should accept semesterly symbol" do
303
+ @recurrence = recurrence(:every => :month, :on => 10, :starts => @starts, :interval => :semesterly)
304
+ @recurrence.events[0].to_s.should == "2008-09-10"
305
+ @recurrence.events[1].to_s.should == "2009-03-10"
306
+ end
307
+ end
308
+ end
309
+
310
+ describe "with yearly recurring" do
311
+ it "should recur until limit date" do
312
+ @recurrence = Recurrence.yearly(:on => [12,31])
313
+ @recurrence.events[-1].should == Date.parse("2037-12-31")
314
+ end
315
+
316
+ it "should repeat until 7 years from now" do
317
+ date = 7.years.from_now
318
+ @recurrence = recurrence(:every => :year, :on => [date.month, date.day], :until => date.to_date)
319
+ @recurrence.events[-1].should == date.to_date
320
+ end
321
+
322
+ it "should start 2 years ago" do
323
+ date = 2.years.ago
324
+ @recurrence = recurrence(:every => :year, :on => [date.month, date.day], :starts => date.to_date)
325
+ @recurrence.events[0].should == date.to_date
326
+ end
327
+
328
+ it "should start at 2003-06-07 and repeat until 2018-06-07" do
329
+ starts = Date.parse("2003-06-07")
330
+ ends = Date.parse("2018-06-07")
331
+
332
+ @recurrence = recurrence(:every => :year, :on => [starts.month, starts.day], :starts => starts, :until => ends)
333
+ @recurrence.events[0].to_s.should == "2003-06-07"
334
+ @recurrence.events[-1].to_s.should == "2018-06-07"
335
+ end
336
+
337
+ it "should use interval" do
338
+ starts = Date.parse("2008-09-21")
339
+
340
+ @recurrence = recurrence(:every => :year, :on => [starts.month, starts.day], :interval => 2, :starts => starts)
341
+ @recurrence.events[0].to_s.should == "2008-09-21"
342
+ @recurrence.events[1].to_s.should == "2010-09-21"
343
+ @recurrence.events[2].to_s.should == "2012-09-21"
344
+ @recurrence.events[3].to_s.should == "2014-09-21"
345
+ end
346
+
347
+ it "should run until next available date when chosen settings are greater than start date" do
348
+ starts = Date.parse("2008-09-03")
349
+
350
+ @recurrence = recurrence(:every => :year, :on => [10, 27], :starts => starts)
351
+ @recurrence.events[0].to_s.should == "2008-10-27"
352
+ end
353
+
354
+ it "should run until next available date when chosen settings are smaller than start date" do
355
+ starts = Date.parse("2008-09-03")
356
+ @recurrence = recurrence(:every => :year, :on => [7, 1], :starts => starts)
357
+ @recurrence.events[0].to_s.should == "2009-07-01"
358
+
359
+ starts = Date.parse("2008-09-03")
360
+ @recurrence = recurrence(:every => :year, :on => [9, 1], :starts => starts)
361
+ @recurrence.events[0].to_s.should == "2009-09-01"
362
+ end
363
+ end
364
+
365
+ describe "#include?" do
366
+ it "should include date (day)" do
367
+ @recurrence = recurrence(:every => :day, :starts => "2008-09-30")
368
+ @recurrence.include?("2008-09-30").should be_true
369
+ @recurrence.include?("2008-10-01").should be_true
370
+ end
371
+
372
+ it "should include date (week)" do
373
+ @recurrence = recurrence(:every => :week, :on => :thursday, :starts => "2008-09-30")
374
+ @recurrence.include?("2008-09-30").should be_false
375
+ @recurrence.include?("2008-10-02").should be_true
376
+
377
+ @recurrence = recurrence(:every => :week, :on => :monday, :starts => "2008-09-29")
378
+ @recurrence.include?("2008-09-29").should be_true
379
+ @recurrence.include?("2008-10-06").should be_true
380
+ end
381
+
382
+ it "should include date (month)" do
383
+ @recurrence = recurrence(:every => :month, :on => 10, :starts => "2008-09-30")
384
+ @recurrence.include?("2008-09-30").should be_false
385
+ @recurrence.include?("2008-10-10").should be_true
386
+
387
+ @recurrence = recurrence(:every => :month, :on => 10, :starts => "2008-09-10")
388
+ @recurrence.include?("2008-09-10").should be_true
389
+ @recurrence.include?("2008-10-10").should be_true
390
+ end
391
+
392
+ it "should include date (year)" do
393
+ @recurrence = recurrence(:every => :year, :on => [6,28], :starts => "2008-09-30")
394
+ @recurrence.include?("2009-09-30").should be_false
395
+ @recurrence.include?("2009-06-28").should be_true
396
+
397
+ @recurrence = recurrence(:every => :year, :on => [6,28], :starts => "2008-06-28")
398
+ @recurrence.include?("2009-06-28").should be_true
399
+ @recurrence.include?("2009-06-28").should be_true
400
+ end
401
+
402
+ it "should not include date when is smaller than starting date (day)" do
403
+ @recurrence = recurrence(:every => :day, :starts => "2008-09-30")
404
+ @recurrence.include?("2008-09-29").should be_false
405
+ end
406
+
407
+ it "should not include date when is smaller than starting date (week)" do
408
+ @recurrence = recurrence(:every => :week, :on => :friday, :starts => "2008-09-30")
409
+ @recurrence.include?("2008-09-24").should be_false
410
+ end
411
+
412
+ it "should not include date when is smaller than starting date (month)" do
413
+ @recurrence = recurrence(:every => :month, :on => 10, :starts => "2008-09-30")
414
+ @recurrence.include?("2008-09-10").should be_false
415
+ end
416
+
417
+ it "should not include date when is smaller than starting date (year)" do
418
+ @recurrence = recurrence(:every => :year, :on => [6,28], :starts => "2008-09-30")
419
+ @recurrence.include?("2008-06-28").should be_false
420
+ end
421
+
422
+ it "should not include date when is greater than ending date (day)" do
423
+ @recurrence = recurrence(:every => :day, :until => "2008-09-30")
424
+ @recurrence.include?("2008-10-01").should be_false
425
+ end
426
+
427
+ it "should not include date when is greater than ending date (week)" do
428
+ @recurrence = recurrence(:every => :week, :on => :friday, :until => "2008-09-30")
429
+ @recurrence.include?("2008-10-03").should be_false
430
+ end
431
+
432
+ it "should not include date when is greater than ending date (year)" do
433
+ @recurrence = recurrence(:every => :year, :on => [6,28], :until => "2008-09-30")
434
+ @recurrence.include?("2009-06-28").should be_false
435
+ end
436
+ end
437
+
438
+ describe "#next" do
439
+ it "should return next date" do
440
+ @recurrence = recurrence(:every => :day)
441
+
442
+ @recurrence.next.to_s.should == Date.today.to_s
443
+ @recurrence.next.to_s.should == Date.today.to_s
444
+ end
445
+
446
+ it "should return next! date" do
447
+ @recurrence = recurrence(:every => :day)
448
+
449
+ @recurrence.next!.to_s.should == Date.today.to_s
450
+ @recurrence.next!.to_s.should == 1.day.from_now.to_date.to_s
451
+ @recurrence.next!.to_s.should == 2.days.from_now.to_date.to_s
452
+ @recurrence.next!.to_s.should == 3.days.from_now.to_date.to_s
453
+ end
454
+ end
455
+
456
+ describe "#reset!" do
457
+ it "should reset to the first available date" do
458
+ @recurrence = recurrence(:every => :year, :on => [2, 31], :starts => "2008-01-01")
459
+ @recurrence.next!.to_s.should == "2008-02-29"
460
+ @recurrence.next!.to_s.should == "2009-02-28"
461
+ @recurrence.reset!
462
+ @recurrence.next.to_s.should == "2008-02-29"
463
+ end
464
+ end
465
+
466
+ describe "event initialization" do
467
+ it "should return the first available date" do
468
+ @recurrence = recurrence(:every => :year, :on => [2, 31], :starts => "2008-01-01")
469
+ @recurrence.next!.to_s.should == "2008-02-29"
470
+ @recurrence.next!.to_s.should == "2009-02-28"
471
+ @recurrence.next!.to_s.should == "2010-02-28"
472
+ @recurrence.next!.to_s.should == "2011-02-28"
473
+ @recurrence.next!.to_s.should == "2012-02-29"
474
+ end
475
+ end
476
+
477
+ context "when generating events" do
478
+ before(:each) do
479
+ @recurrence = recurrence(:every => :day, :starts => "2009-01-06", :until => "2009-01-15")
480
+ end
481
+
482
+ it "should return starting and ending recurrences" do
483
+ @recurrence.events[0].to_s.should == "2009-01-06"
484
+ @recurrence.events[-1].to_s.should == "2009-01-15"
485
+ end
486
+
487
+ it "should reset cache" do
488
+ @recurrence.event.should_receive(:reset!).exactly(3).times
489
+ @recurrence.events(:starts => "2009-01-11")
490
+ @recurrence.events(:until => "2009-01-14")
491
+ @recurrence.events(:starts => "2009-01-11", :until => "2009-01-14")
492
+ end
493
+
494
+ it "should return only events greater than starting date" do
495
+ @events = @recurrence.events(:starts => "2009-01-10")
496
+ @events[0].to_s.should == "2009-01-10"
497
+ end
498
+
499
+ it "should return only events smaller than until date" do
500
+ @events = @recurrence.events(:until => "2009-01-10")
501
+ @events[0].to_s.should == "2009-01-06"
502
+ @events[-1].to_s.should == "2009-01-10"
503
+ end
504
+
505
+ it "should return only events between starting and until date" do
506
+ @events = @recurrence.events(:starts => "2009-01-12", :until => "2009-01-14")
507
+ @events[0].to_s.should == "2009-01-12"
508
+ @events[-1].to_s.should == "2009-01-14"
509
+ end
510
+
511
+ it "should not iterate all dates when using until" do
512
+ @events = @recurrence.events(:starts => "2009-01-06", :until => "2009-01-08")
513
+ @recurrence.instance_variable_get("@events").size.should == 3
514
+ @events.size.should == 3
515
+ @events[-1].to_s.should == "2009-01-08"
516
+ end
517
+ end
518
+
519
+ private
520
+ def recurrence(options)
521
+ Recurrence.new(options)
522
+ end
523
+ end