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



62
63
64
# File 'lib/working_hours/config.rb', line 62

def holiday_hours
  config[:holiday_hours]
end

.holiday_hours=(val) ⇒ Object



66
67
68
69
70
71
# File 'lib/working_hours/config.rb', line 66

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

.holidaysObject



51
52
53
# File 'lib/working_hours/config.rb', line 51

def holidays
  config[:holidays]
end

.holidays=(val) ⇒ Object



55
56
57
58
59
60
# File 'lib/working_hours/config.rb', line 55

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

.precompiledObject

Returns an optimized for computing version



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/working_hours/config.rb', line 74

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



121
122
123
# File 'lib/working_hours/config.rb', line 121

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

.time_zoneObject



110
111
112
# File 'lib/working_hours/config.rb', line 110

def time_zone
  config[:time_zone]
end

.time_zone=(val) ⇒ Object



114
115
116
117
118
119
# File 'lib/working_hours/config.rb', line 114

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/working_hours/config.rb', line 125

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



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

def working_hours
  config[:working_hours]
end

.working_hours=(val) ⇒ Object



44
45
46
47
48
49
# File 'lib/working_hours/config.rb', line 44

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