3 class Condition < Behavior
4 attr_accessor :transition, :notify, :info
6 # Generate a Condition of the given kind. The proper class if found by camel casing the
7 # kind (which is given as an underscored symbol).
8 # +kind+ is the underscored symbol representing the class (e.g. :foo_bar for God::Conditions::FooBar)
9 def self.generate(kind, watch)
10 sym = kind.to_s.capitalize.gsub(/_(.)/){$1.upcase}.intern
11 c = God::Conditions.const_get(sym).new
13 unless c.kind_of?(PollCondition) || c.kind_of?(EventCondition) || c.kind_of?(TriggerCondition)
14 abort "Condition '#{c.class.name}' must subclass God::PollCondition, God::EventCondition, or God::TriggerCondition"
20 raise NoSuchConditionError.new("No Condition found with the class name God::Conditions::#{sym}")
23 def self.valid?(condition)
27 Contact.normalize(condition.notify)
28 rescue ArgumentError => e
29 valid &= Configurable.complain("Attribute 'notify' " + e.message, condition)
35 # Construct the friendly name of this Condition, looks like:
37 # Condition FooBar on Watch 'baz'
39 "Condition #{self.class.name.split('::').last} on Watch '#{self.watch.name}'"
43 class PollCondition < Condition
44 # all poll conditions can specify a poll interval
45 attr_accessor :interval
47 # Override this method in your Conditions (optional)
51 # Override this method in your Conditions (mandatory)
53 # Return true if the test passes (everything is ok)
54 # Return false otherwise
56 raise AbstractMethodNotOverriddenError.new("PollCondition#test must be overridden in subclasses")
59 # Override this method in your Conditions (optional)
64 class EventCondition < Condition
66 raise AbstractMethodNotOverriddenError.new("EventCondition#register must be overridden in subclasses")
70 raise AbstractMethodNotOverriddenError.new("EventCondition#deregister must be overridden in subclasses")
74 class TriggerCondition < Condition
75 def process(event, payload)
76 raise AbstractMethodNotOverriddenError.new("TriggerCondition#process must be overridden in subclasses")
84 Trigger.register(self)
88 Trigger.deregister(self)