Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forced workdays #182

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/business_time/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module BusinessTime
class Config
DEFAULT_CONFIG = {
holidays: SortedSet.new,
forced_workdays: 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),
Expand Down Expand Up @@ -104,6 +105,12 @@ def threadsafe_cattr_setter(name)
# someplace in the initializers of your application.
threadsafe_cattr_accessor :holidays

# You can set this yourself, either by the load method below, or
# by saying
# BusinessTime::Config.forced_workdays << my_forced_workday_date_object
# someplace in the initializers of your application.
threadsafe_cattr_accessor :forced_workdays

# 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:
Expand Down Expand Up @@ -174,6 +181,10 @@ def load(file)
(config["holidays"] || []).each do |holiday|
holidays << Date.parse(holiday)
end

(config["forced_workdays"] || []).each do |forced_workday|
forced_workdays << Date.parse(forced_workday)
end
end

def with(config)
Expand Down
3 changes: 2 additions & 1 deletion lib/business_time/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module TimeExtensions
# True if this time is on a workday (between 00:00:00 and 23:59:59), even if
# this time falls outside of normal business hours.
def workday?
weekday? && !BusinessTime::Config.holidays.include?(to_date)
BusinessTime::Config.forced_workdays.include?(self) ||
(weekday? && !BusinessTime::Config.holidays.include?(to_date))
end

# True if this time falls on a weekday.
Expand Down
11 changes: 11 additions & 0 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@
assert !Time.parse('2012-05-07').workday?
end

it "include forced workdays read from YAML config file" do
yaml = <<-YAML
business_time:
forced_workdays:
- April 28th, 2018
YAML
config_file = StringIO.new(yaml.gsub!(/^ /, ''))
BusinessTime::Config.load(config_file)
assert BusinessTime::Config.forced_workdays.to_a == [Date.new(2018, 4, 28)]
end

it "use defaults for values missing in YAML file" do
yaml = <<-YAML
business_time:
Expand Down
7 changes: 7 additions & 0 deletions test/test_date_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
assert(!july_5.workday?)
end

it "know a forced_workday is a workday" do
april_28 = Date.parse("April 28, 2018")
assert(!april_28.workday?)
BusinessTime::Config.forced_workdays << april_28
assert(april_28.workday?)
end

it "#week" do
assert_equal 1, Date.parse("Jan 1, 2017").week
assert_equal 1, Date.parse("Jan 7, 2017").week
Expand Down