Skip to content

Commit

Permalink
Merge pull request #18 from hmarr/holiday-yaml-fix
Browse files Browse the repository at this point in the history
Fix YAML config loading
  • Loading branch information
hmarr committed Jul 9, 2012
2 parents 49e9db7 + 317b760 commit ff4b579
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
44 changes: 24 additions & 20 deletions lib/business_time/config.rb
@@ -1,7 +1,7 @@
require 'active_support/core_ext'

module BusinessTime

# 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.
Expand All @@ -12,51 +12,51 @@ class << self
# BusinessTime::Config.beginning_of_workday = "8:30 am"
# someplace in the initializers of your application.
attr_accessor :beginning_of_workday

# 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.
attr_accessor :end_of_workday

# 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.
attr_accessor :work_week

# 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.
attr_accessor :holidays

end

def self.work_week=(days)
@work_week = days
@weekdays = nil
end

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

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

# loads the config data from a yaml file written as:
#
# business_time:
Expand All @@ -66,20 +66,24 @@ def self.reset
# - Jan 1st, 2010
# - July 4th, 2010
# - Dec 25th, 2010
def self.load(filename)
def self.load(file)
self.reset
data = YAML::load(File.open(filename))
self.beginning_of_workday = data["business_time"]["beginning_of_workday"]
self.end_of_workday = data["business_time"]["end_of_workday"]
self.work_week = data["business_time"]["work_week"]
data["business_time"]["holidays"].each do |holiday|
self.holidays << (Time.zone ? Time.zone.parse(holiday) : Time.parse(holiday))
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

#reset the first time we are loaded.
self.reset
end

end
55 changes: 49 additions & 6 deletions test/test_config.rb
@@ -1,38 +1,81 @@
require 'helper'

class TestConfig < Test::Unit::TestCase

should "keep track of the start of the day" do
assert_equal "9:00 am", BusinessTime::Config.beginning_of_workday
BusinessTime::Config.beginning_of_workday = "8:30 am"
assert_equal "8:30 am", BusinessTime::Config.beginning_of_workday
end

should "keep track of the end of the day" do
assert_equal "5:00 pm", BusinessTime::Config.end_of_workday
BusinessTime::Config.end_of_workday = "5:30 pm"
assert_equal "5:30 pm", BusinessTime::Config.end_of_workday
end

should "keep track of holidays" do
assert BusinessTime::Config.holidays.empty?
daves_birthday = Date.parse("August 4th, 1969")
BusinessTime::Config.holidays << daves_birthday
assert BusinessTime::Config.holidays.include?(daves_birthday)
end

should "keep track of work week" do
assert_equal %w[mon tue wed thu fri], BusinessTime::Config.work_week
BusinessTime::Config.work_week = %w[sun mon tue wed thu]
assert_equal %w[sun mon tue wed thu], BusinessTime::Config.work_week
end

should "map work week to weekdays" do
assert_equal [1,2,3,4,5], BusinessTime::Config.weekdays
BusinessTime::Config.work_week = %w[sun mon tue wed thu]
assert_equal [0,1,2,3,4], BusinessTime::Config.weekdays
BusinessTime::Config.work_week = %w[tue wed] # Hey, we got it made!
assert_equal [2,3], BusinessTime::Config.weekdays
end


should "load config from YAML files" do
yaml = <<-YAML
business_time:
beginning_of_workday: 11:00 am
end_of_workday: 2:00 pm
work_week:
- mon
holidays:
- December 25th, 2012
YAML
config_file = StringIO.new(yaml.gsub!(/^ /, ''))
BusinessTime::Config.load(config_file)
assert_equal "11:00 am", BusinessTime::Config.beginning_of_workday
assert_equal "2:00 pm", BusinessTime::Config.end_of_workday
assert_equal ['mon'], BusinessTime::Config.work_week
assert_equal [Time.parse('2012-12-25')], BusinessTime::Config.holidays
end

should "include holidays read from YAML config files" do
yaml = <<-YAML
business_time:
holidays:
- May 7th, 2012
YAML
assert Time.workday?(Time.parse('2012-05-07'))
config_file = StringIO.new(yaml.gsub!(/^ /, ''))
BusinessTime::Config.load(config_file)
assert !Time.workday?(Time.parse('2012-05-07'))
end

should "use defaults for values missing in YAML file" do
yaml = <<-YAML
business_time:
missing_values: yup
YAML
config_file = StringIO.new(yaml.gsub!(/^ /, ''))
BusinessTime::Config.load(config_file)
assert_equal "9:00 am", BusinessTime::Config.beginning_of_workday
assert_equal "5:00 pm", BusinessTime::Config.end_of_workday
assert_equal %w[mon tue wed thu fri], BusinessTime::Config.work_week
assert_equal [], BusinessTime::Config.holidays
end

end

0 comments on commit ff4b579

Please sign in to comment.