From dc1bbf37f31b4478aef43051ea6494f23cfe04b0 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Thu, 3 May 2012 19:35:26 +0100 Subject: 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. --- lib/mauve/configuration.rb | 15 ++++----------- lib/mauve/mauve_time.rb | 45 +++++++++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 23 deletions(-) (limited to 'lib') 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 -- cgit v1.2.1