recurrence 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- recurrence (1.0.3)
4
+ recurrence (1.1.0)
5
5
  activesupport (>= 2.3.0)
6
+ i18n
6
7
 
7
8
  GEM
8
9
  remote: http://rubygems.org/
9
10
  specs:
10
- activesupport (3.1.1)
11
- multi_json (~> 1.0)
12
- diff-lcs (1.1.3)
13
- multi_json (1.0.3)
14
- rake (0.9.2.2)
11
+ activesupport (3.0.9)
12
+ diff-lcs (1.1.2)
13
+ i18n (0.6.0)
14
+ rake (0.9.2)
15
15
  rspec (2.6.0)
16
16
  rspec-core (~> 2.6.0)
17
17
  rspec-expectations (~> 2.6.0)
@@ -25,6 +25,6 @@ PLATFORMS
25
25
  ruby
26
26
 
27
27
  DEPENDENCIES
28
- rake
28
+ rake (~> 0.9)
29
29
  recurrence!
30
30
  rspec (~> 2.6)
@@ -81,7 +81,3 @@
81
81
  == 1.0.2 2011-02-09
82
82
 
83
83
  * Added Ruby 1.8 support
84
-
85
- == 1.0.3 2011-19-14
86
-
87
- * Added support for default ending date through Recurrence.default_until_date
@@ -59,6 +59,9 @@ A simple library to handle recurring events.
59
59
  r = Recurrence.new(:every => :day, :until => '2010-01-31')
60
60
  r = Recurrence.new(:every => :day, :starts => Date.today, :until => '2010-01-31')
61
61
 
62
+ # Override the next date handler
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
+
62
65
  # Getting an array with all events
63
66
  r.events.each {|date| puts date.to_s } # => Memoized array
64
67
  r.events!.each {|date| puts date.to_s } # => reset items cache and re-execute it
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ Bundler::GemHelper.install_tasks
4
4
  require "rspec/core/rake_task"
5
5
  RSpec::Core::RakeTask.new
6
6
 
7
- require "rake/rdoctask"
7
+ require "rdoc/task"
8
8
  Rake::RDocTask.new do |rd|
9
9
  rd.main = "README.rdoc"
10
10
  rd.rdoc_files.include("README.rdoc", "lib/**/*.rb", "History.txt", "License.txt")
@@ -6,7 +6,7 @@ module SimplesIdeias
6
6
  def next_in_recurrence
7
7
  date = @date.to_date
8
8
  date += @options[:interval] if initialized?
9
- date
9
+ @options[:handler].call(date.day, date.month, date.year)
10
10
  end
11
11
  end
12
12
  end
@@ -59,9 +59,8 @@ module SimplesIdeias
59
59
 
60
60
  next_year = date.year + raw_month / 12
61
61
  next_month = (raw_month % 12) + 1 # change back to ruby interval
62
- next_day = [ @options[:on], Time.days_in_month(next_month, next_year) ].min
63
62
 
64
- Date.new(next_year, next_month, next_day)
63
+ @options[:handler].call(@options[:on], next_month, next_year)
65
64
  end
66
65
 
67
66
  def advance_to_month_by_weekday(date, interval=@options[:interval])
@@ -84,7 +83,7 @@ module SimplesIdeias
84
83
  date -= weeks * 7
85
84
  end
86
85
 
87
- date
86
+ @options[:handler].call(date.day, date.month, date.year)
88
87
  end
89
88
 
90
89
  private
@@ -22,7 +22,8 @@ module SimplesIdeias
22
22
  to_add += @options[:on].first # Go to first required day
23
23
  end
24
24
 
25
- @date.to_date + to_add
25
+ new_date = @date.to_date + to_add
26
+ @options[:handler].call(new_date.day, new_date.month, new_date.year)
26
27
  end
27
28
  end
28
29
  end
@@ -35,16 +35,15 @@ module SimplesIdeias
35
35
  else
36
36
  new_date = advance_to_year(@date, 0)
37
37
  new_date = advance_to_year(new_date) if @date > new_date
38
- new_date
38
+ @options[:handler].call(new_date.day, new_date.month, new_date.year)
39
39
  end
40
40
  end
41
41
 
42
42
  def advance_to_year(date, interval=@options[:interval])
43
43
  next_year = date.year + interval
44
44
  next_month = @options[:on].first
45
- next_day = [@options[:on].last, Time.days_in_month(next_month, next_year)].min
46
45
 
47
- Date.new(next_year, next_month, next_day)
46
+ @options[:handler].call(@options[:on].last, next_month, next_year)
48
47
  end
49
48
 
50
49
  private
@@ -0,0 +1,7 @@
1
+ module SimplesIdeias
2
+ class Recurrence
3
+ module Handler # :nodoc: all
4
+ autoload :FallBack, 'recurrence/handler/fall_back'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ module SimplesIdeias
2
+ class Recurrence
3
+ module Handler # :nodoc: all
4
+ ##
5
+ # If the given date is invalid - beyond the end of the month - then this
6
+ # will move the day back to the last day of the given month.
7
+ #
8
+ # Examples
9
+ #
10
+ # FallBack.call(1, 1, 2011)
11
+ # # => January 1, 2011
12
+ #
13
+ # FallBack.call(31, 2, 2011)
14
+ # # => February 28, 2011
15
+ #
16
+ module FallBack
17
+ def self.call(day, month, year)
18
+ Date.new(year, month, [day, Time.days_in_month(month, year)].min)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -5,6 +5,7 @@ require "enumerator"
5
5
  module SimplesIdeias
6
6
  class Recurrence
7
7
  autoload :Event, "recurrence/event"
8
+ autoload :Handler, "recurrence/handler"
8
9
  autoload :Version, "recurrence/version"
9
10
 
10
11
  include Enumerable
@@ -14,15 +15,6 @@ module SimplesIdeias
14
15
  FREQUENCY = %w(day week month year)
15
16
 
16
17
  attr_reader :event, :options
17
-
18
- def self.as_date(date) # :nodoc:
19
- case date
20
- when String
21
- Date.parse(date)
22
- else
23
- date
24
- end
25
- end
26
18
 
27
19
  # Return the default starting date.
28
20
  #
@@ -153,6 +145,7 @@ module SimplesIdeias
153
145
  @options = options
154
146
  @_options = initialize_dates(options.dup)
155
147
  @_options[:interval] ||= 1
148
+ @_options[:handler] ||= Handler::FallBack
156
149
 
157
150
  @event = case @_options[:every].to_sym
158
151
  when :day
@@ -300,7 +293,12 @@ module SimplesIdeias
300
293
  end
301
294
 
302
295
  def as_date(date) # :nodoc:
303
- self.class.as_date(date)
296
+ case date
297
+ when String
298
+ Date.parse(date)
299
+ else
300
+ date
301
+ end
304
302
  end
305
303
  end
306
304
  end
@@ -2,8 +2,8 @@ module SimplesIdeias
2
2
  class Recurrence
3
3
  module Version
4
4
  MAJOR = 1
5
- MINOR = 0
6
- PATCH = 3
5
+ MINOR = 1
6
+ PATCH = 0
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
8
  end
9
9
  end
@@ -1,10 +1,10 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "recurrence"
3
+ require 'recurrence/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "recurrence"
7
- s.version = Recurrence::Version::STRING
7
+ s.version = SimplesIdeias::Recurrence::Version::STRING
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Nando Vieira"]
10
10
  s.email = ["fnando.vieira@gmail.com"]
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ["lib"]
19
19
 
20
20
  s.add_dependency "activesupport", ">= 2.3.0"
21
+ s.add_dependency "i18n"
21
22
  s.add_development_dependency "rspec", "~> 2.6"
22
- s.add_development_dependency "rake"
23
+ s.add_development_dependency "rake", "~> 0.9"
23
24
  end
@@ -94,51 +94,6 @@ describe Recurrence do
94
94
  end
95
95
  end
96
96
  end
97
-
98
- describe ".default_until_date" do
99
- after(:each){ Recurrence.default_until_date = Date.new(2037, 12, 31)}
100
-
101
- it "should return 2037-12-31 by default" do
102
- Recurrence.default_until_date.should == Date.new(2037, 12, 31)
103
- end
104
-
105
- it "should return new until when changed" do
106
- Recurrence.default_until_date = Date.new(2057, 12, 31)
107
- Recurrence.default_until_date.should == Date.new(2057, 12, 31)
108
- end
109
-
110
- it "should alter event end with new default until" do
111
- @recurrence = Recurrence.daily
112
- @recurrence.events[-1].should == Date.parse("2037-12-31")
113
-
114
- Recurrence.default_until_date = Date.new(2057, 12, 31)
115
-
116
- @recurrence = Recurrence.daily
117
- @recurrence.events[-1].should == Date.parse("2057-12-31")
118
- end
119
-
120
- it "should not return any events when default until is past" do
121
- Recurrence.default_until_date = Date.new(2007, 12, 31)
122
- @recurrence = Recurrence.daily
123
- @recurrence.events.length.should == 0
124
- end
125
-
126
- it "should allow value to be changed multiple times" do
127
- Recurrence.default_until_date = Date.new(2057, 12, 31)
128
- Recurrence.default_until_date.should == Date.new(2057, 12, 31)
129
- @recurrence1 = Recurrence.monthly(:on => 2)
130
-
131
- Recurrence.default_until_date = Date.new(2187, 12, 31)
132
- Recurrence.default_until_date.should == Date.new(2187, 12, 31)
133
- @recurrence2 = Recurrence.monthly(:on => 2)
134
- @recurrence2.events.length.should == @recurrence1.events.length + 130 * 12
135
-
136
- Recurrence.default_until_date = Date.new(2257, 12, 31)
137
- Recurrence.default_until_date.should == Date.new(2257, 12, 31)
138
- @recurrence3 = Recurrence.monthly(:on => 2)
139
- @recurrence3.events.length.should == @recurrence2.events.length + 70 * 12
140
- end
141
- end
142
97
 
143
98
  context "with daily recurring" do
144
99
  it "should recur until limit date" do
@@ -816,6 +771,23 @@ describe Recurrence do
816
771
  end
817
772
  end
818
773
 
774
+ context "with custom handlers" do
775
+ let(:exception_handler) { Proc.new { raise 'HANDLED' } }
776
+ let(:shift_handler) { Proc.new { |day, month, year| day += 1 if month % 2 == 0; Date.new(year, month, day) } }
777
+
778
+ it 'offsets every other month day' do
779
+ r = recurrence(:every => :month, :on => 1, :starts => '2011-01-01', :handler => shift_handler)
780
+ r.events[0].should == Date.new(2011, 1, 1)
781
+ r.events[1].should == Date.new(2011, 2, 2)
782
+ r.events[2].should == Date.new(2011, 3, 1)
783
+ r.events[3].should == Date.new(2011, 4, 2)
784
+ end
785
+
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
+
819
791
  private
820
792
  def recurrence(options)
821
793
  Recurrence.new(options)
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.0.3
4
+ version: 1.1.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-10-14 00:00:00.000000000 Z
12
+ date: 2011-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70267646596360 !ruby/object:Gem::Requirement
16
+ requirement: &70129728758960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: 2.3.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70267646596360
24
+ version_requirements: *70129728758960
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &70129728757820 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70129728757820
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rspec
27
- requirement: &70267646595780 !ruby/object:Gem::Requirement
38
+ requirement: &70129728756940 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
@@ -32,18 +43,18 @@ dependencies:
32
43
  version: '2.6'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70267646595780
46
+ version_requirements: *70129728756940
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rake
38
- requirement: &70267646595300 !ruby/object:Gem::Requirement
49
+ requirement: &70129728756300 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
- - - ! '>='
52
+ - - ~>
42
53
  - !ruby/object:Gem::Version
43
- version: '0'
54
+ version: '0.9'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70267646595300
57
+ version_requirements: *70129728756300
47
58
  description: A simple library to handle recurring events
48
59
  email:
49
60
  - fnando.vieira@gmail.com
@@ -67,6 +78,8 @@ files:
67
78
  - lib/recurrence/event/monthly.rb
68
79
  - lib/recurrence/event/weekly.rb
69
80
  - lib/recurrence/event/yearly.rb
81
+ - lib/recurrence/handler.rb
82
+ - lib/recurrence/handler/fall_back.rb
70
83
  - lib/recurrence/namespace.rb
71
84
  - lib/recurrence/version.rb
72
85
  - recurrence.gemspec
@@ -86,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
99
  version: '0'
87
100
  segments:
88
101
  - 0
89
- hash: -4581356905210184239
102
+ hash: 2739782105165041222
90
103
  required_rubygems_version: !ruby/object:Gem::Requirement
91
104
  none: false
92
105
  requirements:
@@ -95,10 +108,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
108
  version: '0'
96
109
  segments:
97
110
  - 0
98
- hash: -4581356905210184239
111
+ hash: 2739782105165041222
99
112
  requirements: []
100
113
  rubyforge_project:
101
- rubygems_version: 1.8.10
114
+ rubygems_version: 1.8.6
102
115
  signing_key:
103
116
  specification_version: 3
104
117
  summary: A simple library to handle recurring events