Class: BusinessTime::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/business_time/config.rb

Overview

controls the behavior of this gem. You can change the beginning_of_workday, end_of_workday, and the list of holidays manually, or with a yaml file and the load method.

Constant Summary collapse

DEFAULT_CONFIG =
{
  holidays:              SortedSet.new,
  beginning_of_workday:  ParsedTime.parse('9:00 am'),
  end_of_workday:        ParsedTime.parse('5:00 pm'),
  work_week:             %w(mon tue wed thu fri),
  work_hours:            {},
  work_hours_total:      {},
  _weekdays:             nil,
  fiscal_month_offset:   10,
}

Class Method Summary collapse

Class Method Details

.beginning_of_workday(day = nil) ⇒ Object

You can set this yourself, either by the load method below, or by saying

BusinessTime::Config.beginning_of_workday = "8:30 am"

someplace in the initializers of your application.



122
123
124
125
126
127
128
129
# File 'lib/business_time/config.rb', line 122

def beginning_of_workday(day=nil)
  if day
    wday = work_hours[int_to_wday(day.wday)]
    wday ? wday.first : config[:beginning_of_workday]
  else
    config[:beginning_of_workday]
  end
end

.beginning_of_workday=(time) ⇒ Object



21
22
23
# File 'lib/business_time/config.rb', line 21

def beginning_of_workday=(time)
  config[:beginning_of_workday] = ParsedTime.parse(time)
end

.default_configObject



183
184
185
# File 'lib/business_time/config.rb', line 183

def default_config
  deep_dup(DEFAULT_CONFIG)
end

.end_of_workday(day = nil) ⇒ Object

You can set this yourself, either by the load method below, or by saying

BusinessTime::Config.end_of_workday = "5:30 pm"

someplace in the initializers of your application.



109
110
111
112
113
114
115
116
# File 'lib/business_time/config.rb', line 109

def end_of_workday(day=nil)
  if day
    wday = work_hours[int_to_wday(day.wday)]
    wday ? (wday.last == ParsedTime.new(0, 0) ? ParsedTime.new(23, 59, 59) : wday.last) : config[:end_of_workday]
  else
    config[:end_of_workday]
  end
end

.end_of_workday=(time) ⇒ Object



25
26
27
# File 'lib/business_time/config.rb', line 25

def end_of_workday=(time)
  config[:end_of_workday] = ParsedTime.parse(time)
end

.load(file) ⇒ Object

loads the config data from a yaml file written as:

business_time:
  beginning_od_workday: 8:30 am
  end_of_workday: 5:30 pm
  holidays:
    - Jan 1st, 2010
    - July 4th, 2010
    - Dec 25th, 2010


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/business_time/config.rb', line 159

def load(file)
  reset
  data = YAML::load(file.respond_to?(:read) ? file : File.open(file))
  config = (data["business_time"] || {})

  # load each config variable from the file, if it's present and valid
  config_vars = %w(beginning_of_workday end_of_workday work_week work_hours)
  config_vars.each do |var|
    send("#{var}=", config[var]) if config[var] && respond_to?("#{var}=")
  end

  (config["holidays"] || []).each do |holiday|
    holidays << Date.parse(holiday)
  end
end

.weekdaysObject



140
141
142
143
144
145
146
147
148
# File 'lib/business_time/config.rb', line 140

def weekdays
  return _weekdays unless _weekdays.nil?

  days = (!work_hours.empty? ? work_hours.keys : work_week).map do |day_name|
    wday_to_int(day_name)
  end.compact

  self._weekdays = SortedSet.new(days)
end

.with(config) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/business_time/config.rb', line 175

def with(config)
  local_config_stack.push(config().dup)
  config.each { |k,v| send("#{k}=", v) } # calculations are done on setting
  yield
ensure
  local_config_stack.pop
end

.work_hours=(work_hours) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/business_time/config.rb', line 29

def work_hours=(work_hours)
  work_hours.each_with_object(config[:work_hours] = {}) do |(day, hours), c|
    c[day] = hours.map do |time|
      ParsedTime.parse(time)
    end
  end
end

.work_week=(days) ⇒ Object

You can set this yourself, either by the load method below, or by saying

BusinessTime::Config.work_week = [:sun, :mon, :tue, :wed, :thu]

someplace in the initializers of your application.



135
136
137
138
# File 'lib/business_time/config.rb', line 135

def work_week=(days)
  config[:work_week] = days
  self._weekdays = nil
end