diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-04-23 16:01:44 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-04-23 16:01:44 +0100 |
commit | 6561f886ed03a79ca035a1733816ab97380576d2 (patch) | |
tree | 24282c1a9f85a6a1f1eb010df7c421f3c08315bb /lib/mauve | |
parent | ca99b0dff974d2fc841d7132d03b3ad1d1bf9b1e (diff) |
Persons/PeopleLists can now specify multiple notification preferences.
Diffstat (limited to 'lib/mauve')
-rw-r--r-- | lib/mauve/configuration.rb | 7 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/alert_group.rb | 29 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/people_list.rb | 17 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/person.rb | 18 | ||||
-rw-r--r-- | lib/mauve/notification.rb | 42 | ||||
-rw-r--r-- | lib/mauve/people_list.rb | 23 | ||||
-rw-r--r-- | lib/mauve/person.rb | 22 |
7 files changed, 78 insertions, 80 deletions
diff --git a/lib/mauve/configuration.rb b/lib/mauve/configuration.rb index a74165b..65f5f71 100644 --- a/lib/mauve/configuration.rb +++ b/lib/mauve/configuration.rb @@ -20,10 +20,6 @@ module Mauve # @return [Mauve::Server] attr_accessor :server - # The last AlertGroup to be configured - # @return [Mauve::AlertGroup] - attr_accessor :last_alert_group - # Notification methods # @return [Hash] attr_reader :notification_methods @@ -49,10 +45,9 @@ module Mauve # def initialize @server = nil - @last_alert_group = nil @notification_methods = {} @people = {} - @people_lists = Hash.new{|h,k| h[k] = Mauve::PeopleList.new(k)} + @people_lists = {} @source_lists = Hash.new{|h,k| h[k] = Mauve::SourceList.new(k)} @alert_groups = [] end diff --git a/lib/mauve/configuration_builders/alert_group.rb b/lib/mauve/configuration_builders/alert_group.rb index 40e5d9d..851ab7f 100644 --- a/lib/mauve/configuration_builders/alert_group.rb +++ b/lib/mauve/configuration_builders/alert_group.rb @@ -19,7 +19,10 @@ module Mauve # # @return [Mauve::Notification] New notification instance. def builder_setup(who) - who = if @context.people[who] + who = if who.is_a?(Mauve::Person) or who.is_a?(Mauve::PeopleList) + who + + elsif @context.people[who] @context.people[who] elsif @context.people_lists[who] @@ -29,7 +32,8 @@ module Mauve raise ArgumentError.new("You have not declared who #{who} is") end - @result = Mauve::Notification.new(who, @context.last_alert_group.level) + + @result = Mauve::Notification.new(who) end is_attribute "every" @@ -47,7 +51,6 @@ module Mauve # @return [Mauve::AlertGroup] New alert group instance def builder_setup(name="anonymous_name") @result = Mauve::AlertGroup.new(name) - @context.last_alert_group = @result end is_block_attribute "includes" @@ -62,7 +65,25 @@ module Mauve # def created_notify(notification) @result.notifications ||= [] - @result.notifications << notification + + if notification.during.nil? and notification.every.nil? + @result.notifications += notification.person.notifications.collect do |n| + # + # Set up a new notification for each one defined for this person. + # + new_notification = Mauve::Notification.new(notification.person) + new_notification.level = @result.level + new_notification.every = n.every + new_notification.during = n.during + new_notification + end + else + # + # Set the level for this notification + # + notification.level = @result.level + @result.notifications << notification + end end end diff --git a/lib/mauve/configuration_builders/people_list.rb b/lib/mauve/configuration_builders/people_list.rb index e5f6715..fffa15c 100644 --- a/lib/mauve/configuration_builders/people_list.rb +++ b/lib/mauve/configuration_builders/people_list.rb @@ -2,19 +2,32 @@ require 'object_builder' require 'mauve/people_list' require 'mauve/configuration_builder' +require 'mauve/configuration_builders/alert_group' module Mauve module ConfigurationBuilders class PeopleList < ObjectBuilder + is_builder "notification", Notification + def builder_setup(label, list) @result = Mauve::PeopleList.new(label) @result += list + @result + end + + # + # Notify is a shortcut for "notification" + # + def notify(&block) + notification(@result, &block) end - is_block_attribute "during" - is_attribute "every" + def created_notification(notification) + @result.notifications ||= [] + @result.notifications << notification + end end end diff --git a/lib/mauve/configuration_builders/person.rb b/lib/mauve/configuration_builders/person.rb index 7881d01..dff6f85 100644 --- a/lib/mauve/configuration_builders/person.rb +++ b/lib/mauve/configuration_builders/person.rb @@ -2,6 +2,7 @@ require 'object_builder' require 'mauve/person' require 'mauve/configuration_builder' +require 'mauve/configuration_builders/alert_group' module Mauve module ConfigurationBuilders @@ -12,11 +13,11 @@ module Mauve @result = Mauve::Person.new(username) end + is_builder "notification", Notification + is_block_attribute "urgent" is_block_attribute "normal" is_block_attribute "low" - is_block_attribute "during" - is_attribute "every" is_attribute "password" is_attribute "sms" is_attribute "holiday_url" @@ -29,6 +30,18 @@ module Mauve # @param [Block] block def all(&block); urgent(&block); normal(&block); low(&block); end + # + # Notify is a shortcut for "notification" + # + def notify(&block) + notification(@result, &block) + end + + def created_notification(notification) + @result.notifications ||= [] + @result.notifications << notification + end + # Notification suppression hash # # @param [Hash] h @@ -41,6 +54,7 @@ module Mauve @result.notification_thresholds[v] = Array.new(k) end end + end end diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb index 3c7270e..d06e03a 100644 --- a/lib/mauve/notification.rb +++ b/lib/mauve/notification.rb @@ -182,17 +182,19 @@ module Mauve # at a particular alert level, on a periodic basis, and optionally under # certain conditions specified by a block of code. # - class Notification < Struct.new(:person, :level) + class Notification + + attr_reader :during, :every, :level, :person # Set up a new notification # # @param [Array] person List of Mauve::Person to notify # @param [Symbol] level Level at which to notify - def initialize(person, level) - self.level = level - self.every = nil - self.during = nil - self.person = person + def initialize(person) + @person = person + @during = nil + @every = nil + @level = nil end # @return [String] @@ -203,34 +205,22 @@ module Mauve # @return Log4r::Logger def logger ; Log4r::Logger.new self.class.to_s ; end - # - # - # - def during - @during ||= person.during - end - - # - # - # def during=(arg) @during = arg end - # - # - # - def every - @every ||= person.during - end - - # - # - # def every=(arg) @every = arg end + def level=(arg) + @level = arg + end + + def person=(arg) + @person = arg + end + # Push a notification on to the queue for this alert. The Mauve::Notifier # will then pop it off and do the notification in a separate thread. # diff --git a/lib/mauve/people_list.rb b/lib/mauve/people_list.rb index 1890ab1..eab4ae3 100644 --- a/lib/mauve/people_list.rb +++ b/lib/mauve/people_list.rb @@ -9,7 +9,7 @@ module Mauve # class PeopleList - attr_reader :label, :list, :during, :every + attr_reader :label, :list, :notifications # Create a new list # @@ -17,31 +17,14 @@ module Mauve # @raise [ArgumentError] if the label is not a string # def initialize(label) - raise ArgumentError, "people_list label must be a string" unless label.is_a?(String) + raise ArgumentError, "people_list label must be a string #{label.inspect}" unless label.is_a?(String) @label = label @list = [] - @during = nil - @every = nil + @notifications = [] end alias username label - # - # - # - def during=(arg) - raise "during must be a block" unless arg.is_a?(Proc) - @during = arg - end - - # - # - # - def every=(arg) - raise ArgumentError, "every must be numeric" unless arg.is_a?(Numeric) - @every = arg - end - # Append an Array or String to a list # # @param [Array or String] arr diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb index 4b4baf1..1cf4ea0 100644 --- a/lib/mauve/person.rb +++ b/lib/mauve/person.rb @@ -5,7 +5,7 @@ require 'log4r' module Mauve class Person < Struct.new(:username, :password, :urgent, :normal, :low, :email, :xmpp, :sms) - attr_reader :notification_thresholds, :last_pop3_login, :suppressed, :every, :during + attr_reader :notification_thresholds, :last_pop3_login, :suppressed, :notifications # Set up a new Person # @@ -26,9 +26,7 @@ module Mauve # TODO fix up web login so pop3 can be used as a proxy. # @last_pop3_login = {:from => nil, :at => nil} - - @every = nil - @during = nil + @notifications = [] super(*args) end @@ -41,22 +39,6 @@ module Mauve # @return [Boolean] def suppressed? ; @suppressed ; end - # - # - # - def during=(arg) - raise "during must be a block" unless arg.is_a?(Proc) - @during = arg - end - - # - # - # - def every=(arg) - raise ArgumentError, "every must be numeric" unless arg.is_a?(Numeric) - @every = arg - end - def holiday_url ; nil ; end # Works out if a notification should be suppressed. If no parameters are supplied, it will |