recurrence 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +9 -0
- data/lib/recurrence/namespace.rb +31 -10
- data/lib/recurrence/version.rb +1 -1
- data/spec/recurrence_spec.rb +45 -0
- metadata +3 -3
data/History.txt
CHANGED
@@ -56,3 +56,12 @@
|
|
56
56
|
== 0.1.3 2010-09-14
|
57
57
|
|
58
58
|
* Moved Recurrence class to SimpleIdeias namespace; see troubleshooting section for instructions.
|
59
|
+
|
60
|
+
== 0.1.4 2010-10-19
|
61
|
+
|
62
|
+
* Bug fix: yearly recurrence was raising an error
|
63
|
+
|
64
|
+
== 0.1.5 2010-11-03
|
65
|
+
|
66
|
+
* Recurrence.default_starts_date is now configurable; pass in a String or block
|
67
|
+
* Add #options reader so we'll be able to use Recurrence with ActiveRecord.composed_of
|
data/lib/recurrence/namespace.rb
CHANGED
@@ -8,7 +8,27 @@ module SimplesIdeias
|
|
8
8
|
|
9
9
|
FREQUENCY = %w(day week month year)
|
10
10
|
|
11
|
-
attr_reader :event
|
11
|
+
attr_reader :event, :options
|
12
|
+
|
13
|
+
def self.default_starts_date
|
14
|
+
if @default_starts_date
|
15
|
+
if @default_starts_date.is_a?(String)
|
16
|
+
eval(@default_starts_date)
|
17
|
+
else
|
18
|
+
@default_starts_date.call
|
19
|
+
end
|
20
|
+
else
|
21
|
+
Date.today
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.default_starts_date=(date)
|
26
|
+
unless date.respond_to?(:call) || date.is_a?(String) || date == nil
|
27
|
+
raise ArgumentError, 'default_starts_date must be a proc or an evaluatable string such as "Date.current"'
|
28
|
+
end
|
29
|
+
|
30
|
+
@default_starts_date = date
|
31
|
+
end
|
12
32
|
|
13
33
|
def self.default_until_date
|
14
34
|
@default_until_date ||= Date.new(2037, 12, 31)
|
@@ -46,18 +66,19 @@ module SimplesIdeias
|
|
46
66
|
raise ArgumentError, ":every option is required" unless options.key?(:every)
|
47
67
|
raise ArgumentError, "invalid :every option" unless FREQUENCY.include?(options[:every].to_s)
|
48
68
|
|
49
|
-
@options =
|
50
|
-
@
|
69
|
+
@options = options.dup
|
70
|
+
@normalized_options = initialize_dates(options)
|
71
|
+
@normalized_options[:interval] ||= 1
|
51
72
|
|
52
|
-
@event = case @
|
73
|
+
@event = case @normalized_options[:every].to_sym
|
53
74
|
when :day
|
54
|
-
Recurrence::Event::Daily.new(@
|
75
|
+
Recurrence::Event::Daily.new(@normalized_options)
|
55
76
|
when :week
|
56
|
-
Recurrence::Event::Weekly.new(@
|
77
|
+
Recurrence::Event::Weekly.new(@normalized_options)
|
57
78
|
when :month
|
58
|
-
Recurrence::Event::Monthly.new(@
|
79
|
+
Recurrence::Event::Monthly.new(@normalized_options)
|
59
80
|
when :year
|
60
|
-
Recurrence::Event::Yearly.new(@
|
81
|
+
Recurrence::Event::Yearly.new(@normalized_options)
|
61
82
|
end
|
62
83
|
end
|
63
84
|
|
@@ -69,7 +90,7 @@ module SimplesIdeias
|
|
69
90
|
def include?(required_date)
|
70
91
|
required_date = Date.parse(required_date) if required_date.is_a?(String)
|
71
92
|
|
72
|
-
if required_date < @
|
93
|
+
if required_date < @normalized_options[:starts] || required_date > @normalized_options[:until]
|
73
94
|
false
|
74
95
|
else
|
75
96
|
each do |date|
|
@@ -135,7 +156,7 @@ module SimplesIdeias
|
|
135
156
|
options[name] = Date.parse(options[name]) if options[name].is_a?(String)
|
136
157
|
end
|
137
158
|
|
138
|
-
options[:starts] ||=
|
159
|
+
options[:starts] ||= self.class.default_starts_date
|
139
160
|
options[:until] ||= self.class.default_until_date
|
140
161
|
|
141
162
|
options
|
data/lib/recurrence/version.rb
CHANGED
data/spec/recurrence_spec.rb
CHANGED
@@ -33,6 +33,44 @@ describe "recurrence" do
|
|
33
33
|
expect { recurrence(:every => :month, :on => 10, :interval => :invalid) }.to raise_error(ArgumentError)
|
34
34
|
end
|
35
35
|
|
36
|
+
describe '.default_starts_date' do
|
37
|
+
it 'should return Date.today by default' do
|
38
|
+
Recurrence.default_starts_date.should == Date.today
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should require only strings and procs' do
|
42
|
+
expect { Recurrence.default_starts_date = Date.tomorrow }.to raise_error(ArgumentError)
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when .default_starts_date is reassigned to "Date.tomorrow" string' do
|
46
|
+
before { Recurrence.default_starts_date = 'Date.tomorrow' }
|
47
|
+
after { Recurrence.default_starts_date = nil }
|
48
|
+
|
49
|
+
it 'should return Date.tomorrow' do
|
50
|
+
Recurrence.default_starts_date.should == Date.tomorrow
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should have effect on generated events' do
|
54
|
+
r = Recurrence.new(:every => :day, :until => 3.days.from_now.to_date)
|
55
|
+
r.events.first.should == Date.tomorrow
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when .default_starts_date is reassigned to lambda { Date.tomorrow } proc' do
|
60
|
+
before { Recurrence.default_starts_date = lambda { Date.tomorrow } }
|
61
|
+
after { Recurrence.default_starts_date = nil }
|
62
|
+
|
63
|
+
it 'should return Date.tomorrow' do
|
64
|
+
Recurrence.default_starts_date.should == Date.tomorrow
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should have effect on generated events' do
|
68
|
+
r = Recurrence.new(:every => :day, :until => 3.days.from_now.to_date)
|
69
|
+
r.events.first.should == Date.tomorrow
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
36
74
|
context "with daily recurring" do
|
37
75
|
it "should recur until limit date" do
|
38
76
|
@recurrence = Recurrence.daily
|
@@ -359,6 +397,13 @@ describe "recurrence" do
|
|
359
397
|
end
|
360
398
|
end
|
361
399
|
|
400
|
+
describe "#options" do
|
401
|
+
it "should return passed-in options" do
|
402
|
+
@recurrence = recurrence(:every => :day)
|
403
|
+
@recurrence.options.should == {:every => :day}
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
362
407
|
describe "#include?" do
|
363
408
|
it "should include date (day)" do
|
364
409
|
@recurrence = recurrence(:every => :day, :starts => "2008-09-30")
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
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-
|
17
|
+
date: 2010-11-03 00:00:00 -02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|