biz 1.6.1 → 1.7.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
  SHA1:
3
- metadata.gz: b83065efe295ea89da135b593191120dc58bcf6d
4
- data.tar.gz: 97b8e746410d76e35145b0e41920a0ba04801e9d
3
+ metadata.gz: dc2d88c632e4f3c395997448b3d864883700c8c9
4
+ data.tar.gz: 999e147f312f256325ddf28928d143b863961b80
5
5
  SHA512:
6
- metadata.gz: 5e8f3d0ae0c724ade11514895b7ce160d6303b76679bafb38d2ff6daf03b469f0dd47789696a4e58009f1ef2608d96f63af5abeee888b7fe5954d305927e4e58
7
- data.tar.gz: 63c26133dd00077646bf011d34e22f5b869498c30ba13327e224bf2023c23e3d332e3a59ccd0424b39ea2be1256fda39ab347358a1681fd4c7d9441601759eaa
6
+ metadata.gz: 9d9f6d4c2dc29cda4b2a4d589400840effcaec6a6ec104e5ee80744db56fdecd6d799a28a82ce83ceca948afb93685892e1141c737507a80f43c76f3bb00b104
7
+ data.tar.gz: 450b3f8f0d3052980e9cf815f63159c5e3a6a83a38bd553622c129d66a81cb1ed454962b878fe3d362859b1b191592c6263c23f65e529de3b0835cbef3c82b7a
data/README.md CHANGED
@@ -77,6 +77,13 @@ end
77
77
  Note that times must be specified in 24-hour clock format and time zones
78
78
  must be [IANA identifiers](http://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
79
79
 
80
+ If you're operating in a threaded environment and want to use the same
81
+ configuration across threads, save the configured schedule as a global variable:
82
+
83
+ ```ruby
84
+ $biz = Biz::Schedule.new
85
+ ```
86
+
80
87
  ## Usage
81
88
 
82
89
  ```ruby
@@ -89,9 +96,21 @@ Biz.time(2, :hours).after(Time.utc(2015, 12, 25, 9, 30))
89
96
  # Calculations can be performed in seconds, minutes, hours, or days
90
97
  Biz.time(1, :day).after(Time.utc(2015, 1, 8, 10))
91
98
 
99
+ # Find the previous business time
100
+ Biz.time(0, :hours).before(Time.utc(2016, 1, 8, 6))
101
+
102
+ # Find the next business time
103
+ Biz.time(0, :hours).after(Time.utc(2016, 1, 8, 20))
104
+
92
105
  # Find the amount of business time between two times
93
106
  Biz.within(Time.utc(2015, 3, 7), Time.utc(2015, 3, 14)).in_seconds
94
107
 
108
+ # Find the start of the business day
109
+ Biz.periods.on(Date.today).first.start_time
110
+
111
+ # Find the end of the business day
112
+ Biz.periods.on(Date.today).to_a.last.end_time
113
+
95
114
  # Determine if a time is in business hours
96
115
  Biz.in_hours?(Time.utc(2015, 1, 10, 9))
97
116
 
@@ -102,6 +121,14 @@ Biz.on_break?(Time.utc(2016, 6, 3))
102
121
  Biz.on_holiday?(Time.utc(2014, 1, 1))
103
122
  ```
104
123
 
124
+ The same methods can be called on a configured instance:
125
+
126
+ ```ruby
127
+ schedule = Biz::Schedule.new
128
+
129
+ schedule.in_hours?(Time.utc(2015, 1, 1, 10))
130
+ ```
131
+
105
132
  All returned times are in UTC.
106
133
 
107
134
  If a schedule will be configured with a large number of holidays and performance
@@ -289,7 +316,7 @@ To open a console with the gem and sample schedule loaded:
289
316
 
290
317
  ## Copyright and license
291
318
 
292
- Copyright 2015-16 Zendesk
319
+ Copyright 2015-17 Zendesk
293
320
 
294
321
  Licensed under the Apache License, Version 2.0 (the "License"); you may not use
295
322
  this gem except in compliance with the License.
@@ -20,6 +20,68 @@ module Biz
20
20
  name.split('::').last.downcase.to_sym
21
21
  end
22
22
 
23
+ def self.time_class
24
+ Class.new(self) do
25
+ def before(time)
26
+ return moment_before(time) if scalar.zero?
27
+
28
+ advanced_periods(:before, time).last.start_time
29
+ end
30
+
31
+ def after(time)
32
+ return moment_after(time) if scalar.zero?
33
+
34
+ advanced_periods(:after, time).last.end_time
35
+ end
36
+
37
+ private
38
+
39
+ def advanced_periods(direction, time)
40
+ schedule
41
+ .periods
42
+ .public_send(direction, time)
43
+ .timeline
44
+ .for(Duration.public_send(unit, scalar))
45
+ .to_a
46
+ end
47
+ end
48
+ end
49
+
50
+ def self.day_class
51
+ Class.new(self) do
52
+ def before(time)
53
+ return moment_before(time) if scalar.zero?
54
+
55
+ periods(:before, time).first.end_time
56
+ end
57
+
58
+ def after(time)
59
+ return moment_after(time) if scalar.zero?
60
+
61
+ periods(:after, time).first.start_time
62
+ end
63
+
64
+ private
65
+
66
+ def periods(direction, time)
67
+ schedule.periods.public_send(
68
+ direction,
69
+ advanced_time(direction, schedule.in_zone.local(time))
70
+ )
71
+ end
72
+
73
+ def advanced_time(direction, time)
74
+ schedule.in_zone.on_date(
75
+ schedule.dates.days(scalar).public_send(direction, time),
76
+ DayTime.from_time(time)
77
+ )
78
+ end
79
+ end
80
+ end
81
+
82
+ private_class_method :time_class,
83
+ :day_class
84
+
23
85
  def initialize(schedule, scalar)
24
86
  @schedule = schedule
25
87
  @scalar = Integer(scalar)
@@ -48,68 +110,9 @@ module Biz
48
110
 
49
111
  [
50
112
  *%i[second seconds minute minutes hour hours].map { |unit|
51
- const_set(
52
- unit.to_s.capitalize,
53
- Class.new(self) do
54
- def before(time)
55
- return moment_before(time) if scalar.zero?
56
-
57
- advanced_periods(:before, time).last.start_time
58
- end
59
-
60
- def after(time)
61
- return moment_after(time) if scalar.zero?
62
-
63
- advanced_periods(:after, time).last.end_time
64
- end
65
-
66
- private
67
-
68
- def advanced_periods(direction, time)
69
- schedule
70
- .periods
71
- .public_send(direction, time)
72
- .timeline
73
- .for(Duration.public_send(unit, scalar))
74
- .to_a
75
- end
76
- end
77
- )
113
+ const_set(unit.to_s.capitalize, time_class)
78
114
  },
79
- *%i[day days].map { |unit|
80
- const_set(
81
- unit.to_s.capitalize,
82
- Class.new(self) do
83
- def before(time)
84
- return moment_before(time) if scalar.zero?
85
-
86
- periods(:before, time).first.end_time
87
- end
88
-
89
- def after(time)
90
- return moment_after(time) if scalar.zero?
91
-
92
- periods(:after, time).first.start_time
93
- end
94
-
95
- private
96
-
97
- def periods(direction, time)
98
- schedule.periods.public_send(
99
- direction,
100
- advanced_time(direction, schedule.in_zone.local(time))
101
- )
102
- end
103
-
104
- def advanced_time(direction, time)
105
- schedule.in_zone.on_date(
106
- schedule.dates.days(scalar).public_send(direction, time),
107
- DayTime.from_time(time)
108
- )
109
- end
110
- end
111
- )
112
- }
115
+ *%i[day days].map { |unit| const_set(unit.to_s.capitalize, day_class) }
113
116
  ].each do |unit_class|
114
117
  define_singleton_method(unit_class.unit) { |schedule, scalar|
115
118
  unit_class.new(schedule, scalar)
@@ -14,6 +14,14 @@ module Biz
14
14
  Before.new(schedule, origin)
15
15
  end
16
16
 
17
+ def on(date)
18
+ schedule
19
+ .periods
20
+ .after(schedule.in_zone.on_date(date, DayTime.midnight))
21
+ .timeline
22
+ .until(schedule.in_zone.on_date(date, DayTime.endnight))
23
+ end
24
+
17
25
  protected
18
26
 
19
27
  attr_reader :schedule
@@ -1,3 +1,3 @@
1
1
  module Biz
2
- VERSION = '1.6.1'.freeze
2
+ VERSION = '1.7.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biz
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Little
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-01-05 00:00:00.000000000 Z
12
+ date: 2017-06-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: clavius
@@ -39,34 +39,6 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: rake
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - "~>"
47
- - !ruby/object:Gem::Version
48
- version: '12.0'
49
- type: :development
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - "~>"
54
- - !ruby/object:Gem::Version
55
- version: '12.0'
56
- - !ruby/object:Gem::Dependency
57
- name: rspec
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - "~>"
61
- - !ruby/object:Gem::Version
62
- version: '3.0'
63
- type: :development
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: '3.0'
70
42
  description: Time calculations using business hours.
71
43
  email:
72
44
  - clittle@zendesk.com
@@ -128,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
100
  requirements:
129
101
  - - ">="
130
102
  - !ruby/object:Gem::Version
131
- version: '2.1'
103
+ version: '2.2'
132
104
  required_rubygems_version: !ruby/object:Gem::Requirement
133
105
  requirements:
134
106
  - - ">="