Created
February 19, 2015 21:23
-
-
Save henrik/7f817afcc3c53f665d5d to your computer and use it in GitHub Desktop.
Code example for http://thepugautomatic.com/2015/02/time-restricted-autodeploy/.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AutodeployPolicy | |
ALLOWED_PERIODS = [ | |
"08:30:00".."11:59:59", | |
# No autodeploys during lunch! | |
"13:00:00".."18:00:00", | |
] | |
# On Fridays, people sometimes leave earlier. | |
# Not the funnest day to fix problems, so let's cut autodeploys earlier. | |
END_OF_WORKDAY_ON_FRIDAYS = "16:30:00" | |
def self.allow?(time) | |
new(time).allow? | |
end | |
def initialize(time) | |
@time = time | |
end | |
attr_reader :time | |
private :time | |
def allow? | |
return false if weekend? | |
return false if late_on_friday? | |
within_allowed_hours? | |
end | |
private | |
def weekend? | |
time.saturday? || time.sunday? | |
end | |
def late_on_friday? | |
time.friday? && formatted_time > END_OF_WORKDAY_ON_FRIDAYS | |
end | |
def within_allowed_hours? | |
ALLOWED_PERIODS.any? { |period| period.cover?(formatted_time) } | |
end | |
# E.g. "08:30:00" or "18:00:00". | |
def formatted_time | |
time.strftime("%H:%M:%S") | |
end | |
end | |
# When run directly (and not just required/loaded). | |
if __FILE__ == $0 | |
puts AutodeployPolicy.allow?(Time.now) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
require "ci/support/is_autodeploy_allowed" | |
describe AutodeployPolicy, ".allow?" do | |
it "is true during office hours on weekdays" do | |
expect(AutodeployPolicy.allow?(weekday_at 8, 30, 0)).to be_true | |
expect(AutodeployPolicy.allow?(weekday_at 18, 0, 0)).to be_true | |
end | |
it "is false outside office hours on weekdays" do | |
expect(AutodeployPolicy.allow?(weekday_at 8, 29, 59)).to be_false | |
expect(AutodeployPolicy.allow?(weekday_at 18, 0, 1)).to be_false | |
end | |
it "is false during lunch on weekdays" do | |
expect(AutodeployPolicy.allow?(weekday_at 11, 59, 59)).to be_true | |
expect(AutodeployPolicy.allow?(weekday_at 12, 0, 0)).to be_false | |
expect(AutodeployPolicy.allow?(weekday_at 12, 59, 59)).to be_false | |
expect(AutodeployPolicy.allow?(weekday_at 13, 0, 0)).to be_true | |
end | |
it "false any time during weekends" do | |
expect(AutodeployPolicy.allow?(weekend_at 10, 0, 0)).to be_false | |
end | |
it "assumes the workday ends earlier on Fridays" do | |
expect(AutodeployPolicy.allow?(friday_at 16, 30, 0)).to be_true | |
expect(AutodeployPolicy.allow?(friday_at 16, 30, 1)).to be_false | |
end | |
private | |
def weekday_at(hour, min, sec) | |
# A Thursday. | |
Time.mktime(2014, 10, 2, hour, min, sec) | |
end | |
def friday_at(hour, min, sec) | |
Time.mktime(2014, 10, 3, hour, min, sec) | |
end | |
def weekend_at(hour, min, sec) | |
# A Saturday. | |
Time.mktime(2014, 10, 4, hour, min, sec) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment