recurrence 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- recurrence (1.1.0)
4
+ recurrence (1.2.0)
5
5
  activesupport (>= 2.3.0)
6
6
  i18n
7
7
 
@@ -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)
@@ -2,7 +2,7 @@ module SimplesIdeias
2
2
  class Recurrence
3
3
  module Version
4
4
  MAJOR = 1
5
- MINOR = 1
5
+ MINOR = 2
6
6
  PATCH = 0
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
8
  end
@@ -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 'HANDLED' } }
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 'offsets every other month day' do
779
- r = recurrence(:every => :month, :on => 1, :starts => '2011-01-01', :handler => shift_handler)
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 'raises an exception from the handler' do
787
- expect { recurrence(:every => :day, :handler => exception_handler) }.to raise_error(RuntimeError, 'HANDLED')
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.1.0
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-05 00:00:00.000000000 Z
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: &70129728758960 !ruby/object:Gem::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: *70129728758960
24
+ version_requirements: *70272129285780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &70129728757820 !ruby/object:Gem::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: *70129728757820
35
+ version_requirements: *70272129283720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70129728756940 !ruby/object:Gem::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: *70129728756940
46
+ version_requirements: *70272129280220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70129728756300 !ruby/object:Gem::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: *70129728756300
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: 2739782105165041222
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: 2739782105165041222
111
+ hash: 4417332307528947128
112
112
  requirements: []
113
113
  rubyforge_project:
114
114
  rubygems_version: 1.8.6