ice_cube_ex 0.0.1 → 0.1.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: 3a1f31275d0e2a370214241ff1a1e6431fb40c2f
4
- data.tar.gz: 7332e9354da6df596ec727898b5f61e65b1491f3
3
+ metadata.gz: e5c2110052533d6a2c202dd9b5956ab34860f27b
4
+ data.tar.gz: 4e9b6686c099e9e8c31de765379dd5bcdab0914b
5
5
  SHA512:
6
- metadata.gz: 2c25a1c70946711f7fc347c4ce12b5215298d3b39bf008037c6227e9c694f62056128c9ca7ed1966c8ac84ae9963fec1bd4917fe7c7465cd2101a9ad8dfc0314
7
- data.tar.gz: 18f44f2f3c74d3c33ddfcedb5aa4447bb63072db0ce4c9f9fd728094ec2a460c5f22b92eb74f1b343a86cd348f8e12e55ab204c4da9510b1b965d817c6d704d1
6
+ metadata.gz: 371cce05e26cb616d3b3611ef2becf34c1d47ce0ec161d9489525f3fd5a1cf13ac9f1996b29ee3cc84d788c453f26ac00b39799b9a48be1c8afd2e8a1a58961a
7
+ data.tar.gz: b10f2e2527efcf30594e5176b5b0b6a2cce0eb6fd0d09a07ca6f4a4486139d9cc0d996455d28cf1c7b9c4f64abaaa248f451686b65a87c75eebc5a793398e736
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # IceCubeEx
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/ice_cube_ex.svg)](http://badge.fury.io/rb/ice_cube_ex)
3
4
  [![build](https://travis-ci.org/junhanamaki/ice_cube_ex.svg?branch=master)](https://travis-ci.org/junhanamaki/ice_cube_ex)
4
5
  [![Code Climate](https://codeclimate.com/github/junhanamaki/ice_cube_ex.png)](https://codeclimate.com/github/junhanamaki/ice_cube_ex)
5
6
  [![Test Coverage](https://codeclimate.com/github/junhanamaki/ice_cube_ex/coverage.png)](https://codeclimate.com/github/junhanamaki/ice_cube_ex)
@@ -26,7 +27,40 @@ Or install it yourself as:
26
27
 
27
28
  ## Usage
28
29
 
29
- TODO: Write usage instructions here
30
+ ice_cube_ex has new rules that can be used with ice_cube's schedules:
31
+
32
+ ### DayCycleRule
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:
37
+
38
+ schedule = IceCube::Schedule.new(Time.new(2015, 1, 1)) do |s|
39
+ s.rrule IceCubeEx::Rule.day_cycle(5, 3)
40
+ end
41
+
42
+ Now try calculating some next occurrences:
43
+
44
+ occurrence_time = schedule.next_occurrence(Time.new(2014-12-30))
45
+ # returns 2015-1-1
46
+
47
+ occurrence_time = schedule.next_occurrence(occurrence_time)
48
+ # returns 2015-1-2
49
+
50
+ occurrence_time = schedule.next_occurrence(occurrence_time)
51
+ # returns 2015-1-3
52
+
53
+ occurrence_time = schedule.next_occurrence(occurrence_time)
54
+ # returns 2015-1-6
55
+
56
+ occurrence_time = schedule.next_occurrence(occurrence_time)
57
+ # returns 2015-1-7
58
+
59
+ occurrence_time = schedule.next_occurrence(occurrence_time)
60
+ # returns 2015-1-8
61
+
62
+ You can also use count and until to limit your rule as you would normally do
63
+ with a regular ice_cube rule.
30
64
 
31
65
  ## Contributing
32
66
 
@@ -0,0 +1,12 @@
1
+ require 'ice_cube_ex/rules/day_cycle_rule'
2
+
3
+ module IceCubeEx
4
+ class Rule
5
+ class << self
6
+ # DayCycle Rule
7
+ def day_cycle(interval, cycle)
8
+ DayCycleRule.new(interval, cycle)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,8 +1,8 @@
1
- require 'ice_cube_ex/ice_cube/validations/daily_cycle_interval'
1
+ require 'ice_cube_ex/validations/day_cycle_interval'
2
2
 
3
- module IceCube
4
- class DailyCycleRule < ValidatedRule
5
- include Validations::DailyCycleInterval
3
+ module IceCubeEx
4
+ class DayCycleRule < IceCube::ValidatedRule
5
+ include Validations::DayCycleInterval
6
6
 
7
7
  def initialize(cycle, repeat)
8
8
  super
@@ -11,12 +11,9 @@ module IceCube
11
11
  reset
12
12
  end
13
13
 
14
- #
15
- # cycle_start_time and why we start from one day before:
16
- #
17
14
  # given the following case:
18
15
  #
19
- # Schedule start_time 2014-2-2, DailyCycleRule with cycle 4, repeat 2
16
+ # Schedule start_time 2014-2-2, DayCycleRule with cycle 4, repeat 2
20
17
  #
21
18
  # if we invoke schedule.next_occurrence(2014-2-1), first calculated
22
19
  # time will be 2014-2-2, which will give a day_count of 0, thus being lower
@@ -27,21 +24,21 @@ module IceCube
27
24
  #
28
25
  # 2014-2-2, 2014-2-3, 2014-2-4
29
26
  #
30
- # so to avoid this we start counting from start_time - 1.day
27
+ # so to avoid this we start counting from start_time - 1.day (cycle_start_time)
31
28
  def next_time(time, schedule, closing_time)
32
29
  @time = time
33
30
  @schedule = schedule
34
31
  cycle_start_time = schedule.start_time - 24 * 60 * 60
35
32
 
36
33
  return nil unless find_acceptable_time_before(closing_time)
37
- day_count = number_of_days_between(@time, cycle_start_time)
38
- acceptable_time_percentage = calculate_cycle_percentage(day_count)
34
+ number_of_days = number_of_days_between(@time, cycle_start_time)
35
+ acceptable_time_percentage = calculate_percentage(number_of_days)
39
36
 
40
- until acceptable_time_percentage <= @cycle_percentage
37
+ until acceptable_time_percentage <= @acceptable_cycle_percentage
41
38
  @time += 1
42
39
  return nil unless find_acceptable_time_before(closing_time)
43
- day_count = number_of_days_between(@time, cycle_start_time)
44
- acceptable_time_percentage = calculate_cycle_percentage(day_count)
40
+ number_of_days = number_of_days_between(@time, cycle_start_time)
41
+ acceptable_time_percentage = calculate_percentage(number_of_days)
45
42
  end
46
43
 
47
44
  @uses += 1 if @time
@@ -59,7 +56,7 @@ module IceCube
59
56
  Time.new(time.year, time.month, time.day, 0, 0, 0, '+00:00')
60
57
  end
61
58
 
62
- def calculate_cycle_percentage(day_count)
59
+ def calculate_percentage(day_count)
63
60
  value = (day_count.to_f / @cycle.to_f)
64
61
  percentage = ((value - value.to_i) * 100).to_i
65
62
  percentage.zero? ? 100 : percentage
@@ -1,6 +1,6 @@
1
- module IceCube
1
+ module IceCubeEx
2
2
  module Validations
3
- module DailyCycleInterval
3
+ module DayCycleInterval
4
4
  attr_accessor :rule
5
5
 
6
6
  def cycle(cycle, repeat)
@@ -12,38 +12,22 @@ module IceCube
12
12
  raise ArgumentError, 'cycle has to be a value higher than repeat'
13
13
  end
14
14
 
15
- @cycle_percentage = ((@repeat.to_f / @cycle.to_f) * 100).to_i
15
+ @acceptable_cycle_percentage = ((@repeat.to_f / @cycle.to_f) * 100).to_i
16
16
  replace_validations_for \
17
17
  :interval, [Validation.new(@interval, @cycle, @repeat)]
18
18
  clobber_base_validations(:wday, :day)
19
19
  self
20
20
  end
21
21
 
22
- class Validation
23
- attr_reader :interval, :cycle, :repeat
22
+ class Validation < IceCube::Validations::DailyInterval::Validation
23
+ attr_reader :cycle, :repeat
24
24
 
25
25
  def initialize(interval, cycle, repeat)
26
- @interval = interval
26
+ super(interval)
27
27
  @cycle = cycle
28
28
  @repeat = repeat
29
29
  end
30
30
 
31
- def type
32
- :day
33
- end
34
-
35
- def dst_adjust?
36
- true
37
- end
38
-
39
- def validate(step_time, schedule)
40
- t0, t1 = schedule.start_time, step_time
41
- days = Date.new(t1.year, t1.month, t1.day) -
42
- Date.new(t0.year, t0.month, t0.day)
43
- offset = (days % interval).nonzero?
44
- interval - offset if offset
45
- end
46
-
47
31
  def build_s(builder)
48
32
  builder.base = "Every #{cycle} days, repeat #{repeat} times"
49
33
  end
@@ -55,7 +39,7 @@ module IceCube
55
39
  end
56
40
 
57
41
  def build_ical(builder)
58
- builder['FREQ'] << 'DAILY_CYCLE (CUSTOM RULE)'
42
+ builder['FREQ'] << 'DAY_CYCLE (CUSTOM RULE)'
59
43
  builder['CYCLE'] << cycle
60
44
  builder['REPEAT'] << repeat
61
45
  end
@@ -1,3 +1,3 @@
1
1
  module IceCubeEx
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/ice_cube_ex.rb CHANGED
@@ -7,4 +7,4 @@ end
7
7
  module IceCubeEx
8
8
  end
9
9
 
10
- require 'ice_cube_ex/ice_cube/rule'
10
+ require 'ice_cube_ex/rule'
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe IceCubeEx::Rule do
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) }
10
+
11
+ it 'returns an instance of DayCycleRule' do
12
+ expect(@rule.class).to eq(IceCubeEx::DayCycleRule)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe IceCube::DailyCycleRule do
3
+ describe IceCubeEx::DayCycleRule do
4
4
  describe '.new' do
5
5
  context 'given cycle argument bellow 2' do
6
6
  let(:cycle) { 0 }
@@ -8,7 +8,7 @@ describe IceCube::DailyCycleRule do
8
8
 
9
9
  it 'raises error' do
10
10
  expect do
11
- IceCube::DailyCycleRule.new(cycle, repeat)
11
+ IceCubeEx::DayCycleRule.new(cycle, repeat)
12
12
  end.to raise_error
13
13
  end
14
14
  end
@@ -19,7 +19,7 @@ describe IceCube::DailyCycleRule do
19
19
 
20
20
  it 'raises error' do
21
21
  expect do
22
- IceCube::DailyCycleRule.new(cycle, repeat)
22
+ IceCubeEx::DayCycleRule.new(cycle, repeat)
23
23
  end.to raise_error
24
24
  end
25
25
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'yaml'
2
3
 
3
4
  describe IceCube::Schedule do
4
5
  describe '.next_occurrence' do
@@ -12,12 +13,12 @@ describe IceCube::Schedule do
12
13
  end
13
14
  end
14
15
 
15
- context 'given rule of type DailyCycleRule initialized to ' \
16
+ context 'given rule of type DayCycleRule initialized to ' \
16
17
  'every 4 days (cycle), ' \
17
18
  'repeat 2 days (repeat)' do
18
19
  let(:repeat) { 2 }
19
20
  let(:cycle) { 4 }
20
- let(:rule) { IceCube::DailyCycleRule.new(cycle, repeat) }
21
+ let(:rule) { IceCubeEx::DayCycleRule.new(cycle, repeat) }
21
22
 
22
23
  context 'calculating from 3-2-2012' do
23
24
  let(:from) { Time.new(2012, 2, 3) }
@@ -54,14 +55,14 @@ describe IceCube::Schedule do
54
55
  end
55
56
  end
56
57
 
57
- context 'given rule of type DailyCycleRule initialized to ' \
58
+ context 'given rule of type DayCycleRule initialized to ' \
58
59
  'every 4 days (cycle), ' \
59
60
  'repeat 2 days (repeat), ' \
60
61
  'which repeats 3 times (count)' do
61
62
  let(:repeat) { 2 }
62
63
  let(:cycle) { 4 }
63
64
  let(:count) { 3 }
64
- let(:rule) { IceCube::DailyCycleRule.new(cycle, repeat).count(3) }
65
+ let(:rule) { IceCubeEx::DayCycleRule.new(cycle, repeat).count(3) }
65
66
 
66
67
  context 'calculating from 3-2-2012' do
67
68
  let(:from) { Time.new(2012, 2, 3) }
@@ -88,7 +89,7 @@ describe IceCube::Schedule do
88
89
  end
89
90
  end
90
91
 
91
- context 'given rule of type DailyCycleRule initialized to ' \
92
+ context 'given rule of type DayCycleRule initialized to ' \
92
93
  'every 4 days (cycle), ' \
93
94
  'repeat 2 days (repeat), ' \
94
95
  'which repeats until 6-2-2012' do
@@ -96,7 +97,7 @@ describe IceCube::Schedule do
96
97
  let(:cycle) { 4 }
97
98
  let(:repeat_until) { Time.new(2012, 2, 6) }
98
99
  let(:rule) do
99
- IceCube::DailyCycleRule.new(cycle, repeat).until(repeat_until)
100
+ IceCubeEx::DayCycleRule.new(cycle, repeat).until(repeat_until)
100
101
  end
101
102
 
102
103
  context 'calculating from 3-2-2012' do
@@ -118,6 +119,54 @@ describe IceCube::Schedule do
118
119
  end
119
120
  end
120
121
  end
122
+
123
+ context 'given rule of type DayCycleRule initialized to ' \
124
+ '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) }
129
+
130
+ context 'given serialized and deserialized schedule with YAML' do
131
+ before do
132
+ @schedule = YAML::load(YAML::dump(schedule))
133
+ end
134
+
135
+ context 'calculating from 3-2-2012' do
136
+ let(:from) { Time.new(2012, 2, 3) }
137
+ let(:first_occurrence) { @schedule.next_occurrence from }
138
+ let(:second_occurrence) { @schedule.next_occurrence first_occurrence }
139
+ let(:third_occurrence) { @schedule.next_occurrence second_occurrence }
140
+ let(:fourth_occurrence) { @schedule.next_occurrence third_occurrence }
141
+ let(:fifth_occurrence) { @schedule.next_occurrence fourth_occurrence }
142
+ let(:sixth_occurrence) { @schedule.next_occurrence fifth_occurrence }
143
+
144
+ it 'returns 4-2-2012 at 12am as first occurrence' do
145
+ expect(first_occurrence).to eq(start_time)
146
+ end
147
+
148
+ it 'returns 5-2-2012 at 12am as second occurrence' do
149
+ expect(second_occurrence).to eq(Time.new(2012, 2, 5, 12))
150
+ end
151
+
152
+ it 'returns 8-2-2012 at 12am as third occurrence', test: true do
153
+ expect(third_occurrence).to eq(Time.new(2012, 2, 8, 12))
154
+ end
155
+
156
+ it 'returns 9-2-2012 at 12am as fourth occurrence' do
157
+ expect(fourth_occurrence).to eq(Time.new(2012, 2, 9, 12))
158
+ end
159
+
160
+ it 'returns 12-2-2012 at 12am as fifth occurrence', test: true do
161
+ expect(fifth_occurrence).to eq(Time.new(2012, 2, 12, 12))
162
+ end
163
+
164
+ it 'returns 13-2-2012 at 12am as sixth occurrence' do
165
+ expect(sixth_occurrence).to eq(Time.new(2012, 2, 13, 12))
166
+ end
167
+ end
168
+ end
169
+ end
121
170
  end
122
171
  end
123
172
  end
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.0.1
4
+ version: 0.1.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-27 00:00:00.000000000 Z
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,13 +114,13 @@ files:
114
114
  - Rakefile
115
115
  - ice_cube_ex.gemspec
116
116
  - lib/ice_cube_ex.rb
117
- - lib/ice_cube_ex/ice_cube/rule.rb
118
- - lib/ice_cube_ex/ice_cube/rules/daily_cycle_rule.rb
119
- - lib/ice_cube_ex/ice_cube/validations/daily_cycle_interval.rb
117
+ - lib/ice_cube_ex/rule.rb
118
+ - lib/ice_cube_ex/rules/day_cycle_rule.rb
119
+ - lib/ice_cube_ex/validations/day_cycle_interval.rb
120
120
  - lib/ice_cube_ex/version.rb
121
- - spec/ice_cube_ex/ice_cube/rule_spec.rb
122
- - spec/ice_cube_ex/ice_cube/rules/daily_cycle_spec.rb
123
- - spec/ice_cube_ex/ice_cube/schedule_spec.rb
121
+ - spec/ice_cube_ex/rule_spec.rb
122
+ - spec/ice_cube_ex/rules/day_cycle_spec.rb
123
+ - spec/ice_cube_ex/schedule_spec.rb
124
124
  - spec/spec_helper.rb
125
125
  homepage: https://github.com/junhanamaki/ice_cube_ex
126
126
  licenses:
@@ -147,8 +147,8 @@ signing_key:
147
147
  specification_version: 4
148
148
  summary: extending ice_cube with new rules
149
149
  test_files:
150
- - spec/ice_cube_ex/ice_cube/rule_spec.rb
151
- - spec/ice_cube_ex/ice_cube/rules/daily_cycle_spec.rb
152
- - spec/ice_cube_ex/ice_cube/schedule_spec.rb
150
+ - spec/ice_cube_ex/rule_spec.rb
151
+ - spec/ice_cube_ex/rules/day_cycle_spec.rb
152
+ - spec/ice_cube_ex/schedule_spec.rb
153
153
  - spec/spec_helper.rb
154
154
  has_rdoc:
@@ -1,12 +0,0 @@
1
- require 'ice_cube_ex/ice_cube/rules/daily_cycle_rule'
2
-
3
- module IceCube
4
- class Rule
5
- class << self
6
- # DailyCycle Rule
7
- def daily_cycle(interval, cycle)
8
- DailyCycleRule.new(interval, cycle)
9
- end
10
- end
11
- end
12
- end
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe IceCube::Rule do
4
- describe '.daily_cycle' do
5
- context 'every 5 days, ' \
6
- 'repeat 3 days' do
7
- let(:cycle) { 5 }
8
- let(:repeat) { 3 }
9
- before { @rule = IceCube::Rule.daily_cycle(cycle, repeat) }
10
-
11
- it 'returns an instance of DailyCycleRule' do
12
- expect(@rule.class).to eq(IceCube::DailyCycleRule)
13
- end
14
- end
15
- end
16
- end