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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ae0c775f963616a18e2b349a55dac219f0fd3d6
4
- data.tar.gz: 8b7a47c160c80a30ef5f3919aea666baa93d0e13
3
+ metadata.gz: aea07a97eb08adfb0b987354b8146f2178595f7a
4
+ data.tar.gz: e2afd6e33044f41c77075429e71f0cb447c3a42f
5
5
  SHA512:
6
- metadata.gz: e0d494d9034fa8b520391e16be318a79bdd97869bca734aed9807b896dcfe2057c63b98ecabfbfbd78d7cae4075caa359783144ff7826a1e73f35f2237e43e14
7
- data.tar.gz: fc9c1c277be8e4764e0f19a1d767aaa5490f0e08c58647525ac9da9808f20e2239f88d1b37371641f98b1247ffe84eb73be80b4dcf67ad08eba936a68ed83820
6
+ metadata.gz: 543a5bc36e6b6c59224d3cd8f14075e4874d88ab7bbfe99989ec8114ffcd9cdd8ad4fc9d5924e72fd14efbcf264890e2cb723e6306285797d4dd0bd000fd542c
7
+ data.tar.gz: d0942f54379748f62cb77dc54e8e72347ba3467631195d3ea86f4681e8f4696ef1a50e547197f9a06142f997dbfcda1e66c8533c1477f0d5c6959adbb53c8de2
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Stagger
2
2
 
3
3
  [![Build Status](https://travis-ci.org/Valve/stagger.svg?branch=master)](https://travis-ci.org/Valve/stagger)
4
+ [![Gem Version](https://badge.fury.io/rb/stagger.svg)](http://badge.fury.io/rb/stagger)
5
+ [![Code Climate](https://codeclimate.com/github/Valve/stagger.png)](https://codeclimate.com/github/Valve/stagger)
4
6
 
5
7
  Stagger is a simple gem that evenly distributes items across business
6
8
  days.
@@ -1,3 +1,3 @@
1
1
  module Stagger
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
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 = items.reduce [] do |arr, item|
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 = Time.now
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 = Time.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
- at_beginning_of_day(time) + SECONDS_IN_DAY - 0.000000000001
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
- fork do
259
- require 'active_support/all'
260
- Time.zone = 'Moscow'
261
- pair = Stagger.distribute([1], 1).first
262
- expect(pair.first).to eq(1)
263
- expect(pair.last).to be_a ::ActiveSupport::TimeWithZone
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valentin Vasilyev