File: condition.rb

package info (click to toggle)
ruby-god 0.12.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 752 kB
  • sloc: ruby: 5,913; ansic: 217; makefile: 3
file content (96 lines) | stat: -rw-r--r-- 2,832 bytes parent folder | download | duplicates (4)
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
module God

  class Condition < Behavior
    attr_accessor :transition, :notify, :info, :phase

    # Generate a Condition of the given kind. The proper class if found by camel casing the
    # kind (which is given as an underscored symbol).
    #   +kind+ is the underscored symbol representing the class (e.g. :foo_bar for God::Conditions::FooBar)
    def self.generate(kind, watch)
      sym = kind.to_s.capitalize.gsub(/_(.)/){$1.upcase}.intern
      c = God::Conditions.const_get(sym).new

      unless c.kind_of?(PollCondition) || c.kind_of?(EventCondition) || c.kind_of?(TriggerCondition)
        abort "Condition '#{c.class.name}' must subclass God::PollCondition, God::EventCondition, or God::TriggerCondition"
      end

      if !EventHandler.loaded? && c.kind_of?(EventCondition)
        abort "Condition '#{c.class.name}' requires an event system but none has been loaded"
      end

      c.watch = watch
      c
    rescue NameError
      raise NoSuchConditionError.new("No Condition found with the class name God::Conditions::#{sym}")
    end

    def self.valid?(condition)
      valid = true
      if condition.notify
        begin
          Contact.normalize(condition.notify)
        rescue ArgumentError => e
          valid &= Configurable.complain("Attribute 'notify' " + e.message, condition)
        end
      end
      valid
    end

    # Construct the friendly name of this Condition, looks like:
    #
    # Condition FooBar on Watch 'baz'
    def friendly_name
      "Condition #{self.class.name.split('::').last} on Watch '#{self.watch.name}'"
    end
  end

  class PollCondition < Condition
    # all poll conditions can specify a poll interval
    attr_accessor :interval

    # Override this method in your Conditions (optional)
    def before
    end

    # Override this method in your Conditions (mandatory)
    #
    # Return true if the test passes (everything is ok)
    # Return false otherwise
    def test
      raise AbstractMethodNotOverriddenError.new("PollCondition#test must be overridden in subclasses")
    end

    # Override this method in your Conditions (optional)
    def after
    end
  end

  class EventCondition < Condition
    def register
      raise AbstractMethodNotOverriddenError.new("EventCondition#register must be overridden in subclasses")
    end

    def deregister
      raise AbstractMethodNotOverriddenError.new("EventCondition#deregister must be overridden in subclasses")
    end
  end

  class TriggerCondition < Condition
    def process(event, payload)
      raise AbstractMethodNotOverriddenError.new("TriggerCondition#process must be overridden in subclasses")
    end

    def trigger
      self.watch.trigger(self)
    end

    def register
      Trigger.register(self)
    end

    def deregister
      Trigger.deregister(self)
    end
  end

end