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_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.



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

def beginning_of_workday
  @beginning_of_workday
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.



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_hoursObject

working hours for each day - if not set using global variables :beginning_of_workday and end_of_workday. Keys will be added ad weekdays. Example:

{:mon => ["9:00","17:00"],:tue => ["9:00","17:00"].....}


38
39
40
# File 'lib/business_time/config.rb', line 38

def work_hours
  @work_hours
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

.int_to_wday(num) ⇒ Object



79
80
81
# File 'lib/business_time/config.rb', line 79

def self.int_to_wday num
  ::Time::RFC2822_DAY_NAME.map(&:downcase).map(&:to_sym)[num]
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


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/business_time/config.rb', line 101

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 work_hours)
  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



83
84
85
86
87
88
89
90
# File 'lib/business_time/config.rb', line 83

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]
  self.work_hours = {}
  @weekdays = nil
end

.wday_to_int(day_name) ⇒ Object



74
75
76
77
# File 'lib/business_time/config.rb', line 74

def self.wday_to_int day_name
  lowercase_day_names = ::Time::RFC2822_DAY_NAME.map(&:downcase)
  lowercase_day_names.find_index(day_name.to_s.downcase)
end

.weekdaysObject



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

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

  @weekdays = (!work_hours.empty? ? work_hours.keys : work_week).each_with_object([]) do |day_name, days|
    day_num = self.wday_to_int(day_name)
    days << day_num unless day_num.nil?
  end
end