diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-05-03 19:35:26 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-05-03 19:35:26 +0100 |
commit | dc1bbf37f31b4478aef43051ea6494f23cfe04b0 (patch) | |
tree | 72f023f5dfce7c3e5b1aefb499ccbf3e72ab8175 | |
parent | 757853bb96c2f75bcb28f16fcf80601abee48033 (diff) |
Update Time.bank_holiday? etc. to work correctly.
Mauve::Configuration#do_parse_time now a class method, and used elsewhere to
standardise ranges in the configuration file.
-rw-r--r-- | lib/mauve/configuration.rb | 15 | ||||
-rw-r--r-- | lib/mauve/mauve_time.rb | 45 | ||||
-rw-r--r-- | test/tc_mauve_configuration.rb | 5 |
3 files changed, 39 insertions, 26 deletions
diff --git a/lib/mauve/configuration.rb b/lib/mauve/configuration.rb index 55bdd9f..4ee71dd 100644 --- a/lib/mauve/configuration.rb +++ b/lib/mauve/configuration.rb @@ -32,10 +32,6 @@ module Mauve # @return [Array] attr_reader :alert_groups - # People lists - # @return [Hash] - attr_reader :people_lists - # The source lists # @return [Hash] attr_reader :source_lists @@ -53,7 +49,6 @@ module Mauve @server = nil @notification_methods = {} @people = {} - @people_lists = {} @source_lists = Hash.new{|h,k| h[k] = Mauve::SourceList.new(k)} @alert_groups = [] @@ -177,19 +172,17 @@ module Mauve end def working_hours=(arg) - @working_hours = do_parse_range(arg) + @working_hours = self.class.parse_range(arg) end def daytime_hours=(arg) - @daytime_hours = do_parse_range(arg) + @daytime_hours = self.class.parse_range(arg) end def dead_zone=(arg) - @dead_zone = do_parse_range(arg) + @dead_zone = self.class.parse_range(arg) end - private - # This method takes a range, and wraps it within the specs defined by # allowed_range. # @@ -197,7 +190,7 @@ module Mauve # # @param # - def do_parse_range(arg, allowed_range = (0...24)) + def self.parse_range(arg, allowed_range = (0...24)) args = [arg].flatten # diff --git a/lib/mauve/mauve_time.rb b/lib/mauve/mauve_time.rb index ab0734a..d3fc039 100644 --- a/lib/mauve/mauve_time.rb +++ b/lib/mauve/mauve_time.rb @@ -103,40 +103,46 @@ class Time # # def bank_holidays - @bank_holidays = if defined? Server and Server.instance - Server.instance.bank_holidays + if defined? Mauve::Server and Mauve::Server.instance + @bank_holidays = Mauve::Server.instance.bank_holidays else - @bank_holidays || [] + @bank_holidays ||= [] end + + @bank_holidays end # Returns an array of ranges of working hours # # def working_hours - if defined? Configuration and Configuration.current - Configuration.current.working_hours + if defined? Mauve::Configuration and Mauve::Configuration.current + Mauve::Configuration.current.working_hours else [9.0...17.0] end end def dead_zone - if defined? Configuration and Configuration.current - Configuration.current.working_hours + if defined? Mauve::Configuration and Mauve::Configuration.current + Mauve::Configuration.current.dead_zone else [3.0...7.0] end end def daytime_hours - if defined? Configuration and Configuration.current - Configuration.current.working_hours + if defined? Mauve::Configuration and Mauve::Configuration.current + Mauve::Configuration.current.daytime_hours else [8.0...20.0] end end + def fractional_hour + self.hour + self.min.to_f/60 + self.sec.to_f/3600 + end + # This relies on bank_holidays being set. # def bank_holiday? @@ -150,7 +156,7 @@ class Time # @return [Boolean] def working_hours? (1..5).include?(self.wday) and - self.working_hours.any?{|r| r.include?(self.hour.to_f + self.min.to_f/60.0)} and + x_in_list_of_y(fractional_hour, self.working_hours) and !self.bank_holiday? end @@ -158,7 +164,7 @@ class Time # # @return [Boolean] def daytime_hours? - self.daytime_hours.any?{|r| r.include?(self.hour.to_f + self.min.to_f/60.0)} + x_in_list_of_y(fractional_hour, self.daytime_hours) end # We're always in wallclock hours @@ -172,7 +178,7 @@ class Time # # @return [Boolean] def dead_zone? - self.dead_zone.any?{|r| r.include?(self.hour.to_f + self.min.to_f/60.0)} + x_in_list_of_y(fractional_hour, self.dead_zone) end # Format the time as a string, relative to +now+ @@ -248,6 +254,21 @@ class Time end + # Checks to see if x is contained in y + # + # @param [Array] y Array to search for +x+ + # @param [Object] x + # @return [Boolean] + def x_in_list_of_y(x,y) + y.any? do |range| + if range.respond_to?("include?") + range.include?(x) + else + range == x + end + end + end + end #module Mauve diff --git a/test/tc_mauve_configuration.rb b/test/tc_mauve_configuration.rb index bbc1901..36a929e 100644 --- a/test/tc_mauve_configuration.rb +++ b/test/tc_mauve_configuration.rb @@ -14,7 +14,7 @@ class TcMauveConfiguration < Mauve::UnitTest teardown_logger end - def test_do_parse_range + def test_parse_range [ [[1.0...2.0], 1], [[1.0...3.0], 1..2], @@ -28,8 +28,7 @@ class TcMauveConfiguration < Mauve::UnitTest [[6.0...7.0, 0.0...1.0], 6..0, 0...7], [["x".."z", "a".."c"], "x".."c", "a".."z"] ].each do |output, *input| - c = Configuration.new - assert_equal(output, c.__send__("do_parse_range",*input)) + assert_equal(output, Configuration.parse_range(*input)) end end |