class BusinessTime::Config

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.

Constants

DAY_NAMES
DEFAULT_CONFIG

Public Class Methods

beginning_of_workday(day=nil) click to toggle source

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.

# 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) click to toggle source
# File lib/business_time/config.rb, line 21
def beginning_of_workday=(time)
  config[:beginning_of_workday] = ParsedTime.parse(time)
end
default_config() click to toggle source
# File lib/business_time/config.rb, line 183
def default_config
  deep_dup(DEFAULT_CONFIG)
end
end_of_workday(day=nil) click to toggle source

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.

# 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) click to toggle source
# File lib/business_time/config.rb, line 25
def end_of_workday=(time)
  config[:end_of_workday] = ParsedTime.parse(time)
end
load(file) click to toggle source

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
# 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
weekdays() click to toggle source
# 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 = days.sort.to_set
end
with(config) { || ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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.

# File lib/business_time/config.rb, line 135
def work_week=(days)
  config[:work_week] = days
  self._weekdays = nil
end

Private Class Methods

config() click to toggle source
# File lib/business_time/config.rb, line 39
def config
  return local_config if local_config?
  Thread.main[:business_time_config] ||= default_config
end
config=(config) click to toggle source
# File lib/business_time/config.rb, line 44
def config=(config)
  return self.local_config = config if local_config?
  Thread.main[:business_time_config] = config
end
deep_dup(object) click to toggle source
# File lib/business_time/config.rb, line 208
def deep_dup(object)
  Marshal.load(Marshal.dump(object))
end
int_to_wday(num) click to toggle source
# File lib/business_time/config.rb, line 199
def int_to_wday num
  DAY_NAMES.map(&:downcase).map(&:to_sym)[num]
end
local_config() click to toggle source
# File lib/business_time/config.rb, line 49
def local_config
  local_config_stack.last
end
local_config=(config) click to toggle source
# File lib/business_time/config.rb, line 53
def local_config=(config)
  local_config_stack.last.replace(config)
end
local_config?() click to toggle source
# File lib/business_time/config.rb, line 61
def local_config?
  !local_config_stack.empty?
end
local_config_stack() click to toggle source
# File lib/business_time/config.rb, line 57
def local_config_stack
  Thread.current[:business_time_local_config] ||= []
end
reset() click to toggle source
# File lib/business_time/config.rb, line 203
def reset
  local_config_stack.clear
  self.config = default_config
end
threadsafe_cattr_accessor(name) click to toggle source
# File lib/business_time/config.rb, line 65
def threadsafe_cattr_accessor(name)
  threadsafe_cattr_reader(name)
  threadsafe_cattr_setter(name)
end
threadsafe_cattr_reader(name) click to toggle source
# File lib/business_time/config.rb, line 70
def threadsafe_cattr_reader(name)
  define_singleton_method name do
    config[name]
  end
end
threadsafe_cattr_setter(name) click to toggle source
# File lib/business_time/config.rb, line 76
def threadsafe_cattr_setter(name)
  define_singleton_method "#{name}=" do |value|
    config[name] = value
  end
end
wday_to_int(day_name) click to toggle source
# File lib/business_time/config.rb, line 194
def wday_to_int day_name
  lowercase_day_names = DAY_NAMES.map(&:downcase)
  lowercase_day_names.find_index(day_name.to_s.downcase)
end