recurring 0.4.5 → 0.5.1
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.
- data/lib/schedule.rb +27 -8
- data/spec/schedule_spec.rb +38 -3
- metadata +4 -3
data/lib/schedule.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Recurring
|
2
|
-
|
2
|
+
|
3
|
+
VERSION = '0.5.1'
|
3
4
|
|
4
5
|
class << self
|
5
6
|
# returns a number starting with 1
|
@@ -7,6 +8,12 @@ module Recurring
|
|
7
8
|
(((date.day - 1).to_f / 7.0) + 1).floor
|
8
9
|
end
|
9
10
|
|
11
|
+
def negative_week_in_month date
|
12
|
+
end_of_month = (date.month < 12 ? Time.utc(date.year, date.month+1) : Time.utc(date.year + 1)) - 3600
|
13
|
+
|
14
|
+
(((end_of_month.day - date.day).to_f / 7.0) + 1).floor * -1
|
15
|
+
end
|
16
|
+
|
10
17
|
# just a wrapper for strftime
|
11
18
|
def week_of_year date
|
12
19
|
date.strftime('%U').to_i
|
@@ -136,15 +143,27 @@ module Recurring
|
|
136
143
|
# <tt>range = (Time.now..Time.now+24*60*60)</tt>
|
137
144
|
#
|
138
145
|
# <tt>rs.find_in_range(range)</tt>
|
139
|
-
def find_in_range *
|
140
|
-
|
141
|
-
|
146
|
+
def find_in_range *args
|
147
|
+
if args[0].respond_to?(:first) && args[0].respond_to?(:last)
|
148
|
+
t_start = args[0].first
|
149
|
+
t_end = args[0].last
|
150
|
+
else
|
151
|
+
t_start = args[0]
|
152
|
+
t_end = args[1]
|
153
|
+
end
|
154
|
+
opts = args.last if args.last.respond_to?(:keys)
|
155
|
+
if opts
|
156
|
+
limit = opts[:limit]
|
157
|
+
end
|
142
158
|
result = []
|
159
|
+
count = 1
|
143
160
|
loop do
|
144
|
-
rnext = find_next
|
145
|
-
break if
|
161
|
+
rnext = find_next t_start
|
162
|
+
break if count > limit if limit
|
163
|
+
break if rnext > t_end
|
146
164
|
result << rnext
|
147
|
-
|
165
|
+
t_start = rnext + 1
|
166
|
+
count += 1
|
148
167
|
end
|
149
168
|
result
|
150
169
|
end
|
@@ -284,7 +303,7 @@ module Recurring
|
|
284
303
|
return ((Recurring.week_of_year(date) - Recurring.week_of_year(@anchor)) % @frequency) == 0
|
285
304
|
end
|
286
305
|
if @weeks
|
287
|
-
@weeks.include?(Recurring.week_in_month(date))
|
306
|
+
@weeks.include?(Recurring.week_in_month(date)) || @weeks.include?(Recurring.negative_week_in_month(date))
|
288
307
|
else
|
289
308
|
true
|
290
309
|
end
|
data/spec/schedule_spec.rb
CHANGED
@@ -343,6 +343,26 @@ end
|
|
343
343
|
context "A six-monthly schedule with week, weekday, and times params" do
|
344
344
|
end
|
345
345
|
|
346
|
+
context "A monthly schedule with negative week params and weekday params" do
|
347
|
+
|
348
|
+
setup do
|
349
|
+
@rs = Recurring::Schedule.new :unit => 'months', :weeks => -1,
|
350
|
+
:weekdays => :monday
|
351
|
+
end
|
352
|
+
|
353
|
+
specify "should include the beginnings of matching days" do
|
354
|
+
@rs.should_include Time.utc(2006,12,25)
|
355
|
+
@rs.should_include Time.utc(2007,4,30)
|
356
|
+
end
|
357
|
+
|
358
|
+
specify "should not include the matching days in off weeks" do
|
359
|
+
@rs.should_not_include Time.utc(2006,12,18)
|
360
|
+
@rs.should_not_include Time.utc(2006,12,11)
|
361
|
+
@rs.should_not_include Time.utc(2006,12,4)
|
362
|
+
end
|
363
|
+
|
364
|
+
end
|
365
|
+
|
346
366
|
#WEEKLY
|
347
367
|
|
348
368
|
context "A bi-weekly schedule with weekdays and a midnight time" do
|
@@ -734,8 +754,13 @@ context "Every 45 minutes from an anchor" do
|
|
734
754
|
end
|
735
755
|
end
|
736
756
|
|
737
|
-
context "A minutely schedule
|
738
|
-
|
757
|
+
context "A 30 minutely schedule" do
|
758
|
+
setup do
|
759
|
+
@rs = Recurring::Schedule.new :unit => 'minutes', :frequency => 30, :anchor => Time.utc(2006,1,1,1,15)
|
760
|
+
end
|
761
|
+
specify "should match the very next time after the start" do
|
762
|
+
@rs.find_in_range(Time.utc(2006,1,15,2), Time.utc(2006,1,16)).first.should == Time.utc(2006,1,15,2,15)
|
763
|
+
end
|
739
764
|
end
|
740
765
|
|
741
766
|
context "A 5-minutely schedule with no other params" do
|
@@ -764,7 +789,17 @@ context "A 5-minutely schedule with no other params" do
|
|
764
789
|
|
765
790
|
specify "should find 7 times in a 30 minute range" do
|
766
791
|
should_find_in_range(@rs, 7, Time.utc(2006,12,12,0,45), Time.utc(2006,12,12,1,15))
|
767
|
-
@rs.find_in_range( Time.utc(2006,12,12,0,45)
|
792
|
+
@rs.find_in_range( Time.utc(2006,12,12,0,45)..Time.utc(2006,12,12,1,15) ).first.class.should_be Time
|
793
|
+
end
|
794
|
+
|
795
|
+
specify "should find 3 times in a 30 minute range with a limit of 3" do
|
796
|
+
results = @rs.find_in_range(Time.utc(2006,12,12,0,45), Time.utc(2006,12,12,1,15), :limit => 3)
|
797
|
+
results.length.should == 3
|
798
|
+
end
|
799
|
+
|
800
|
+
specify "should find 3 times in a 30 minute range (with range args) with a limit of 3" do
|
801
|
+
results = @rs.find_in_range(Time.utc(2006,12,12,0,45)..Time.utc(2006,12,12,1,15), :limit => 3)
|
802
|
+
results.length.should == 3
|
768
803
|
end
|
769
804
|
|
770
805
|
specify "should find in a range given as an object with first and last params" do
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: recurring
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.5.1
|
7
|
+
date: 2007-04-25 00:00:00 -07:00
|
8
8
|
summary: A scheduling library for recurring events
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
25
25
|
platform: ruby
|
26
26
|
signing_key:
|
27
27
|
cert_chain:
|
28
|
+
post_install_message:
|
28
29
|
authors:
|
29
30
|
- Chris Anderson
|
30
31
|
files:
|