business_time 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa0158cf6f58b97d18d14f31f5e1fa969b880cd7
4
- data.tar.gz: f7144c2f47e4ce925c19d661715212469f278245
3
+ metadata.gz: ec9aa7a308caa41ed4307f45ef8b798759b10a0c
4
+ data.tar.gz: 1430aa8263c16efb585414432230db3861472f55
5
5
  SHA512:
6
- metadata.gz: 015f73637a4d3958b08442811e12552edfb68a251c6b5624e57ed46ec8fc923f48a70105d3082da292ada0c4fe3eeda590a8b228b038fde1c2e513411561d1d6
7
- data.tar.gz: 7374e3d3c4264f29a5447408fb4932a2099af93b3e628a3f5a10a950ce27ea599fe624e3bd1158f564e66293e292177aadc6819e34db85a05a289c72568f72be
6
+ metadata.gz: 366fbc15803d7fab5a13ea85c485a62ad71c037de68322e3b662f9530ea1e261aecab5df46761391dcef13a3c408063b42633049f0aa70dc18832b5b4dab644b
7
+ data.tar.gz: 2c60d5bef66eaa83b38a70a8603c0b62f1a6ea1bb6ad562acfdb3714c8db6074c3c3248790bf26c86dd56b6f3729fe469bfb64b1bcc945f22d58eee1e117d7e3
@@ -17,7 +17,26 @@ module BusinessTime
17
17
  end
18
18
 
19
19
  def after(time = Time.current)
20
- days = @days
20
+ positive_days? ? calculate_after(time, @days) : calculate_before(time, -@days)
21
+ end
22
+
23
+ alias_method :from_now, :after
24
+ alias_method :since, :after
25
+
26
+ def before(time = Time.current)
27
+ positive_days? ? calculate_before(time, @days) : calculate_after(time, -@days)
28
+ end
29
+
30
+ alias_method :ago, :before
31
+ alias_method :until, :before
32
+
33
+ private
34
+
35
+ def positive_days?
36
+ @days > 0
37
+ end
38
+
39
+ def calculate_after(time, days)
21
40
  while days > 0 || !time.workday?
22
41
  days -= 1 if time.workday?
23
42
  time += 1.day
@@ -30,11 +49,7 @@ module BusinessTime
30
49
  time
31
50
  end
32
51
 
33
- alias_method :from_now, :after
34
- alias_method :since, :after
35
-
36
- def before(time = Time.current)
37
- days = @days
52
+ def calculate_before(time, days)
38
53
  while days > 0 || !time.workday?
39
54
  days -= 1 if time.workday?
40
55
  time -= 1.day
@@ -48,8 +63,5 @@ module BusinessTime
48
63
  end
49
64
  time
50
65
  end
51
-
52
- alias_method :ago, :before
53
- alias_method :until, :before
54
66
  end
55
67
  end
@@ -24,9 +24,24 @@ module BusinessTime
24
24
  end
25
25
 
26
26
  def after(time)
27
+ positive_hours? ? calculate_after(time, @hours) : calculate_before(time, -@hours)
28
+ end
29
+ alias_method :since, :after
30
+
31
+ def before(time)
32
+ positive_hours? ? calculate_before(time, @hours) : calculate_after(time, -@hours)
33
+ end
34
+
35
+ private
36
+
37
+ def positive_hours?
38
+ @hours > 0
39
+ end
40
+
41
+ def calculate_after(time, hours)
27
42
  after_time = Time.roll_forward(time)
28
43
  # Step through the hours, skipping over non-business hours
29
- @hours.times do
44
+ hours.times do
30
45
  after_time = after_time + 1.hour
31
46
 
32
47
  if after_time.hour == 0 && after_time.min == 0 && after_time.sec == 0
@@ -44,12 +59,11 @@ module BusinessTime
44
59
  end
45
60
  after_time
46
61
  end
47
- alias_method :since, :after
48
62
 
49
- def before(time)
63
+ def calculate_before(time, hours)
50
64
  before_time = Time.roll_backward(time)
51
65
  # Step through the hours, skipping over non-business hours
52
- @hours.times do
66
+ hours.times do
53
67
  before_time = before_time - 1.hour
54
68
 
55
69
  if before_time.hour == 0 && before_time.min == 0 && before_time.sec == 0
@@ -169,5 +169,30 @@ module BusinessTime
169
169
  def during_business_hours?
170
170
  self.workday? && self.to_i.between?(Time.beginning_of_workday(self).to_i, Time.end_of_workday(self).to_i)
171
171
  end
172
+
173
+ def consecutive_workdays
174
+ workday? ? consecutive_days { |date| date.workday? } : []
175
+ end
176
+
177
+ def consecutive_non_working_days
178
+ !workday? ? consecutive_days { |date| !date.workday? } : []
179
+ end
180
+
181
+ private
182
+
183
+ def consecutive_days
184
+ days = []
185
+ date = self + 1.day
186
+ while yield(date)
187
+ days << date
188
+ date += 1.day
189
+ end
190
+ date = self - 1.day
191
+ while yield(date)
192
+ days << date
193
+ date -= 1.day
194
+ end
195
+ (days << self).sort
196
+ end
172
197
  end
173
198
  end
@@ -1,3 +1,3 @@
1
1
  module BusinessTime
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - bokmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-04 00:00:00.000000000 Z
11
+ date: 2017-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.8
19
+ version: 3.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.8
26
+ version: 3.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: tzinfo
29
29
  requirement: !ruby/object:Gem::Requirement