Class: WorkingHours::Config

Inherits:
Object
  • Object
show all
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]
MIDNIGHT =
Rational('86399.999999')

Class Method Summary collapse

Class Method Details

.holiday_hoursObject



34
35
36
# File 'lib/working_hours/config.rb', line 34

def holiday_hours
  config[:holiday_hours]
end

.holiday_hours=(val) ⇒ Object



38
39
40
41
42
43
# File 'lib/working_hours/config.rb', line 38

def holiday_hours=(val)
  validate_holiday_hours! val
  config[:holiday_hours] = val
  global_config[:holiday_hours] = val
  config.delete :precompiled
end

.holidaysObject



23
24
25
# File 'lib/working_hours/config.rb', line 23

def holidays
  config[:holidays]
end

.holidays=(val) ⇒ Object



27
28
29
30
31
32
# File 'lib/working_hours/config.rb', line 27

def holidays=(val)
  validate_holidays! val
  config[:holidays] = val
  global_config[:holidays] = val
  config.delete :precompiled
end

.precompiledObject

Returns an optimized for computing version



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/working_hours/config.rb', line 46

def precompiled
  config_hash = [
    config[:working_hours],
    config[:holiday_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_holiday_hours! config[:holiday_hours]
    validate_holidays! config[:holidays]
    validate_time_zone! config[:time_zone]
    compiled = { working_hours: Array.new(7) { Hash.new }, holiday_hours: {} }
    working_hours.each do |day, hours|
      hours.each do |start, finish|
        compiled[:working_hours][DAYS_OF_WEEK.index(day)][compile_time(start)] = compile_time(finish)
      end
    end
    holiday_hours.each do |day, hours|
      compiled[:holiday_hours][day] = {}
      hours.each do |start, finish|
        compiled[:holiday_hours][day][compile_time(start)] = compile_time(finish)
      end
    end
    compiled[:holidays] = Set.new(holidays)
    compiled[:time_zone] = time_zone
    compiled
  end
end

.reset!Object



93
94
95
# File 'lib/working_hours/config.rb', line 93

def reset!
  Thread.current[:working_hours] = default_config
end

.time_zoneObject



82
83
84
# File 'lib/working_hours/config.rb', line 82

def time_zone
  config[:time_zone]
end

.time_zone=(val) ⇒ Object



86
87
88
89
90
91
# File 'lib/working_hours/config.rb', line 86

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, holiday_hours: nil, holidays: nil, time_zone: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/working_hours/config.rb', line 97

def with_config(working_hours: nil, holiday_hours: nil, holidays: nil, time_zone: nil)
  original_working_hours = self.working_hours
  original_holiday_hours = self.holiday_hours
  original_holidays = self.holidays
  original_time_zone = self.time_zone
  self.working_hours = working_hours if working_hours
  self.holiday_hours = holiday_hours if holiday_hours
  self.holidays = holidays if holidays
  self.time_zone = time_zone if time_zone
  yield
ensure
  self.working_hours = original_working_hours
  self.holiday_hours = original_holiday_hours
  self.holidays = original_holidays
  self.time_zone = original_time_zone
end

.working_hoursObject



12
13
14
# File 'lib/working_hours/config.rb', line 12

def working_hours
  config[:working_hours]
end

.working_hours=(val) ⇒ Object



16
17
18
19
20
21
# File 'lib/working_hours/config.rb', line 16

def working_hours=(val)
  validate_working_hours! val
  config[:working_hours] = val
  global_config[:working_hours] = val
  config.delete :precompiled
end