working_hours 1.4.0 → 1.4.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
  SHA256:
3
- metadata.gz: d2f3a760854612e389ff263f670c2ddbd3206c5895f13a753267e44a92ca12f1
4
- data.tar.gz: f9f30c0aaf3c7004a68b7f18ef39767e30c497bedc14361c2ad450894272997e
3
+ metadata.gz: 9d51f59c3456e35117b42605450d911595a331ef6a72303aba33a6eedcdabd6f
4
+ data.tar.gz: '0527249d73eb1da76205e5b02bd7d67226a105c91f2a14240ddfd39abc0daeb6'
5
5
  SHA512:
6
- metadata.gz: c63b351486621dca69deb0b827dc51024f74b4304e6639c9272b01d5a5fd9a58013e1d9874ed13466ce6f5fb60ccab67c9f2712a0af2b4d5e4a94bbf214ac996
7
- data.tar.gz: 8662b6a40f4ee6bc734cd2f87eecf1e6b51a474638dc5b9a867525cbdc0c7a4b329a34054fff395e71e36b5b93e26d01d3205b2965fd0573c70bbe764c1e450b
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.0](https://github.com/intrepidd/working_hours/compare/v1.4.0...master)
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**:
@@ -1,7 +1,35 @@
1
1
  require 'set'
2
2
 
3
3
  module WorkingHours
4
- InvalidConfiguration = Class.new StandardError
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 "Invalid type for `#{day}`: #{hours.class} - must be Hash"
180
+ raise InvalidConfiguration.new :invalid_type, data: { day: day, hours_class: hours.class }
153
181
  elsif hours.empty?
154
- raise InvalidConfiguration.new "No working hours given for day `#{day}`"
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 "Invalid time: #{start} - must be 'HH:MM(:SS)'"
187
+ raise InvalidConfiguration.new :invalid_format, data: { time: start }
160
188
  elsif not finish =~ TIME_FORMAT
161
- raise InvalidConfiguration.new "Invalid time: #{finish} - must be 'HH:MM(:SS)'"
189
+ raise InvalidConfiguration.new :invalid_format, data: { time: finish }
162
190
  elsif compile_time(finish) >= 24 * 60 * 60
163
- raise InvalidConfiguration.new "Invalid time: #{finish} - outside of day"
191
+ raise InvalidConfiguration.new :outside_of_day, data: { time: finish }
164
192
  elsif start >= finish
165
- raise InvalidConfiguration.new "Invalid range: #{start} => #{finish} - ends before it starts"
193
+ raise InvalidConfiguration.new :wrong_order, data: { start: start, finish: finish }
166
194
  elsif last_time and start < last_time
167
- raise InvalidConfiguration.new "Invalid range: #{start} => #{finish} - overlaps previous range"
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 "No working hours given"
204
+ raise InvalidConfiguration.new :empty
177
205
  end
178
206
  if (invalid_keys = (week.keys - DAYS_OF_WEEK)).any?
179
- raise InvalidConfiguration.new "Invalid day identifier(s): #{invalid_keys.join(', ')} - must be 3 letter symbols"
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 "Invalid day identifier(s): #{invalid_keys.join(', ')} - must be a Date object"
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 "Invalid type for holidays: #{holidays.class} - must act like an array"
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 "Invalid holiday: #{day} - must be Date"
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 "Unknown time zone: #{zone}"
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 "Invalid time zone: #{zone.inspect} - must be String or ActiveSupport::TimeZone"
239
+ raise InvalidConfiguration.new :invalid_timezone, data: { zone: zone.inspect }
212
240
  end
213
241
  res
214
242
  end
@@ -1,3 +1,3 @@
1
1
  module WorkingHours
2
- VERSION = "1.4.0"
2
+ VERSION = "1.4.1"
3
3
  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.0
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-05 00:00:00.000000000 Z
12
+ date: 2021-07-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport