ice_cube_ex 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -4
- data/lib/ice_cube_ex/rule.rb +2 -2
- data/lib/ice_cube_ex/rules/day_cycle_rule.rb +7 -7
- data/lib/ice_cube_ex/validations/day_cycle_interval.rb +15 -14
- data/lib/ice_cube_ex/version.rb +1 -1
- data/spec/ice_cube_ex/rule_spec.rb +5 -5
- data/spec/ice_cube_ex/rules/day_cycle_spec.rb +7 -7
- data/spec/ice_cube_ex/schedule_spec.rb +74 -22
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c56e3b3deea8d3db6aa092f1bfb2718acc5556c2
|
4
|
+
data.tar.gz: 903127529c8a3dddc212b3e8ea34b2e71beb5f23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83b57e7f76e01c907d25382b44a4883c1a7ccfb25ce77a7b6afcdbdf8631485d573f3702925a808bd0638688e495e0564eb5ad50999ffbc9bfbc3d0f3b5097a6
|
7
|
+
data.tar.gz: 57e654fa8a9d8d342b496adf6f3b14cefb896202a15b749943dcb181326c9be748810591047f59fdf9a1dd761a5c74a25c0cbb45b5a7b944a0afec70cdf741b8
|
data/README.md
CHANGED
@@ -31,15 +31,16 @@ ice_cube_ex has new rules that can be used with ice_cube's schedules:
|
|
31
31
|
|
32
32
|
### DayCycleRule
|
33
33
|
|
34
|
-
This rule allows to specify a
|
35
|
-
|
36
|
-
|
34
|
+
This rule allows to specify a number of days, which indicates a cycle, and
|
35
|
+
another number of days to skip inside that cycle.
|
36
|
+
For example, we want an event that starts at 2015-1-1 to recur every 5 days
|
37
|
+
while skipping the last 3 days, we would do something like this:
|
37
38
|
|
38
39
|
schedule = IceCube::Schedule.new(Time.new(2015, 1, 1)) do |s|
|
39
40
|
s.rrule IceCubeEx::Rule.day_cycle(5, 3)
|
40
41
|
end
|
41
42
|
|
42
|
-
Now try calculating
|
43
|
+
Now try calculating next occurrences:
|
43
44
|
|
44
45
|
occurrence_time = schedule.next_occurrence(Time.new(2014-12-30))
|
45
46
|
# returns 2015-1-1
|
data/lib/ice_cube_ex/rule.rb
CHANGED
@@ -4,16 +4,16 @@ module IceCubeEx
|
|
4
4
|
class DayCycleRule < IceCube::ValidatedRule
|
5
5
|
include Validations::DayCycleInterval
|
6
6
|
|
7
|
-
def initialize(cycle,
|
7
|
+
def initialize(cycle, skip)
|
8
8
|
super
|
9
|
-
cycle(cycle,
|
9
|
+
cycle(cycle, skip)
|
10
10
|
schedule_lock(:hour, :min, :sec)
|
11
11
|
reset
|
12
12
|
end
|
13
13
|
|
14
14
|
# given the following case:
|
15
15
|
#
|
16
|
-
# Schedule start_time 2014-2-2, DayCycleRule with cycle 4,
|
16
|
+
# Schedule start_time 2014-2-2, DayCycleRule with cycle 4, skip 2
|
17
17
|
#
|
18
18
|
# if we invoke schedule.next_occurrence(2014-2-1), first calculated
|
19
19
|
# time will be 2014-2-2, which will give a day_count of 0, thus being lower
|
@@ -32,13 +32,13 @@ module IceCubeEx
|
|
32
32
|
|
33
33
|
return nil unless find_acceptable_time_before(closing_time)
|
34
34
|
number_of_days = number_of_days_between(@time, cycle_start_time)
|
35
|
-
|
35
|
+
current_cycle_percentage = calculate_percentage(number_of_days)
|
36
36
|
|
37
|
-
until
|
37
|
+
until current_cycle_percentage <= @acceptable_cycle_percentage
|
38
38
|
@time += 1
|
39
39
|
return nil unless find_acceptable_time_before(closing_time)
|
40
40
|
number_of_days = number_of_days_between(@time, cycle_start_time)
|
41
|
-
|
41
|
+
current_cycle_percentage = calculate_percentage(number_of_days)
|
42
42
|
end
|
43
43
|
|
44
44
|
@uses += 1 if @time
|
@@ -57,7 +57,7 @@ module IceCubeEx
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def calculate_percentage(day_count)
|
60
|
-
value
|
60
|
+
value = (day_count.to_f / @cycle.to_f)
|
61
61
|
percentage = ((value - value.to_i) * 100).to_i
|
62
62
|
percentage.zero? ? 100 : percentage
|
63
63
|
end
|
@@ -3,45 +3,46 @@ module IceCubeEx
|
|
3
3
|
module DayCycleInterval
|
4
4
|
attr_accessor :rule
|
5
5
|
|
6
|
-
def cycle(cycle,
|
6
|
+
def cycle(cycle, skip)
|
7
7
|
@interval = 1
|
8
|
-
@
|
8
|
+
@skip = normalize(skip)
|
9
9
|
@cycle = normalize(cycle)
|
10
10
|
|
11
|
-
unless @
|
12
|
-
raise ArgumentError, '
|
11
|
+
unless @skip < @cycle
|
12
|
+
raise ArgumentError, "we can't skip more days than the number of " \
|
13
|
+
"days in the cycle, so skip has to be a value " \
|
14
|
+
"bellow cycle"
|
13
15
|
end
|
14
16
|
|
15
|
-
@acceptable_cycle_percentage =
|
17
|
+
@acceptable_cycle_percentage = \
|
18
|
+
(((@cycle - @skip).to_f / @cycle.to_f) * 100).to_i
|
16
19
|
replace_validations_for \
|
17
|
-
:interval, [Validation.new(@interval, @cycle, @
|
20
|
+
:interval, [Validation.new(@interval, @cycle, @skip)]
|
18
21
|
clobber_base_validations(:wday, :day)
|
19
22
|
self
|
20
23
|
end
|
21
24
|
|
22
25
|
class Validation < IceCube::Validations::DailyInterval::Validation
|
23
|
-
attr_reader :cycle, :
|
26
|
+
attr_reader :cycle, :skip
|
24
27
|
|
25
|
-
def initialize(interval, cycle,
|
28
|
+
def initialize(interval, cycle, skip)
|
26
29
|
super(interval)
|
27
30
|
@cycle = cycle
|
28
|
-
@
|
31
|
+
@skip = skip
|
29
32
|
end
|
30
33
|
|
31
34
|
def build_s(builder)
|
32
|
-
builder.base = "Every #{cycle} days,
|
35
|
+
builder.base = "Every #{cycle} days, skip #{skip} times"
|
33
36
|
end
|
34
37
|
|
35
38
|
def build_hash(builder)
|
36
39
|
builder[:interval] = interval
|
37
40
|
builder[:cycle] = cycle
|
38
|
-
builder[:
|
41
|
+
builder[:skip] = skip
|
39
42
|
end
|
40
43
|
|
41
44
|
def build_ical(builder)
|
42
|
-
builder['FREQ']
|
43
|
-
builder['CYCLE'] << cycle
|
44
|
-
builder['REPEAT'] << repeat
|
45
|
+
builder['FREQ'] << 'DAY_CYCLE (CUSTOM RULE)'
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
data/lib/ice_cube_ex/version.rb
CHANGED
@@ -2,11 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe IceCubeEx::Rule do
|
4
4
|
describe '.day_cycle' do
|
5
|
-
context '
|
6
|
-
'
|
7
|
-
let(:cycle)
|
8
|
-
let(:
|
9
|
-
before { @rule = IceCubeEx::Rule.day_cycle(cycle,
|
5
|
+
context 'cycle 5 days, ' \
|
6
|
+
'skip 3 days' do
|
7
|
+
let(:cycle) { 5 }
|
8
|
+
let(:skip) { 3 }
|
9
|
+
before { @rule = IceCubeEx::Rule.day_cycle(cycle, skip) }
|
10
10
|
|
11
11
|
it 'returns an instance of DayCycleRule' do
|
12
12
|
expect(@rule.class).to eq(IceCubeEx::DayCycleRule)
|
@@ -3,23 +3,23 @@ require 'spec_helper'
|
|
3
3
|
describe IceCubeEx::DayCycleRule do
|
4
4
|
describe '.new' do
|
5
5
|
context 'given cycle argument bellow 2' do
|
6
|
-
let(:cycle)
|
7
|
-
let(:
|
6
|
+
let(:cycle) { 0 }
|
7
|
+
let(:skip) { 1 }
|
8
8
|
|
9
9
|
it 'raises error' do
|
10
10
|
expect do
|
11
|
-
IceCubeEx::DayCycleRule.new(cycle,
|
11
|
+
IceCubeEx::DayCycleRule.new(cycle, skip)
|
12
12
|
end.to raise_error
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
context 'given cycle argument lower than
|
17
|
-
let(:cycle)
|
18
|
-
let(:
|
16
|
+
context 'given cycle argument lower than skip' do
|
17
|
+
let(:cycle) { 9 }
|
18
|
+
let(:skip) { 10 }
|
19
19
|
|
20
20
|
it 'raises error' do
|
21
21
|
expect do
|
22
|
-
IceCubeEx::DayCycleRule.new(cycle,
|
22
|
+
IceCubeEx::DayCycleRule.new(cycle, skip)
|
23
23
|
end.to raise_error
|
24
24
|
end
|
25
25
|
end
|
@@ -13,12 +13,64 @@ describe IceCube::Schedule do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
context 'given rule of type DayCycleRule initialized to ' \
|
17
|
+
'every 5 days (cycle), ' \
|
18
|
+
'skip 2 days (skip)' do
|
19
|
+
let(:cycle) { 5 }
|
20
|
+
let(:skip) { 2 }
|
21
|
+
let(:rule) { IceCubeEx::DayCycleRule.new(cycle, skip) }
|
22
|
+
|
23
|
+
context 'calculating from 3-2-2012' do
|
24
|
+
let(:from) { Time.new(2012, 2, 3) }
|
25
|
+
let(:first_occurrence) { schedule.next_occurrence from }
|
26
|
+
let(:second_occurrence) { schedule.next_occurrence first_occurrence }
|
27
|
+
let(:third_occurrence) { schedule.next_occurrence second_occurrence }
|
28
|
+
let(:fourth_occurrence) { schedule.next_occurrence third_occurrence }
|
29
|
+
let(:fifth_occurrence) { schedule.next_occurrence fourth_occurrence }
|
30
|
+
let(:sixth_occurrence) { schedule.next_occurrence fifth_occurrence }
|
31
|
+
let(:seventh_occurrence) { schedule.next_occurrence sixth_occurrence }
|
32
|
+
let(:eighth_occurrence) { schedule.next_occurrence seventh_occurrence }
|
33
|
+
|
34
|
+
it 'returns 4-2-2012 at 12am as first occurrence' do
|
35
|
+
expect(first_occurrence).to eq(start_time)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'returns 5-2-2012 at 12am as second occurrence' do
|
39
|
+
expect(second_occurrence).to eq(Time.new(2012, 2, 5, 12))
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns 6-2-2012 at 12am as third occurrence' do
|
43
|
+
expect(third_occurrence).to eq(Time.new(2012, 2, 6, 12))
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns 9-2-2012 at 12am as fourth occurrence' do
|
47
|
+
expect(fourth_occurrence).to eq(Time.new(2012, 2, 9, 12))
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'returns 10-2-2012 at 12am as fifth occurrence' do
|
51
|
+
expect(fifth_occurrence).to eq(Time.new(2012, 2, 10, 12))
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns 11-2-2012 at 12am as sixth occurrence' do
|
55
|
+
expect(sixth_occurrence).to eq(Time.new(2012, 2, 11, 12))
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'returns 14-2-2012 at 12am as seventh occurrence' do
|
59
|
+
expect(seventh_occurrence).to eq(Time.new(2012, 2, 14, 12))
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'returns 15-2-2012 at 12am as eighth occurrence' do
|
63
|
+
expect(eighth_occurrence).to eq(Time.new(2012, 2, 15, 12))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
16
68
|
context 'given rule of type DayCycleRule initialized to ' \
|
17
69
|
'every 4 days (cycle), ' \
|
18
|
-
'
|
19
|
-
let(:
|
20
|
-
let(:
|
21
|
-
let(:rule)
|
70
|
+
'skip 2 days (skip)' do
|
71
|
+
let(:cycle) { 4 }
|
72
|
+
let(:skip) { 2 }
|
73
|
+
let(:rule) { IceCubeEx::DayCycleRule.new(cycle, skip) }
|
22
74
|
|
23
75
|
context 'calculating from 3-2-2012' do
|
24
76
|
let(:from) { Time.new(2012, 2, 3) }
|
@@ -37,7 +89,7 @@ describe IceCube::Schedule do
|
|
37
89
|
expect(second_occurrence).to eq(Time.new(2012, 2, 5, 12))
|
38
90
|
end
|
39
91
|
|
40
|
-
it 'returns 8-2-2012 at 12am as third occurrence'
|
92
|
+
it 'returns 8-2-2012 at 12am as third occurrence' do
|
41
93
|
expect(third_occurrence).to eq(Time.new(2012, 2, 8, 12))
|
42
94
|
end
|
43
95
|
|
@@ -45,7 +97,7 @@ describe IceCube::Schedule do
|
|
45
97
|
expect(fourth_occurrence).to eq(Time.new(2012, 2, 9, 12))
|
46
98
|
end
|
47
99
|
|
48
|
-
it 'returns 12-2-2012 at 12am as fifth occurrence'
|
100
|
+
it 'returns 12-2-2012 at 12am as fifth occurrence' do
|
49
101
|
expect(fifth_occurrence).to eq(Time.new(2012, 2, 12, 12))
|
50
102
|
end
|
51
103
|
|
@@ -57,12 +109,12 @@ describe IceCube::Schedule do
|
|
57
109
|
|
58
110
|
context 'given rule of type DayCycleRule initialized to ' \
|
59
111
|
'every 4 days (cycle), ' \
|
60
|
-
'
|
112
|
+
'skip 2 days (skip), ' \
|
61
113
|
'which repeats 3 times (count)' do
|
62
|
-
let(:
|
63
|
-
let(:
|
64
|
-
let(:count)
|
65
|
-
let(:rule)
|
114
|
+
let(:cycle) { 4 }
|
115
|
+
let(:skip) { 2 }
|
116
|
+
let(:count) { 3 }
|
117
|
+
let(:rule) { IceCubeEx::DayCycleRule.new(cycle, skip).count(3) }
|
66
118
|
|
67
119
|
context 'calculating from 3-2-2012' do
|
68
120
|
let(:from) { Time.new(2012, 2, 3) }
|
@@ -79,7 +131,7 @@ describe IceCube::Schedule do
|
|
79
131
|
expect(second_occurrence).to eq(Time.new(2012, 2, 5, 12))
|
80
132
|
end
|
81
133
|
|
82
|
-
it 'returns 8-2-2012 at 12am as third occurrence'
|
134
|
+
it 'returns 8-2-2012 at 12am as third occurrence' do
|
83
135
|
expect(third_occurrence).to eq(Time.new(2012, 2, 8, 12))
|
84
136
|
end
|
85
137
|
|
@@ -91,13 +143,13 @@ describe IceCube::Schedule do
|
|
91
143
|
|
92
144
|
context 'given rule of type DayCycleRule initialized to ' \
|
93
145
|
'every 4 days (cycle), ' \
|
94
|
-
'
|
146
|
+
'skip 2 days (skip), ' \
|
95
147
|
'which repeats until 6-2-2012' do
|
96
|
-
let(:repeat) { 2 }
|
97
148
|
let(:cycle) { 4 }
|
149
|
+
let(:skip) { 2 }
|
98
150
|
let(:repeat_until) { Time.new(2012, 2, 6) }
|
99
151
|
let(:rule) do
|
100
|
-
IceCubeEx::DayCycleRule.new(cycle,
|
152
|
+
IceCubeEx::DayCycleRule.new(cycle, skip).until(repeat_until)
|
101
153
|
end
|
102
154
|
|
103
155
|
context 'calculating from 3-2-2012' do
|
@@ -114,7 +166,7 @@ describe IceCube::Schedule do
|
|
114
166
|
expect(second_occurrence).to eq(Time.new(2012, 2, 5, 12))
|
115
167
|
end
|
116
168
|
|
117
|
-
it 'returns nil as third occurrence'
|
169
|
+
it 'returns nil as third occurrence' do
|
118
170
|
expect(third_occurrence).to eq(nil)
|
119
171
|
end
|
120
172
|
end
|
@@ -122,10 +174,10 @@ describe IceCube::Schedule do
|
|
122
174
|
|
123
175
|
context 'given rule of type DayCycleRule initialized to ' \
|
124
176
|
'every 4 days (cycle), ' \
|
125
|
-
'
|
126
|
-
let(:
|
127
|
-
let(:
|
128
|
-
let(:rule)
|
177
|
+
'skip 2 days (skip)' do
|
178
|
+
let(:cycle) { 4 }
|
179
|
+
let(:skip) { 2 }
|
180
|
+
let(:rule) { IceCubeEx::DayCycleRule.new(cycle, skip) }
|
129
181
|
|
130
182
|
context 'given serialized and deserialized schedule with YAML' do
|
131
183
|
before do
|
@@ -149,7 +201,7 @@ describe IceCube::Schedule do
|
|
149
201
|
expect(second_occurrence).to eq(Time.new(2012, 2, 5, 12))
|
150
202
|
end
|
151
203
|
|
152
|
-
it 'returns 8-2-2012 at 12am as third occurrence'
|
204
|
+
it 'returns 8-2-2012 at 12am as third occurrence' do
|
153
205
|
expect(third_occurrence).to eq(Time.new(2012, 2, 8, 12))
|
154
206
|
end
|
155
207
|
|
@@ -157,7 +209,7 @@ describe IceCube::Schedule do
|
|
157
209
|
expect(fourth_occurrence).to eq(Time.new(2012, 2, 9, 12))
|
158
210
|
end
|
159
211
|
|
160
|
-
it 'returns 12-2-2012 at 12am as fifth occurrence'
|
212
|
+
it 'returns 12-2-2012 at 12am as fifth occurrence' do
|
161
213
|
expect(fifth_occurrence).to eq(Time.new(2012, 2, 12, 12))
|
162
214
|
end
|
163
215
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ice_cube_ex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- junhanamaki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|