recurrence 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- recurrence (0.1.5)
5
- activesupport (>= 3.0.0)
4
+ recurrence (1.0.0)
5
+ activesupport
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
@@ -36,7 +36,7 @@ PLATFORMS
36
36
  ruby
37
37
 
38
38
  DEPENDENCIES
39
- activesupport (>= 3.0.0)
39
+ activesupport
40
40
  recurrence!
41
41
  rspec (>= 2.0.0)
42
42
  ruby-debug19
@@ -72,3 +72,8 @@
72
72
  * Recurrence now includes Enumerable
73
73
  * The public API is now documented
74
74
  * Remove support for Rails plugin
75
+
76
+ == 1.0.1 2010-11-19
77
+
78
+ * Added :repeat option
79
+ * Added support for weekday shortcut (sun..sat)
@@ -14,6 +14,7 @@ A simple library to handle recurring events.
14
14
  # Daily
15
15
  r = Recurrence.new(:every => :day)
16
16
  r = Recurrence.new(:every => :day, :interval => 9)
17
+ r = Recurrence.new(:every => :day, :repeat => 7)
17
18
  r = Recurrence.daily(options = {})
18
19
 
19
20
  # Weekly
@@ -22,6 +23,7 @@ A simple library to handle recurring events.
22
23
  r = Recurrence.new(:every => :week, :on => [:monday, :friday])
23
24
  r = Recurrence.new(:every => :week, :on => [:monday, :wednesday, :friday])
24
25
  r = Recurrence.new(:every => :week, :on => :friday, :interval => 2)
26
+ r = Recurrence.new(:every => :week, :on => :friday, :repeat => 4)
25
27
  r = Recurrence.weekly(:on => :thursday)
26
28
 
27
29
  # Monthly by month day
@@ -30,6 +32,7 @@ A simple library to handle recurring events.
30
32
  r = Recurrence.new(:every => :month, :on => 7, :interval => 2)
31
33
  r = Recurrence.new(:every => :month, :on => 7, :interval => :monthly)
32
34
  r = Recurrence.new(:every => :month, :on => 7, :interval => :bimonthly)
35
+ r = Recurrence.new(:every => :month, :on => 7, :repeat => 6)
33
36
  r = Recurrence.monthly(:on => 31)
34
37
 
35
38
  # Monthly by week day
@@ -39,12 +42,14 @@ A simple library to handle recurring events.
39
42
  r = Recurrence.new(:every => :month, :on => :last, :weekday => :friday, :interval => 2)
40
43
  r = Recurrence.new(:every => :month, :on => :last, :weekday => :friday, :interval => :quarterly)
41
44
  r = Recurrence.new(:every => :month, :on => :last, :weekday => :friday, :interval => :semesterly)
45
+ r = Recurrence.new(:every => :month, :on => :last, :weekday => :friday, :repeat => 3)
42
46
 
43
47
  # Yearly
44
48
  r = Recurrence.new(:every => :year, :on => [7, 4]) # => [month, day]
45
49
  r = Recurrence.new(:every => :year, :on => [10, 31], :interval => 3)
46
50
  r = Recurrence.new(:every => :year, :on => [:jan, 31])
47
51
  r = Recurrence.new(:every => :year, :on => [:january, 31])
52
+ r = Recurrence.new(:every => :year, :on => [10, 31], :repeat => 3)
48
53
  r = Recurrence.yearly(:on => [:january, 31])
49
54
 
50
55
  # Limit recurrence
@@ -99,6 +104,7 @@ If you're using Rails/Bundler or something like that, remember to override the <
99
104
 
100
105
  * José Valim (http://plataformatec.com.br)
101
106
  * Maxim Chernyak (http://mediumexposure.com)
107
+ * Ricardo H. (https://github.com/ricardohsd)
102
108
 
103
109
  == License
104
110
 
@@ -3,11 +3,19 @@ module SimplesIdeias
3
3
  module Event # :nodoc: all
4
4
  class Base
5
5
  CARDINALS = %w(first second third fourth fifth)
6
- DAYS = %w(sunday monday tuesday wednesday thursday friday saturday)
6
+ WEEKDAYS = {
7
+ "sun" => 0, "sunday" => 0,
8
+ "mon" => 1, "monday" => 1,
9
+ "tue" => 2, "tuesday" => 2,
10
+ "wed" => 3, "wednesday" => 3,
11
+ "thu" => 4, "thursday" => 4,
12
+ "fri" => 5, "friday" => 5,
13
+ "sat" => 6, "saturday" => 6
14
+ }
7
15
 
8
16
  attr_accessor :start_date
9
17
 
10
- def initialize(options={})
18
+ def initialize(options = {})
11
19
  every, options = nil, every if every.kind_of?(Hash)
12
20
 
13
21
  @options = options
@@ -16,6 +24,7 @@ module SimplesIdeias
16
24
 
17
25
  validate
18
26
  raise ArgumentError, "interval should be greater than zero" if @options[:interval] <= 0
27
+ raise ArgumentError, "repeat should be greater than zero" if !@options[:repeat].nil? && @options[:repeat] <= 0
19
28
 
20
29
  prepare!
21
30
  end
@@ -72,8 +81,9 @@ module SimplesIdeias
72
81
  raise ArgumentError, "invalid day #{value}" unless (0..6).include?(value)
73
82
  value
74
83
  else
75
- raise ArgumentError, "invalid weekday #{value}" unless DAYS.include?(value.to_s)
76
- DAYS.index(value.to_s)
84
+ weekday = WEEKDAYS[value.to_s]
85
+ raise ArgumentError, "invalid weekday #{value}" unless weekday
86
+ weekday
77
87
  end
78
88
  end
79
89
  end
@@ -42,7 +42,7 @@ module SimplesIdeias
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
45
+ next_day = [@options[:on].last, Time.days_in_month(next_month, next_year)].min
46
46
 
47
47
  Date.new(next_year, next_month, next_day)
48
48
  end
@@ -69,6 +69,7 @@ module SimplesIdeias
69
69
  # Recurrence.daily(:interval => 2) #=> every 2 days
70
70
  # Recurrence.daily(:starts => 3.days.from_now)
71
71
  # Recurrence.daily(:until => 10.days.from_now)
72
+ # Recurrence.daily(:repeat => 5)
72
73
  #
73
74
  def self.daily(options = {})
74
75
  options[:every] = :day
@@ -81,6 +82,7 @@ module SimplesIdeias
81
82
  # Recurrence.weekly(:on => :saturday)
82
83
  # Recurrence.weekly(:on => [sunday, :saturday])
83
84
  # Recurrence.weekly(:on => :saturday, :interval => 2)
85
+ # Recurrence.weekly(:on => :saturday, :repeat => 5)
84
86
  #
85
87
  def self.weekly(options = {})
86
88
  options[:every] = :week
@@ -101,6 +103,12 @@ module SimplesIdeias
101
103
  # Recurrence.monthly(:on => 15, :interval => :bimonthly)
102
104
  # Recurrence.monthly(:on => 15, :interval => :quarterly)
103
105
  # Recurrence.monthly(:on => 15, :interval => :semesterly)
106
+ # Recurrence.monthly(:on => 15, :repeat => 5)
107
+ #
108
+ # The <tt>:on</tt> option can be one of the following:
109
+ #
110
+ # * :sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday
111
+ # * :sun, :mon, :tue, :wed, :thu, :fri, :sat
104
112
  #
105
113
  def self.monthly(options = {})
106
114
  options[:every] = :month
@@ -113,6 +121,7 @@ module SimplesIdeias
113
121
  # Recurrence.yearly(:on => [7, 14], :interval => 2) #=> every 2 years on Jul 14
114
122
  # Recurrence.yearly(:on => [:jan, 14], :interval => 2)
115
123
  # Recurrence.yearly(:on => [:january, 14], :interval => 2)
124
+ # Recurrence.yearly(:on => [:january, 14], :repeat => 5)
116
125
  #
117
126
  def self.yearly(options = {})
118
127
  options[:every] = :year
@@ -133,18 +142,18 @@ module SimplesIdeias
133
142
  raise ArgumentError, "invalid :every option" unless FREQUENCY.include?(options[:every].to_s)
134
143
 
135
144
  @options = options
136
- @normalized_options = initialize_dates(options.dup)
137
- @normalized_options[:interval] ||= 1
145
+ @_options = initialize_dates(options.dup)
146
+ @_options[:interval] ||= 1
138
147
 
139
- @event = case @normalized_options[:every].to_sym
148
+ @event = case @_options[:every].to_sym
140
149
  when :day
141
- Event::Daily.new(@normalized_options)
150
+ Event::Daily.new(@_options)
142
151
  when :week
143
- Event::Weekly.new(@normalized_options)
152
+ Event::Weekly.new(@_options)
144
153
  when :month
145
- Event::Monthly.new(@normalized_options)
154
+ Event::Monthly.new(@_options)
146
155
  when :year
147
- Event::Yearly.new(@normalized_options)
156
+ Event::Yearly.new(@_options)
148
157
  end
149
158
  end
150
159
 
@@ -163,7 +172,7 @@ module SimplesIdeias
163
172
  def include?(required_date)
164
173
  required_date = as_date(required_date)
165
174
 
166
- if required_date < @normalized_options[:starts] || required_date > @normalized_options[:until]
175
+ if required_date < @_options[:starts] || required_date > @_options[:until]
167
176
  false
168
177
  else
169
178
  each do |date|
@@ -213,25 +222,25 @@ module SimplesIdeias
213
222
  def events(options={})
214
223
  options[:starts] = as_date(options[:starts])
215
224
  options[:until] = as_date(options[:until])
225
+ options[:repeat] ||= @options[:repeat]
216
226
 
217
227
  reset! if options[:starts] || options[:until]
218
228
 
219
- @events ||= begin
220
- _events = []
221
-
229
+ @events ||= Array.new.tap do |list|
222
230
  loop do
223
231
  date = @event.next!
224
232
 
225
- break if date.nil?
233
+ break unless date
226
234
 
227
235
  valid_start = options[:starts].nil? || date >= options[:starts]
228
236
  valid_until = options[:until].nil? || date <= options[:until]
229
- _events << date if valid_start && valid_until
237
+ list << date if valid_start && valid_until
230
238
 
231
- break if options[:until] && options[:until] <= date
232
- end
239
+ stop_repeat = options[:repeat] && list.size == options[:repeat]
240
+ stop_until = options[:until] && options[:until] <= date
233
241
 
234
- _events
242
+ break if stop_until || stop_repeat
243
+ end
235
244
  end
236
245
  end
237
246
 
@@ -3,7 +3,7 @@ module SimplesIdeias
3
3
  module Version
4
4
  MAJOR = 1
5
5
  MINOR = 0
6
- PATCH = 0
6
+ PATCH = 1
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
8
  end
9
9
  end
@@ -13,6 +13,10 @@ describe Recurrence do
13
13
  expect { recurrence(:every => :day, :interval => 0) }.to raise_error(ArgumentError)
14
14
  end
15
15
 
16
+ it "should require :repeat to be greater than zero when provided" do
17
+ expect { recurrence(:every => :day, :repeat => 0) }.to raise_error(ArgumentError)
18
+ end
19
+
16
20
  it "should return an enumerator when Recurrence#each is called without a block" do
17
21
  recurrence(:every => :day).each.should be_instance_of(Enumerator)
18
22
  end
@@ -123,6 +127,11 @@ describe Recurrence do
123
127
  @recurrence.events[2].to_s.should == "2008-09-25"
124
128
  end
125
129
 
130
+ it "should use repeat" do
131
+ @recurrence = recurrence(:every => :day, :starts => "2008-09-21", :repeat => 10)
132
+ @recurrence.events.size.should == 10
133
+ end
134
+
126
135
  it "should have a lacking day if the interval does not match the last day" do
127
136
  @recurrence = recurrence(
128
137
  :every => :day,
@@ -192,6 +201,18 @@ describe Recurrence do
192
201
  @recurrence.events[6].to_s.should == "2008-12-14"
193
202
  end
194
203
 
204
+ it "should use repeat" do
205
+ starts = Date.parse("2008-09-21")
206
+ @recurrence = recurrence(
207
+ :every => :week,
208
+ :on => starts.wday,
209
+ :starts => starts,
210
+ :until => "2011-01-01",
211
+ :repeat => 5
212
+ )
213
+ @recurrence.events.size.should == 5
214
+ end
215
+
195
216
  it "should occur several times per week" do
196
217
  starts = Date.parse("2008-09-21") #=> sunday
197
218
  @recurrence = recurrence(
@@ -328,6 +349,18 @@ describe Recurrence do
328
349
  @recurrence.events[2].to_s.should == "2008-10-31"
329
350
  @recurrence.events[3].to_s.should == "2009-02-28"
330
351
  end
352
+
353
+ it "should use repeat" do
354
+ starts = Date.parse("2008-01-31")
355
+ @recurrence = recurrence(
356
+ :every => :month,
357
+ :on => 31,
358
+ :starts => starts,
359
+ :until => "2010-01-01",
360
+ :repeat => 5
361
+ )
362
+ @recurrence.events.size.should == 5
363
+ end
331
364
  end
332
365
 
333
366
  context "using weekday" do
@@ -336,6 +369,11 @@ describe Recurrence do
336
369
  @recurrence.events[-1].should == Date.parse("2037-12-31")
337
370
  end
338
371
 
372
+ it "should use weekday shortcut" do
373
+ @recurrence = Recurrence.daily(:on => 5, :weekday => :thu)
374
+ @recurrence.events[-1].should == Date.parse("2037-12-31")
375
+ end
376
+
339
377
  it "should repeat until 8 months from now" do
340
378
  date = 8.months.from_now
341
379
  week = (date.day - 1) / 7 + 1
@@ -408,6 +446,19 @@ describe Recurrence do
408
446
  @recurrence.events[5].to_s.should == "2009-11-15"
409
447
  @recurrence.events[6].to_s.should == "2010-01-17"
410
448
  end
449
+
450
+ it "should use repeat" do
451
+ starts = Date.parse("2009-01-01")
452
+ @recurrence = recurrence(
453
+ :every => :month,
454
+ :on => :third,
455
+ :weekday => :sunday,
456
+ :starts => starts,
457
+ :until => "2011-02-01",
458
+ :repeat => 5
459
+ )
460
+ @recurrence.events.size.should == 5
461
+ end
411
462
  end
412
463
 
413
464
  context "using interval" do
@@ -519,6 +570,18 @@ describe Recurrence do
519
570
  @recurrence.events[3].to_s.should == "2014-09-21"
520
571
  end
521
572
 
573
+ it "should use repeat" do
574
+ starts = Date.parse("2008-09-21")
575
+
576
+ @recurrence = recurrence(
577
+ :every => :year,
578
+ :on => [starts.month, starts.day],
579
+ :starts => starts,
580
+ :repeat => 5
581
+ )
582
+ @recurrence.events.size.should == 5
583
+ end
584
+
522
585
  it "should run until next available date when chosen settings are greater than start date" do
523
586
  starts = Date.parse("2008-09-03")
524
587
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-16 00:00:00 -02:00
17
+ date: 2010-11-19 00:00:00 -02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency