working_hours 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/README.md +15 -0
- data/lib/working_hours/config.rb +43 -15
- data/lib/working_hours/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d51f59c3456e35117b42605450d911595a331ef6a72303aba33a6eedcdabd6f
|
4
|
+
data.tar.gz: '0527249d73eb1da76205e5b02bd7d67226a105c91f2a14240ddfd39abc0daeb6'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b276429238c21dbb526c53d4be5082450c99b5c5414d9b21786ec4278ef83ff07c6e2a952cdf6bbb9125f0b32b013f7d2b009cadb020b062a780dcf119d733aa
|
7
|
+
data.tar.gz: 77524f554091fccfdd9b96ea0ff64295fb55a8358f010adac5f008455428fd91c3b8ef80111f515529f2059b4e3db6081177b3563b2c16c83ee5d6e829e27789
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# Unreleased
|
2
2
|
|
3
|
-
[Compare master with v1.4.
|
3
|
+
[Compare master with v1.4.1](https://github.com/intrepidd/working_hours/compare/v1.4.1...master)
|
4
|
+
|
5
|
+
# v1.4.1
|
6
|
+
* Add InvalidConfiguration error code to allow custom message or behavior - [#47](https://github.com/Intrepidd/working_hours/pull/47)
|
4
7
|
|
5
8
|
# v1.4.0
|
6
9
|
* New config option: holiday_hours - [#37](https://github.com/Intrepidd/working_hours/pull/37)
|
data/README.md
CHANGED
@@ -116,6 +116,21 @@ If *any* hours are set for a calendar day in `holiday_hours`, then the `working_
|
|
116
116
|
WorkingHours::Config.holiday_hours = {Date.new(2020, 12, 24) => {'09:00' => '12:00', '13:00' => '15:00'}}
|
117
117
|
```
|
118
118
|
|
119
|
+
### Handling errors
|
120
|
+
|
121
|
+
If the configuration is erroneous, an ``WorkingHours::InvalidConfiguration`` exception will be raised containing the appropriate error message.
|
122
|
+
|
123
|
+
You can also access the error code in case you want to implement custom behavior or changing one specific message, e.g:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
rescue WorkingHours::InvalidConfiguration => e
|
127
|
+
if e.error_code == :empty
|
128
|
+
raise StandardError.new "Config is required"
|
129
|
+
end
|
130
|
+
raise e
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
119
134
|
## No core extensions / monkey patching
|
120
135
|
|
121
136
|
Core extensions (monkey patching to add methods on Time, Date, Numbers, etc.) are handy but not appreciated by everyone. WorkingHours can also be used **without any monkey patching**:
|
data/lib/working_hours/config.rb
CHANGED
@@ -1,7 +1,35 @@
|
|
1
1
|
require 'set'
|
2
2
|
|
3
3
|
module WorkingHours
|
4
|
-
InvalidConfiguration
|
4
|
+
class InvalidConfiguration < StandardError
|
5
|
+
attr_reader :data, :error_code
|
6
|
+
|
7
|
+
def initialize(error_code, data: nil)
|
8
|
+
@data = data
|
9
|
+
@error_code = error_code
|
10
|
+
super compose_message(error_code)
|
11
|
+
end
|
12
|
+
|
13
|
+
def compose_message(error_code)
|
14
|
+
case error_code
|
15
|
+
when :empty then "No working hours given"
|
16
|
+
when :empty_day then "No working hours given for day `#{@data[:day]}`"
|
17
|
+
when :holidays_not_array then "Invalid type for holidays: #{@data[:holidays_class]} - must act like an array"
|
18
|
+
when :holiday_not_date then "Invalid holiday: #{@data[:day]} - must be Date"
|
19
|
+
when :invalid_day_keys then "Invalid day identifier(s): #{@data[:invalid_keys]} - must be 3 letter symbols"
|
20
|
+
when :invalid_format then "Invalid time: #{@data[:time]} - must be 'HH:MM(:SS)'"
|
21
|
+
when :invalid_holiday_keys then "Invalid day identifier(s): #{@data[:invalid_keys]} - must be a Date object"
|
22
|
+
when :invalid_timezone then "Invalid time zone: #{@data[:zone]} - must be String or ActiveSupport::TimeZone"
|
23
|
+
when :invalid_type then "Invalid type for `#{@data[:day]}`: #{@data[:hours_class]} - must be Hash"
|
24
|
+
when :outside_of_day then "Invalid time: #{@data[:time]} - outside of day"
|
25
|
+
when :overlap then "Invalid range: #{@data[:start]} => #{@data[:finish]} - overlaps previous range"
|
26
|
+
when :unknown_timezone then "Unknown time zone: #{@data[:zone]}"
|
27
|
+
when :wrong_order then "Invalid range: #{@data[:start]} => #{@data[:finish]} - ends before it starts"
|
28
|
+
else "Invalid Configuration"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
5
33
|
|
6
34
|
class Config
|
7
35
|
TIME_FORMAT = /\A([0-2][0-9])\:([0-5][0-9])(?:\:([0-5][0-9]))?\z/
|
@@ -149,22 +177,22 @@ module WorkingHours
|
|
149
177
|
def validate_hours! dates
|
150
178
|
dates.each do |day, hours|
|
151
179
|
if not hours.is_a? Hash
|
152
|
-
raise InvalidConfiguration.new
|
180
|
+
raise InvalidConfiguration.new :invalid_type, data: { day: day, hours_class: hours.class }
|
153
181
|
elsif hours.empty?
|
154
|
-
raise InvalidConfiguration.new
|
182
|
+
raise InvalidConfiguration.new :empty_day, data: { day: day }
|
155
183
|
end
|
156
184
|
last_time = nil
|
157
185
|
hours.sort.each do |start, finish|
|
158
186
|
if not start =~ TIME_FORMAT
|
159
|
-
raise InvalidConfiguration.new
|
187
|
+
raise InvalidConfiguration.new :invalid_format, data: { time: start }
|
160
188
|
elsif not finish =~ TIME_FORMAT
|
161
|
-
raise InvalidConfiguration.new
|
189
|
+
raise InvalidConfiguration.new :invalid_format, data: { time: finish }
|
162
190
|
elsif compile_time(finish) >= 24 * 60 * 60
|
163
|
-
raise InvalidConfiguration.new
|
191
|
+
raise InvalidConfiguration.new :outside_of_day, data: { time: finish }
|
164
192
|
elsif start >= finish
|
165
|
-
raise InvalidConfiguration.new
|
193
|
+
raise InvalidConfiguration.new :wrong_order, data: { start: start, finish: finish }
|
166
194
|
elsif last_time and start < last_time
|
167
|
-
raise InvalidConfiguration.new
|
195
|
+
raise InvalidConfiguration.new :overlap, data: { start: start, finish: finish }
|
168
196
|
end
|
169
197
|
last_time = finish
|
170
198
|
end
|
@@ -173,28 +201,28 @@ module WorkingHours
|
|
173
201
|
|
174
202
|
def validate_working_hours! week
|
175
203
|
if week.empty?
|
176
|
-
raise InvalidConfiguration.new
|
204
|
+
raise InvalidConfiguration.new :empty
|
177
205
|
end
|
178
206
|
if (invalid_keys = (week.keys - DAYS_OF_WEEK)).any?
|
179
|
-
raise InvalidConfiguration.new
|
207
|
+
raise InvalidConfiguration.new :invalid_day_keys, data: { invalid_keys: invalid_keys.join(', ') }
|
180
208
|
end
|
181
209
|
validate_hours!(week)
|
182
210
|
end
|
183
211
|
|
184
212
|
def validate_holiday_hours! days
|
185
213
|
if (invalid_keys = (days.keys.reject{ |day| day.is_a?(Date) })).any?
|
186
|
-
raise InvalidConfiguration.new
|
214
|
+
raise InvalidConfiguration.new :invalid_holiday_keys, data: { invalid_keys: invalid_keys.join(', ') }
|
187
215
|
end
|
188
216
|
validate_hours!(days)
|
189
217
|
end
|
190
218
|
|
191
219
|
def validate_holidays! holidays
|
192
220
|
if not holidays.respond_to?(:to_a)
|
193
|
-
raise InvalidConfiguration.new
|
221
|
+
raise InvalidConfiguration.new :holidays_not_array, data: { holidays_class: holidays.class }
|
194
222
|
end
|
195
223
|
holidays.to_a.each do |day|
|
196
224
|
if not day.is_a? Date
|
197
|
-
raise InvalidConfiguration.new
|
225
|
+
raise InvalidConfiguration.new :holiday_not_date, data: { day: day }
|
198
226
|
end
|
199
227
|
end
|
200
228
|
end
|
@@ -203,12 +231,12 @@ module WorkingHours
|
|
203
231
|
if zone.is_a? String
|
204
232
|
res = ActiveSupport::TimeZone[zone]
|
205
233
|
if res.nil?
|
206
|
-
raise InvalidConfiguration.new
|
234
|
+
raise InvalidConfiguration.new :unknown_timezone, data: { zone: zone }
|
207
235
|
end
|
208
236
|
elsif zone.is_a? ActiveSupport::TimeZone
|
209
237
|
res = zone
|
210
238
|
else
|
211
|
-
raise InvalidConfiguration.new
|
239
|
+
raise InvalidConfiguration.new :invalid_timezone, data: { zone: zone.inspect }
|
212
240
|
end
|
213
241
|
res
|
214
242
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: working_hours
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adrien Jarthon
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-07-
|
12
|
+
date: 2021-07-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|