summaryrefslogtreecommitdiff
path: root/lib/custodian/alertfactory.rb
blob: a9b6d3d7c18e30a2f5e70faad40802de93252e19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#
#
# Base class for custodian notifiers.
#
# 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 target for the alert.
    #
    # The meaning of the target is notifier-specific.
    #
    # In the case of the smtp-notifier the target is the
    # email address to notify, for example.
    #
    attr_reader :target


    #
    # The global configuration settings object.
    #
    attr_reader :settings



    #
    # The subclasses we have.
    #
    @@subclasses = { }



    #
    # Create an notifier object, based upon name given to us.
    #
    # The "obj" here is the test-case that will be generating the
    # raise/clear event.
    #
    def self.create( alert_type, obj )

      raise ArgumentError, "The type of notifier to create cannot be nil" if ( alert_type.nil? )
      raise ArgumentError, "The type of notifier to create must be a string" unless ( alert_type.kind_of? String )

      c = @@subclasses[alert_type]
      if c
        c.new( obj )
      else
        raise ArgumentError, "Bad alert type: '#{alert_type}'"
      end
    end


    #
    # Register a new type of notifier - this must be called by our derived classes
    #
    def self.register_alert_type name
      @@subclasses[name] = self
    end


    #
    # Return the notifiers we know about.
    #
    # i.e. Derived classes that have registered themselves.
    #
    def self.known_alerters
      @@subclasses
    end


    #
    # Get the friendly-type of derived-classes.
    #
    def get_type
      @@subclasses.each do |name,value|
        if ( value == self.class  )
          return name
        end
      end
      nil
    end



    #
    # Set the target for this alert.
    #
    def set_target( target )
      @target = target
    end



    #
    # Store a reference to the settings
    #
    def set_settings( obj )
      @settings = obj
    end



    #
    # Raise an alert.
    #
    def raise
      puts "NOP"
    end



    #
    # Clear an alert.
    #
    def clear
      puts "NOP"
    end


  end

end