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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5c2110052533d6a2c202dd9b5956ab34860f27b
4
- data.tar.gz: 4e9b6686c099e9e8c31de765379dd5bcdab0914b
3
+ metadata.gz: c56e3b3deea8d3db6aa092f1bfb2718acc5556c2
4
+ data.tar.gz: 903127529c8a3dddc212b3e8ea34b2e71beb5f23
5
5
  SHA512:
6
- metadata.gz: 371cce05e26cb616d3b3611ef2becf34c1d47ce0ec161d9489525f3fd5a1cf13ac9f1996b29ee3cc84d788c453f26ac00b39799b9a48be1c8afd2e8a1a58961a
7
- data.tar.gz: b10f2e2527efcf30594e5176b5b0b6a2cce0eb6fd0d09a07ca6f4a4486139d9cc0d996455d28cf1c7b9c4f64abaaa248f451686b65a87c75eebc5a793398e736
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 cycle (in number of days), and the number of
35
- repeat counting from the start of the cycle. For example, if we want to repeat
36
- 3 days, every 5 days, starting from 2015-1-1 we would do:
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 some next occurrences:
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
@@ -4,8 +4,8 @@ module IceCubeEx
4
4
  class Rule
5
5
  class << self
6
6
  # DayCycle Rule
7
- def day_cycle(interval, cycle)
8
- DayCycleRule.new(interval, cycle)
7
+ def day_cycle(cycle, skip)
8
+ DayCycleRule.new(cycle, skip)
9
9
  end
10
10
  end
11
11
  end
@@ -4,16 +4,16 @@ module IceCubeEx
4
4
  class DayCycleRule < IceCube::ValidatedRule
5
5
  include Validations::DayCycleInterval
6
6
 
7
- def initialize(cycle, repeat)
7
+ def initialize(cycle, skip)
8
8
  super
9
- cycle(cycle, repeat)
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, repeat 2
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
- acceptable_time_percentage = calculate_percentage(number_of_days)
35
+ current_cycle_percentage = calculate_percentage(number_of_days)
36
36
 
37
- until acceptable_time_percentage <= @acceptable_cycle_percentage
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
- acceptable_time_percentage = calculate_percentage(number_of_days)
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 = (day_count.to_f / @cycle.to_f)
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, repeat)
6
+ def cycle(cycle, skip)
7
7
  @interval = 1
8
- @repeat = normalize(repeat)
8
+ @skip = normalize(skip)
9
9
  @cycle = normalize(cycle)
10
10
 
11
- unless @repeat < @cycle
12
- raise ArgumentError, 'cycle has to be a value higher than repeat'
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 = ((@repeat.to_f / @cycle.to_f) * 100).to_i
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, @repeat)]
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, :repeat
26
+ attr_reader :cycle, :skip
24
27
 
25
- def initialize(interval, cycle, repeat)
28
+ def initialize(interval, cycle, skip)
26
29
  super(interval)
27
30
  @cycle = cycle
28
- @repeat = repeat
31
+ @skip = skip
29
32
  end
30
33
 
31
34
  def build_s(builder)
32
- builder.base = "Every #{cycle} days, repeat #{repeat} times"
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[:repeat] = repeat
41
+ builder[:skip] = skip
39
42
  end
40
43
 
41
44
  def build_ical(builder)
42
- builder['FREQ'] << 'DAY_CYCLE (CUSTOM RULE)'
43
- builder['CYCLE'] << cycle
44
- builder['REPEAT'] << repeat
45
+ builder['FREQ'] << 'DAY_CYCLE (CUSTOM RULE)'
45
46
  end
46
47
  end
47
48
 
@@ -1,3 +1,3 @@
1
1
  module IceCubeEx
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,11 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe IceCubeEx::Rule do
4
4
  describe '.day_cycle' do
5
- context 'every 5 days, ' \
6
- 'repeat 3 days' do
7
- let(:cycle) { 5 }
8
- let(:repeat) { 3 }
9
- before { @rule = IceCubeEx::Rule.day_cycle(cycle, repeat) }
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) { 0 }
7
- let(:repeat) { 1 }
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, repeat)
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 repeat' do
17
- let(:cycle) { 9 }
18
- let(:repeat) { 10 }
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, repeat)
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
- 'repeat 2 days (repeat)' do
19
- let(:repeat) { 2 }
20
- let(:cycle) { 4 }
21
- let(:rule) { IceCubeEx::DayCycleRule.new(cycle, repeat) }
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', test: true do
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', test: true do
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
- 'repeat 2 days (repeat), ' \
112
+ 'skip 2 days (skip), ' \
61
113
  'which repeats 3 times (count)' do
62
- let(:repeat) { 2 }
63
- let(:cycle) { 4 }
64
- let(:count) { 3 }
65
- let(:rule) { IceCubeEx::DayCycleRule.new(cycle, repeat).count(3) }
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', test: true do
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
- 'repeat 2 days (repeat), ' \
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, repeat).until(repeat_until)
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', test: true do
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
- 'repeat 2 days (repeat)' do
126
- let(:repeat) { 2 }
127
- let(:cycle) { 4 }
128
- let(:rule) { IceCubeEx::DayCycleRule.new(cycle, repeat) }
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', test: true do
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', test: true do
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.1.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-07-28 00:00:00.000000000 Z
11
+ date: 2014-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler