diff options
| -rw-r--r-- | lib/custodian/alertfactory.rb | 70 | ||||
| -rw-r--r-- | lib/custodian/alerts.rb | 20 | ||||
| -rw-r--r-- | lib/custodian/alerts/mauve.rb | 34 | ||||
| -rw-r--r-- | lib/custodian/alerts/smtp.rb | 34 | ||||
| -rwxr-xr-x | t/test-custodian-alertfactory.rb | 50 | 
5 files changed, 208 insertions, 0 deletions
| diff --git a/lib/custodian/alertfactory.rb b/lib/custodian/alertfactory.rb new file mode 100644 index 0000000..f82706f --- /dev/null +++ b/lib/custodian/alertfactory.rb @@ -0,0 +1,70 @@ + + +# +# +# Base class for custodian alerters. +# +# Each subclass will register themselves, via the call +# to 'register_alert_type'. +# +# This class is a factory that will return the correct +# derived class. +# +# +module Custodian + +  class AlertFactory + + +    # +    # The subclasses we have. +    # +    @@subclasses = { } + + +    # +    # Create an alerter object, based upon the type +    # +    def self.create( alert_type, obj ) + +      c = @@subclasses[alert_type] +      if c +        c.new( obj ) +      else +        raise ArgumentError, "Bad alert type: '#{alert_type}'" +      end +    end + + +    # +    # Register a new test type - this must be called by our derived classes +    # +    def self.register_alert_type name +      @@subclasses[name] = self +    end + + +    # +    # Return an array of test-types we know about +    # +    # i.e. Derived classes that have registered themselves. +    # +    # +    def self.known_alerters +      @@subclasses +    end + + + +    def raise +      raise NoMethod, "This should be implemented in a derived class" +    end + + + +    def clear +      raise NoMethod, "This should be implemented in a derived class" +    end +  end + +end diff --git a/lib/custodian/alerts.rb b/lib/custodian/alerts.rb new file mode 100644 index 0000000..74d021b --- /dev/null +++ b/lib/custodian/alerts.rb @@ -0,0 +1,20 @@ +# +# This is just a helper which means that you'll only need to +# update the list of requires in one-place if you add a new +# protocol alert-type +# + + +# +# The factory +# +require 'custodian/alertfactory' + + +# +# The individual alert-types. +# +require 'custodian/alerts/mauve' +require 'custodian/alerts/smtp' + + diff --git a/lib/custodian/alerts/mauve.rb b/lib/custodian/alerts/mauve.rb new file mode 100644 index 0000000..c3e8a76 --- /dev/null +++ b/lib/custodian/alerts/mauve.rb @@ -0,0 +1,34 @@ + + + +# +#  The Mauve-alerter. +# +module Custodian + +  module Alerter + +    class Mauve < AlertFactory + +      # +      # The test this alerter cares about +      # +      attr_reader :test + +      # +      # Constructor +      # +      def initialize( obj ) +        @test = obj +      end + + + +      register_alert_type "mauve" + + + + +    end +  end +end diff --git a/lib/custodian/alerts/smtp.rb b/lib/custodian/alerts/smtp.rb new file mode 100644 index 0000000..21ef791 --- /dev/null +++ b/lib/custodian/alerts/smtp.rb @@ -0,0 +1,34 @@ + + + +# +#  The SMTP-alerter. +# +module Custodian + +  module Alerter + +    class SMTP < AlertFactory + +      # +      # The test this alerter cares about +      # +      attr_reader :test + +      # +      # Constructor +      # +      def initialize( obj ) +        @test = obj +      end + + + +      register_alert_type "smtp" + + + + +    end +  end +end diff --git a/t/test-custodian-alertfactory.rb b/t/test-custodian-alertfactory.rb new file mode 100755 index 0000000..11ad4b9 --- /dev/null +++ b/t/test-custodian-alertfactory.rb @@ -0,0 +1,50 @@ +#!/usr/bin/ruby -Ilib/ -I../lib/ + + +require 'test/unit' + +require 'custodian/alerts' + + + +class TestAlertFactory < Test::Unit::TestCase + +  # +  # Create the test suite environment: NOP. +  # +  def setup +  end + +  # +  # Destroy the test suite environment: NOP. +  # +  def teardown +  end + + +  # +  # Test the FTP-test may be created +  # +  def test_alert_creation + +    # +    # Ensure we can create each of the two alert types we care about +    # +    %w( mauve smtp ).each do |name| +      obj = Custodian::AlertFactory.create( name, nil ) + +      # +      # Get the name of the class, and ensure it matches +      # what we expect. +      # +      nm = obj.class +      if ( nm =~ /Alerter::(.*)$/i ) +        tst = $1.dup.downcase + +        assert_equal( name, nm ) +      end +    end +  end + +end + | 
