recurrence 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +3 -0
- data/lib/recurrence/event/base.rb +5 -0
- data/lib/recurrence/event/monthly.rb +4 -0
- data/lib/recurrence/event/yearly.rb +4 -0
- data/lib/recurrence/version.rb +1 -1
- data/spec/recurrence_spec.rb +54 -5
- metadata +12 -12
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -62,6 +62,9 @@ A simple library to handle recurring events.
|
|
62
62
|
# Override the next date handler
|
63
63
|
r = Recurrence.new(:every => :month, :on => 1, :handler => Proc.new { |day, month, year| raise("Date not allowed!") if year == 2011 && month == 12 && day == 31 })
|
64
64
|
|
65
|
+
# Shift the recurrences to maintain dates around boundaries (Jan 31 -> Feb 28 -> Mar 28)
|
66
|
+
r = Recurrence.new(:every => :month, :on => 31, :shift => true)
|
67
|
+
|
65
68
|
# Getting an array with all events
|
66
69
|
r.events.each {|date| puts date.to_s } # => Memoized array
|
67
70
|
r.events!.each {|date| puts date.to_s } # => reset items cache and re-execute it
|
@@ -36,6 +36,7 @@ module SimplesIdeias
|
|
36
36
|
@date = next_in_recurrence
|
37
37
|
|
38
38
|
@finished, @date = true, nil if @date > @options[:until]
|
39
|
+
shift_to @date if @date && @options[:shift]
|
39
40
|
@date
|
40
41
|
end
|
41
42
|
|
@@ -86,6 +87,10 @@ module SimplesIdeias
|
|
86
87
|
weekday
|
87
88
|
end
|
88
89
|
end
|
90
|
+
|
91
|
+
def shift_to(date)
|
92
|
+
# no-op
|
93
|
+
end
|
89
94
|
end
|
90
95
|
end
|
91
96
|
end
|
@@ -86,6 +86,10 @@ module SimplesIdeias
|
|
86
86
|
@options[:handler].call(date.day, date.month, date.year)
|
87
87
|
end
|
88
88
|
|
89
|
+
def shift_to(date)
|
90
|
+
@options[:on] = date.day
|
91
|
+
end
|
92
|
+
|
89
93
|
private
|
90
94
|
def valid_cardinal?(cardinal)
|
91
95
|
raise ArgumentError, "invalid cardinal #{cardinal}" unless CARDINALS.include?(cardinal.to_s)
|
@@ -46,6 +46,10 @@ module SimplesIdeias
|
|
46
46
|
@options[:handler].call(@options[:on].last, next_month, next_year)
|
47
47
|
end
|
48
48
|
|
49
|
+
def shift_to(date)
|
50
|
+
@options[:on] = [date.month, date.day]
|
51
|
+
end
|
52
|
+
|
49
53
|
private
|
50
54
|
def valid_month?(month)
|
51
55
|
raise ArgumentError, "invalid month #{month}" unless (1..12).include?(month)
|
data/lib/recurrence/version.rb
CHANGED
data/spec/recurrence_spec.rb
CHANGED
@@ -772,19 +772,68 @@ describe Recurrence do
|
|
772
772
|
end
|
773
773
|
|
774
774
|
context "with custom handlers" do
|
775
|
-
let(:exception_handler) { Proc.new { raise
|
775
|
+
let(:exception_handler) { Proc.new { raise "HANDLED" } }
|
776
776
|
let(:shift_handler) { Proc.new { |day, month, year| day += 1 if month % 2 == 0; Date.new(year, month, day) } }
|
777
777
|
|
778
|
-
it
|
779
|
-
r = recurrence(:every => :month, :on => 1, :starts =>
|
778
|
+
it "offsets every other month day" do
|
779
|
+
r = recurrence(:every => :month, :on => 1, :starts => "2011-01-01", :handler => shift_handler)
|
780
780
|
r.events[0].should == Date.new(2011, 1, 1)
|
781
781
|
r.events[1].should == Date.new(2011, 2, 2)
|
782
782
|
r.events[2].should == Date.new(2011, 3, 1)
|
783
783
|
r.events[3].should == Date.new(2011, 4, 2)
|
784
784
|
end
|
785
785
|
|
786
|
-
it
|
787
|
-
expect { recurrence(:every => :day, :handler => exception_handler) }.to raise_error(RuntimeError,
|
786
|
+
it "raises an exception from the handler" do
|
787
|
+
expect { recurrence(:every => :day, :handler => exception_handler) }.to raise_error(RuntimeError, "HANDLED")
|
788
|
+
end
|
789
|
+
end
|
790
|
+
|
791
|
+
context "with shifting enabled" do
|
792
|
+
it "shifts yearly recurrences around February 29" do
|
793
|
+
r = recurrence(:every => :year, :starts => "2012-02-29", :on => [2,29], :shift => true)
|
794
|
+
r.events[0].should == Date.new(2012, 2, 29)
|
795
|
+
r.events[1].should == Date.new(2013, 2, 28)
|
796
|
+
r.events[2].should == Date.new(2014, 2, 28)
|
797
|
+
r.events[3].should == Date.new(2015, 2, 28)
|
798
|
+
r.events[4].should == Date.new(2016, 2, 28)
|
799
|
+
end
|
800
|
+
|
801
|
+
it "shifts monthly recurrences around the 31st" do
|
802
|
+
r = recurrence(:every => :month, :starts => "2011-01-31", :on => 31, :shift => true)
|
803
|
+
r.events[0].should == Date.new(2011, 1, 31)
|
804
|
+
r.events[1].should == Date.new(2011, 2, 28)
|
805
|
+
r.events[2].should == Date.new(2011, 3, 28)
|
806
|
+
end
|
807
|
+
|
808
|
+
it "shifts monthly recurrences around the 30th" do
|
809
|
+
r = recurrence(:every => :month, :starts => "2011-01-30", :on => 30, :shift => true)
|
810
|
+
r.events[0].should == Date.new(2011, 1, 30)
|
811
|
+
r.events[1].should == Date.new(2011, 2, 28)
|
812
|
+
r.events[2].should == Date.new(2011, 3, 28)
|
813
|
+
end
|
814
|
+
|
815
|
+
it "shifts monthly recurrences around the 29th" do
|
816
|
+
r = recurrence(:every => :month, :starts => "2011-01-29", :on => 29, :shift => true)
|
817
|
+
r.events[0].should == Date.new(2011, 1, 29)
|
818
|
+
r.events[1].should == Date.new(2011, 2, 28)
|
819
|
+
r.events[2].should == Date.new(2011, 3, 28)
|
820
|
+
|
821
|
+
r = recurrence(:every => :month, :starts => "2012-01-29", :on => 29, :shift => true)
|
822
|
+
r.events[0].should == Date.new(2012, 1, 29)
|
823
|
+
r.events[1].should == Date.new(2012, 2, 29)
|
824
|
+
r.events[2].should == Date.new(2012, 3, 29)
|
825
|
+
end
|
826
|
+
|
827
|
+
it "correctly resets to original day for monthly" do
|
828
|
+
r = recurrence(:every => :month, :starts => "2011-01-31", :on => 31, :shift => true)
|
829
|
+
r.next!; r.next!
|
830
|
+
expect { r.reset! }.to change(r, :next).from(Date.new(2011, 2, 28)).to(Date.new(2011, 1, 31))
|
831
|
+
end
|
832
|
+
|
833
|
+
it "correctly resets to original month and day for yearly" do
|
834
|
+
r = recurrence(:every => :year, :starts => "2012-02-29", :on => [2,29], :shift => true)
|
835
|
+
r.next!; r.next!
|
836
|
+
expect { r.reset! }.to change(r, :next).from(Date.new(2013, 2, 28)).to(Date.new(2012, 2, 29))
|
788
837
|
end
|
789
838
|
end
|
790
839
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: recurrence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70272129285780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.3.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70272129285780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: i18n
|
27
|
-
requirement: &
|
27
|
+
requirement: &70272129283720 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70272129283720
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70272129280220 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '2.6'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70272129280220
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
|
-
requirement: &
|
49
|
+
requirement: &70272129277740 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0.9'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70272129277740
|
58
58
|
description: A simple library to handle recurring events
|
59
59
|
email:
|
60
60
|
- fnando.vieira@gmail.com
|
@@ -99,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
99
|
version: '0'
|
100
100
|
segments:
|
101
101
|
- 0
|
102
|
-
hash:
|
102
|
+
hash: 4417332307528947128
|
103
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
104
|
none: false
|
105
105
|
requirements:
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
segments:
|
110
110
|
- 0
|
111
|
-
hash:
|
111
|
+
hash: 4417332307528947128
|
112
112
|
requirements: []
|
113
113
|
rubyforge_project:
|
114
114
|
rubygems_version: 1.8.6
|