business_time 0.12.0 → 0.13.0

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
  SHA256:
3
- metadata.gz: 1c76c5a8e2c840f078e70de2e996a33bc11106501cccca459f1e77ce236bf587
4
- data.tar.gz: 8d41d5e765ff55f6158049fabd41039c6acb121e2c2be834d579f60a0fa6a3ed
3
+ metadata.gz: 3006a4d1708551ad12693457ba051e16b0b8f94439cc8cca54999519d20a5a63
4
+ data.tar.gz: 785c54ba3652f861532d1963dff4afbcb8194b78ffb7a46de3932778aafb6703
5
5
  SHA512:
6
- metadata.gz: 46bececd4f088d2a50a061f83510e0e998f427e4fe7ef49d304a8949b7f42f8a1265689f2f83c972829d3a175813dc3a79956b117aa8719a4203b5afd86ab9c6
7
- data.tar.gz: 42316dc95c99be2600588ad5a2d765a97b79abf9369c0d41235f375c3e7b6ee3ae63318da62a0a20b0a989499ae9527263da3b7184a65e21c9d2e5bc6b0db2e1
6
+ metadata.gz: dac78848ef4878c0891b30f3eb8842578047eb3ae559681e25f7f5dbac885643719fa28ebab37af36daa26d5278cc88f9e40c8919bfbb9b26aa8d38e625c25b2
7
+ data.tar.gz: 73529a47adeb15c99c0cc06143ee70bb5f2eeb6ee4f0da6c2cff79970082f742aeb412f4e0d201643bb60b9c024dea51c96b0db67ff4220f9a0ca51049264ca8
@@ -5,7 +5,7 @@ module BusinessTime
5
5
  include Comparable
6
6
  attr_reader :days
7
7
 
8
- def initialize(days)
8
+ def initialize(days, options={})
9
9
  @days = days
10
10
  end
11
11
 
@@ -16,15 +16,15 @@ module BusinessTime
16
16
  self.days <=> other.days
17
17
  end
18
18
 
19
- def after(time = Time.current)
20
- non_negative_days? ? calculate_after(time, @days) : calculate_before(time, -@days)
19
+ def after(time = Time.current, options={})
20
+ non_negative_days? ? calculate_after(time, @days, options) : calculate_before(time, -@days, options)
21
21
  end
22
22
 
23
23
  alias_method :from_now, :after
24
24
  alias_method :since, :after
25
25
 
26
- def before(time = Time.current)
27
- non_negative_days? ? calculate_before(time, @days) : calculate_after(time, -@days)
26
+ def before(time = Time.current, options={})
27
+ non_negative_days? ? calculate_before(time, @days, options) : calculate_after(time, -@days, options)
28
28
  end
29
29
 
30
30
  alias_method :ago, :before
@@ -36,35 +36,35 @@ module BusinessTime
36
36
  @days >= 0
37
37
  end
38
38
 
39
- def calculate_after(time, days)
40
- if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?
39
+ def calculate_after(time, days, options={})
40
+ if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?(options)
41
41
  time = Time.beginning_of_workday(time)
42
42
  end
43
- while days > 0 || !time.workday?
44
- days -= 1 if time.workday?
43
+ while days > 0 || !time.workday?(options)
44
+ days -= 1 if time.workday?(options)
45
45
  time += 1.day
46
46
  end
47
47
  # If we have a Time or DateTime object, we can roll_forward to the
48
48
  # beginning of the next business day
49
49
  if time.is_a?(Time) || time.is_a?(DateTime)
50
- time = Time.roll_forward(time) unless time.during_business_hours?
50
+ time = Time.roll_forward(time, options) unless time.during_business_hours?
51
51
  end
52
52
  time
53
53
  end
54
54
 
55
- def calculate_before(time, days)
56
- if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?
55
+ def calculate_before(time, days, options={})
56
+ if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?(options)
57
57
  time = Time.beginning_of_workday(time)
58
58
  end
59
- while days > 0 || !time.workday?
60
- days -= 1 if time.workday?
59
+ while days > 0 || !time.workday?(options)
60
+ days -= 1 if time.workday?(options)
61
61
  time -= 1.day
62
62
  end
63
63
  # If we have a Time or DateTime object, we can roll_backward to the
64
64
  # beginning of the previous business day
65
65
  if time.is_a?(Time) || time.is_a?(DateTime)
66
66
  unless time.during_business_hours?
67
- time = Time.beginning_of_workday(Time.roll_backward(time))
67
+ time = Time.beginning_of_workday(Time.roll_backward(time, options))
68
68
  end
69
69
  end
70
70
  time
@@ -3,8 +3,8 @@ module BusinessTime
3
3
  class BusinessHours
4
4
  include Comparable
5
5
  attr_reader :hours
6
-
7
- def initialize(hours)
6
+
7
+ def initialize(hours, options={})
8
8
  @hours = hours
9
9
  end
10
10
 
@@ -15,21 +15,21 @@ module BusinessTime
15
15
  self.hours <=> other.hours
16
16
  end
17
17
 
18
- def ago
19
- Time.zone ? before(Time.zone.now) : before(Time.now)
18
+ def ago(options={})
19
+ Time.zone ? before(Time.zone.now, options) : before(Time.now, options)
20
20
  end
21
21
 
22
- def from_now
23
- Time.zone ? after(Time.zone.now) : after(Time.now)
22
+ def from_now(options={})
23
+ Time.zone ? after(Time.zone.now, options) : after(Time.now, options)
24
24
  end
25
25
 
26
- def after(time)
27
- non_negative_hours? ? calculate_after(time, @hours) : calculate_before(time, -@hours)
26
+ def after(time, options={})
27
+ non_negative_hours? ? calculate_after(time, @hours, options) : calculate_before(time, -@hours, options)
28
28
  end
29
29
  alias_method :since, :after
30
30
 
31
- def before(time)
32
- non_negative_hours? ? calculate_before(time, @hours) : calculate_after(time, -@hours)
31
+ def before(time, options={})
32
+ non_negative_hours? ? calculate_before(time, @hours, options) : calculate_after(time, -@hours, options)
33
33
  end
34
34
 
35
35
  private
@@ -38,18 +38,18 @@ module BusinessTime
38
38
  @hours >= 0
39
39
  end
40
40
 
41
- def calculate_after(time, hours)
42
- after_time = Time.roll_forward(time)
41
+ def calculate_after(time, hours, options={})
42
+ after_time = Time.roll_forward(time, options)
43
43
  # Step through the hours, skipping over non-business hours
44
44
  hours.times do
45
45
  after_time = after_time + 1.hour
46
46
 
47
47
  if after_time.hour == 0 && after_time.min == 0 && after_time.sec == 0
48
- after_time = Time.roll_forward(after_time)
48
+ after_time = Time.roll_forward(after_time, options)
49
49
  elsif (after_time > Time.end_of_workday(after_time))
50
50
  # Ignore hours before opening and after closing
51
51
  delta = after_time - Time.end_of_workday(after_time)
52
- after_time = Time.roll_forward(after_time) + delta
52
+ after_time = Time.roll_forward(after_time, options) + delta
53
53
  end
54
54
 
55
55
  # Ignore weekends and holidays
@@ -60,20 +60,20 @@ module BusinessTime
60
60
  after_time
61
61
  end
62
62
 
63
- def calculate_before(time, hours)
63
+ def calculate_before(time, hours, options={})
64
64
  before_time = Time.roll_backward(time)
65
65
  # Step through the hours, skipping over non-business hours
66
66
  hours.times do
67
67
  before_time = before_time - 1.hour
68
68
 
69
69
  if before_time.hour == 0 && before_time.min == 0 && before_time.sec == 0
70
- before_time = Time.roll_backward(before_time - 1.second)
70
+ before_time = Time.roll_backward(before_time - 1.second, options)
71
71
  elsif (before_time <= Time.beginning_of_workday(before_time))
72
72
  # Ignore hours before opening and after closing
73
73
  delta = Time.beginning_of_workday(before_time) - before_time
74
74
 
75
75
  # Due to the 23:59:59 end-of-workday exception
76
- time_roll_backward = Time.roll_backward(before_time)
76
+ time_roll_backward = Time.roll_backward(before_time, options)
77
77
  time_roll_backward += 1.second if time_roll_backward.iso8601 =~ /23:59:59/
78
78
 
79
79
  before_time = time_roll_backward - delta
@@ -2,15 +2,15 @@
2
2
  class Date
3
3
  include BusinessTime::TimeExtensions
4
4
 
5
- def business_days_until(to_date, inclusive = false)
6
- business_dates_until(to_date, inclusive).size
5
+ def business_days_until(to_date, inclusive = false, options={})
6
+ business_dates_until(to_date, inclusive, options).size
7
7
  end
8
8
 
9
- def business_dates_until(to_date, inclusive = false)
9
+ def business_dates_until(to_date, inclusive = false, options={})
10
10
  if inclusive
11
- (self..to_date).select(&:workday?)
11
+ (self..to_date).select{|this_date| this_date.workday?(options)}
12
12
  else
13
- (self...to_date).select(&:workday?)
13
+ (self...to_date).select{|this_date| this_date.workday?(options)}
14
14
  end
15
15
  end
16
16
 
@@ -4,13 +4,13 @@
4
4
  # 3.business_days.after(some_date)
5
5
  # 4.business_hours.before(some_date_time)
6
6
  class Integer
7
- def business_hours
8
- BusinessTime::BusinessHours.new(self)
7
+ def business_hours(options={})
8
+ BusinessTime::BusinessHours.new(self, options)
9
9
  end
10
10
  alias_method :business_hour, :business_hours
11
11
 
12
- def business_days
13
- BusinessTime::BusinessDays.new(self)
12
+ def business_days(options={})
13
+ BusinessTime::BusinessDays.new(self, options)
14
14
  end
15
15
  alias_method :business_day, :business_days
16
16
  end
@@ -2,8 +2,12 @@ module BusinessTime
2
2
  module TimeExtensions
3
3
  # True if this time is on a workday (between 00:00:00 and 23:59:59), even if
4
4
  # this time falls outside of normal business hours.
5
- def workday?
6
- weekday? && !BusinessTime::Config.holidays.include?(to_date)
5
+ # holidays option allows you to pass in a different Array of holiday dates on
6
+ # each call vs the BusinessTime::Config.holidays which is always static.
7
+ def workday?(options={})
8
+ weekday? &&
9
+ !BusinessTime::Config.holidays.include?(to_date) &&
10
+ !to_array_of_dates(options[:holidays]).include?(to_date)
7
11
  end
8
12
 
9
13
  # True if this time falls on a weekday.
@@ -32,9 +36,9 @@ module BusinessTime
32
36
 
33
37
  # True if this time is on a workday (between 00:00:00 and 23:59:59), even if
34
38
  # this time falls outside of normal business hours.
35
- def workday?(day)
39
+ def workday?(day, options={})
36
40
  ActiveSupport::Deprecation.warn("`Time.workday?(time)` is deprecated. Please use `time.workday?`")
37
- day.workday?
41
+ day.workday?(options)
38
42
  end
39
43
 
40
44
  # True if this time falls on a weekday.
@@ -53,9 +57,8 @@ module BusinessTime
53
57
 
54
58
  # Rolls forward to the next beginning_of_workday
55
59
  # when the time is outside of business hours
56
- def roll_forward(time)
57
-
58
- if Time.before_business_hours?(time) || !time.workday?
60
+ def roll_forward(time, options={})
61
+ if Time.before_business_hours?(time) || !time.workday?(options)
59
62
  next_business_time = Time.beginning_of_workday(time)
60
63
  elsif Time.after_business_hours?(time) || Time.end_of_workday(time) == time
61
64
  next_business_time = Time.beginning_of_workday(time + 1.day)
@@ -63,7 +66,7 @@ module BusinessTime
63
66
  next_business_time = time.clone
64
67
  end
65
68
 
66
- while !next_business_time.workday?
69
+ while !next_business_time.workday?(options)
67
70
  next_business_time = Time.beginning_of_workday(next_business_time + 1.day)
68
71
  end
69
72
 
@@ -72,8 +75,8 @@ module BusinessTime
72
75
 
73
76
  # Returns the time parameter itself if it is a business day
74
77
  # or else returns the next business day
75
- def first_business_day(time)
76
- while !time.workday?
78
+ def first_business_day(time, options={})
79
+ while !time.workday?(options)
77
80
  time = time + 1.day
78
81
  end
79
82
 
@@ -82,8 +85,8 @@ module BusinessTime
82
85
 
83
86
  # Rolls backwards to the previous end_of_workday when the time is outside
84
87
  # of business hours
85
- def roll_backward(time)
86
- prev_business_time = if (Time.before_business_hours?(time) || !time.workday?)
88
+ def roll_backward(time, options={})
89
+ prev_business_time = if (Time.before_business_hours?(time) || !time.workday?(options))
87
90
  Time.end_of_workday(time - 1.day)
88
91
  elsif Time.after_business_hours?(time)
89
92
  Time.end_of_workday(time)
@@ -91,7 +94,7 @@ module BusinessTime
91
94
  time.clone
92
95
  end
93
96
 
94
- while !prev_business_time.workday?
97
+ while !prev_business_time.workday?(options)
95
98
  prev_business_time = Time.end_of_workday(prev_business_time - 1.day)
96
99
  end
97
100
 
@@ -100,16 +103,16 @@ module BusinessTime
100
103
 
101
104
  # Returns the time parameter itself if it is a business day
102
105
  # or else returns the previous business day
103
- def previous_business_day(time)
104
- while !time.workday?
106
+ def previous_business_day(time, options={})
107
+ while !time.workday?(options)
105
108
  time = time - 1.day
106
109
  end
107
110
 
108
111
  time
109
112
  end
110
113
 
111
- def work_hours_total(day)
112
- return 0 unless day.workday?
114
+ def work_hours_total(day, options={})
115
+ return 0 unless day.workday?(options)
113
116
 
114
117
  day = day.strftime('%a').downcase.to_sym
115
118
 
@@ -136,7 +139,7 @@ module BusinessTime
136
139
  end
137
140
  end
138
141
 
139
- def business_time_until(to_time)
142
+ def business_time_until(to_time, options={})
140
143
  # Make sure that we will calculate time from A to B "clockwise"
141
144
  if self < to_time
142
145
  time_a = self
@@ -149,8 +152,8 @@ module BusinessTime
149
152
  end
150
153
 
151
154
  # Align both times to the closest business hours
152
- time_a = Time::roll_forward(time_a)
153
- time_b = Time::roll_forward(time_b)
155
+ time_a = Time::roll_forward(time_a, options)
156
+ time_b = Time::roll_forward(time_b, options)
154
157
 
155
158
  if time_a.to_date == time_b.to_date
156
159
  time_b - time_a
@@ -166,16 +169,16 @@ module BusinessTime
166
169
  end * direction
167
170
  end
168
171
 
169
- def during_business_hours?
170
- self.workday? && self.to_i.between?(Time.beginning_of_workday(self).to_i, Time.end_of_workday(self).to_i)
172
+ def during_business_hours?(options={})
173
+ self.workday?(options) && self.to_i.between?(Time.beginning_of_workday(self).to_i, Time.end_of_workday(self).to_i)
171
174
  end
172
175
 
173
- def consecutive_workdays
174
- workday? ? consecutive_days { |date| date.workday? } : []
176
+ def consecutive_workdays(options={})
177
+ workday?(options) ? consecutive_days { |date| date.workday?(options) } : []
175
178
  end
176
179
 
177
- def consecutive_non_working_days
178
- !workday? ? consecutive_days { |date| !date.workday? } : []
180
+ def consecutive_non_working_days(options={})
181
+ !workday?(options) ? consecutive_days { |date| !date.workday?(options) } : []
179
182
  end
180
183
 
181
184
  private
@@ -194,5 +197,9 @@ module BusinessTime
194
197
  end
195
198
  (days << self).sort
196
199
  end
200
+
201
+ def to_array_of_dates(values)
202
+ Array.wrap(values).map(&:to_date)
203
+ end
197
204
  end
198
205
  end
@@ -1,3 +1,3 @@
1
1
  module BusinessTime
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.0"
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.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bokmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-07 00:00:00.000000000 Z
11
+ date: 2022-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport