stagger 0.0.1 → 0.1.1
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 +2 -0
- data/lib/stagger/version.rb +1 -1
- data/lib/stagger.rb +14 -10
- data/spec/stagger_spec.rb +16 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aea07a97eb08adfb0b987354b8146f2178595f7a
|
4
|
+
data.tar.gz: e2afd6e33044f41c77075429e71f0cb447c3a42f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 543a5bc36e6b6c59224d3cd8f14075e4874d88ab7bbfe99989ec8114ffcd9cdd8ad4fc9d5924e72fd14efbcf264890e2cb723e6306285797d4dd0bd000fd542c
|
7
|
+
data.tar.gz: d0942f54379748f62cb77dc54e8e72347ba3467631195d3ea86f4681e8f4696ef1a50e547197f9a06142f997dbfcda1e66c8533c1477f0d5c6959adbb53c8de2
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Stagger
|
2
2
|
|
3
3
|
[](https://travis-ci.org/Valve/stagger)
|
4
|
+
[](http://badge.fury.io/rb/stagger)
|
5
|
+
[](https://codeclimate.com/github/Valve/stagger)
|
4
6
|
|
5
7
|
Stagger is a simple gem that evenly distributes items across business
|
6
8
|
days.
|
data/lib/stagger/version.rb
CHANGED
data/lib/stagger.rb
CHANGED
@@ -7,7 +7,7 @@ module Stagger
|
|
7
7
|
return [] if Array(items).empty? || number_of_days.to_i < 1
|
8
8
|
time = get_starting_time
|
9
9
|
period_in_seconds = get_period_in_seconds(items.size, number_of_days, time)
|
10
|
-
items
|
10
|
+
items.reduce [] do |arr, item|
|
11
11
|
if business_day?(time)
|
12
12
|
arr << [item, time]
|
13
13
|
time = time + period_in_seconds
|
@@ -17,11 +17,6 @@ module Stagger
|
|
17
17
|
redo
|
18
18
|
end
|
19
19
|
end
|
20
|
-
if active_support_time?
|
21
|
-
items.map{|i,t| [i, t.in_time_zone(Time.zone)]}
|
22
|
-
else
|
23
|
-
items
|
24
|
-
end
|
25
20
|
end
|
26
21
|
|
27
22
|
private
|
@@ -35,7 +30,7 @@ module Stagger
|
|
35
30
|
end
|
36
31
|
|
37
32
|
def get_starting_time
|
38
|
-
tc =
|
33
|
+
tc = current_time
|
39
34
|
if tc.saturday?
|
40
35
|
at_beginning_of_day(tc) + SECONDS_IN_DAY * 2
|
41
36
|
elsif tc.sunday?
|
@@ -46,7 +41,7 @@ module Stagger
|
|
46
41
|
end
|
47
42
|
|
48
43
|
def get_period_in_seconds(items_size, number_of_days, starting_time)
|
49
|
-
now =
|
44
|
+
now = current_time
|
50
45
|
total_period = number_of_days * SECONDS_IN_DAY
|
51
46
|
if business_day?(now)
|
52
47
|
total_period -= (now - at_beginning_of_day(now))
|
@@ -55,11 +50,20 @@ module Stagger
|
|
55
50
|
end
|
56
51
|
|
57
52
|
def at_beginning_of_day(time)
|
58
|
-
Time.new(time.year, time.month, time.day)
|
53
|
+
active_support_time? ? time.at_beginning_of_day : Time.new(time.year, time.month, time.day)
|
59
54
|
end
|
60
55
|
|
61
56
|
def at_end_of_day(time)
|
62
|
-
|
57
|
+
if active_support_time?
|
58
|
+
time.at_end_of_day
|
59
|
+
else
|
60
|
+
at_beginning_of_day(time) + SECONDS_IN_DAY - 0.000000000001
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
def current_time
|
66
|
+
active_support_time? ? Time.zone.now : Time.now
|
63
67
|
end
|
64
68
|
|
65
69
|
def active_support_time?
|
data/spec/stagger_spec.rb
CHANGED
@@ -254,13 +254,23 @@ RSpec.describe Stagger do
|
|
254
254
|
end
|
255
255
|
|
256
256
|
context 'active support integration' do
|
257
|
+
before do
|
258
|
+
require 'active_support/all'
|
259
|
+
Time.zone = 'Asia/Tokyo'
|
260
|
+
end
|
257
261
|
it 'returns ActiveSupport::TimeWithZone when ActiveSupport is available' do
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
262
|
+
pair = Stagger.distribute([1], 1).first
|
263
|
+
expect(pair.first).to eq(1)
|
264
|
+
expect(pair.last).to be_a ::ActiveSupport::TimeWithZone
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'schedules three items in two days if today is Friday' do
|
268
|
+
t = Time.zone.local(2014, 6, 27, 14) # - 2pm, Friday
|
269
|
+
Timecop.freeze(t) do
|
270
|
+
results = Stagger.distribute([1, 2, 3], 2)
|
271
|
+
expect(results[0][1]).to eq(t) # Friday
|
272
|
+
expect(results[1][1]).to eq Time.zone.local(2014, 6, 30, 1, 20) # Monday
|
273
|
+
expect(results[2][1]).to eq Time.zone.local(2014, 6, 30, 12, 40) # Monday
|
264
274
|
end
|
265
275
|
end
|
266
276
|
end
|