Register the watch's process at the end of meddle.
[god.git] / lib / god / condition.rb
blob5c384bdae793e222da329332393fefd6a44f2fc9
1 module God
2   
3   class Condition < Behavior
4     # Generate a Condition of the given kind. The proper class if found by camel casing the
5     # kind (which is given as an underscored symbol).
6     #   +kind+ is the underscored symbol representing the class (e.g. foo_bar for God::Conditions::FooBar)
7     def self.generate(kind)
8       sym = kind.to_s.capitalize.gsub(/_(.)/){$1.upcase}.intern
9       cond = God::Conditions.const_get(sym).new
10       
11       unless cond.kind_of?(PollCondition) || cond.kind_of?(EventCondition)
12         abort "Condition '#{cond.class.name}' must subclass either God::PollCondition or God::EventCondition" 
13       end
14       
15       cond
16     rescue NameError
17       raise NoSuchConditionError.new("No Condition found with the class name God::Conditions::#{sym}")
18     end
19   end
20   
21   class PollCondition < Condition
22     # all poll conditions can specify a poll interval 
23     attr_accessor :interval
24     
25     # Override this method in your Conditions (optional)
26     def before
27     end
28     
29     # Override this method in your Conditions (mandatory)
30     #
31     # Return true if the test passes (everything is ok)
32     # Return false otherwise
33     def test
34       raise AbstractMethodNotOverriddenError.new("Condition#test must be overridden in subclasses")
35     end
36     
37     # Override this method in your Conditions (optional)
38     def after
39     end
40   end
41   
42   class EventCondition < Condition
43     def register
44       
45     end
46   end
47   
48 end