aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/configuration_builders/notification_method.rb
blob: 3f9283e95274a7e1d1dfba7baf6683d700a65d7b (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
require 'mauve/notifiers'
require 'mauve/configuration_builder'

module Mauve
  module ConfigurationBuilders
    class NotificationMethod < ObjectBuilder

      #
      # Set up the notification.  Missing notifiers are caught via NameError in
      # the ObjectBuilder#parse method.
      #
      # @param [String] name Name of the notifier
      # 
      def builder_setup(name)
        notifiers_base = Mauve::Notifiers

        @notifier_type = notifiers_base.const_get(name.capitalize)

        @name = name
        provider("Default")
      end

      # This allows use of multiple notification providers, e.g. in the case of
      # SMS.
      #
      # Missing providers are caught via NameError in the ObjectBuilder#parse
      # method.
      #
      def provider(name)
        @provider_class = @notifier_type.const_get(name)
      end
      
      # Returns the result for this builder, depending on the configuration
      #
      def result
        @result ||= @provider_class.new(@name)
      end

      def debug!
        result.extend(Mauve::Notifiers::Debug)
      end
     
      # This catches all methods available for a provider, as needed.
      #
      # Missing methods / bad arguments etc. are caught in the
      # ObjectBuilder#parse method, via NoMethodError.
      #
      def method_missing(name, value=nil)
        if value
          result.send("#{name}=".to_sym, value)
        else
          result.send(name.to_sym)
        end
      end
    end
  end

  #
  # Add notification_method to our top-level config builder
  #
  class ConfigurationBuilder < ObjectBuilder
    is_builder "notification_method", ConfigurationBuilders::NotificationMethod

    # Method called after a notification method has been created to check for duplicate names.
    #
    # @raise [BuildException] when a duplicate notification method is found.
    def created_notification_method(notification_method)
      name = notification_method.name
      raise BuildException.new("Duplicate notification '#{name}'") if @result.notification_methods[name]
      @result.notification_methods[name] = notification_method
    end

  end

end