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.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.beginning_of_workdayObject

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.



14
15
16
# File 'lib/business_time/config.rb', line 14

def beginning_of_workday
  @beginning_of_workday
end

.end_of_workdayObject

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.



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

def end_of_workday
  @end_of_workday
end

.holidaysObject

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

BusinessTime::Config.holidays << my_holiday_date_object

someplace in the initializers of your application.



32
33
34
# File 'lib/business_time/config.rb', line 32

def holidays
  @holidays
end

.work_weekObject

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.



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

def work_week
  @work_week
end

Class Method Details

.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


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/business_time/config.rb', line 69

def self.load(file)
  self.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)
  config_vars.each do |var|
    send("#{var}=", config[var]) if config[var] && respond_to?("#{var}=")
  end

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

.resetObject



52
53
54
55
56
57
58
# File 'lib/business_time/config.rb', line 52

def self.reset
  self.holidays = []
  self.beginning_of_workday = "9:00 am"
  self.end_of_workday = "5:00 pm"
  self.work_week = %w[mon tue wed thu fri]
  @weekdays = nil
end

.weekdaysObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/business_time/config.rb', line 41

def self.weekdays
  return @weekdays unless @weekdays.nil?

  lowercase_day_names = ::Time::RFC2822_DAY_NAME.map(&:downcase)

  @weekdays = work_week.each_with_object([]) do |day_name, days|
    day_num = lowercase_day_names.find_index(day_name.to_s.downcase)
    days << day_num unless day_num.nil?
  end
end