Class: WorkingHours::Config
- Inherits:
-
Object
- Object
- WorkingHours::Config
- Defined in:
- lib/working_hours/config.rb
Constant Summary collapse
- TIME_FORMAT =
/\A([0-2][0-9])\:([0-5][0-9])(?:\:([0-5][0-9]))?\z/
- DAYS_OF_WEEK =
[:sun, :mon, :tue, :wed, :thu, :fri, :sat]
Class Method Summary collapse
- .holidays ⇒ Object
- .holidays=(val) ⇒ Object
-
.precompiled ⇒ Object
Returns an optimized for computing version.
- .reset! ⇒ Object
- .time_zone ⇒ Object
- .time_zone=(val) ⇒ Object
- .with_config(working_hours: nil, holidays: nil, time_zone: nil) ⇒ Object
- .working_hours ⇒ Object
- .working_hours=(val) ⇒ Object
Class Method Details
.holidays ⇒ Object
24 25 26 |
# File 'lib/working_hours/config.rb', line 24 def holidays config[:holidays] end |
.holidays=(val) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/working_hours/config.rb', line 28 def holidays=(val) validate_holidays! val config[:holidays] = val global_config[:holidays] = val config.delete :precompiled end |
.precompiled ⇒ Object
Returns an optimized for computing version
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/working_hours/config.rb', line 36 def precompiled config_hash = [config[:working_hours], config[:holidays], config[:time_zone]].hash if config_hash != config[:config_hash] config[:config_hash] = config_hash config.delete :precompiled end config[:precompiled] ||= begin validate_working_hours! config[:working_hours] validate_holidays! config[:holidays] validate_time_zone! config[:time_zone] compiled = {working_hours: []} working_hours.each do |day, hours| compiled[:working_hours][DAYS_OF_WEEK.index(day)] = {} hours.each do |start, finish| compiled[:working_hours][DAYS_OF_WEEK.index(day)][compile_time(start)] = compile_time(finish) end end compiled[:holidays] = Set.new(holidays) compiled[:time_zone] = time_zone compiled end end |
.reset! ⇒ Object
71 72 73 |
# File 'lib/working_hours/config.rb', line 71 def reset! Thread.current[:working_hours] = default_config end |
.time_zone ⇒ Object
60 61 62 |
# File 'lib/working_hours/config.rb', line 60 def time_zone config[:time_zone] end |
.time_zone=(val) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/working_hours/config.rb', line 64 def time_zone=(val) zone = validate_time_zone! val config[:time_zone] = zone global_config[:time_zone] = val config.delete :precompiled end |
.with_config(working_hours: nil, holidays: nil, time_zone: nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/working_hours/config.rb', line 75 def with_config(working_hours: nil, holidays: nil, time_zone: nil) original_working_hours = self.working_hours original_holidays = self.holidays original_time_zone = self.time_zone self.working_hours = working_hours if working_hours self.holidays = holidays if holidays self.time_zone = time_zone if time_zone yield ensure self.working_hours = original_working_hours self.holidays = original_holidays self.time_zone = original_time_zone end |
.working_hours ⇒ Object
13 14 15 |
# File 'lib/working_hours/config.rb', line 13 def working_hours config[:working_hours] end |
.working_hours=(val) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/working_hours/config.rb', line 17 def working_hours=(val) validate_working_hours! val config[:working_hours] = val global_config[:working_hours] = val config.delete :precompiled end |