Initial commit
[god.git] / pkg / god-0.1.0 / lib / god / watch.rb
blob5dfa7c54d976a5e32fea3586bd2ab727ed302e68
1 module God
2   
3   class Watch
4     attr_accessor :name, :cwd, :start, :stop
5     
6     def initialize
7       @action = nil
8       @conditions = {:start => []}
9     end
10     
11     def start_if
12       @action = :start
13       yield(self)
14     end
15     
16     def condition(kind)
17       begin
18         c = Condition.generate(kind)
19       rescue
20         puts "No condition found for #{kind}"
21         exit
22       end
23       
24       yield(c)
25       
26       unless c.valid?
27         exit
28       end
29       
30       @conditions[@action] << c
31     end
32     
33     def run
34       @conditions[:start].each do |c|
35         if c.test
36           puts self.name + ' ' + c.class.name + ' [ok]'
37         else
38           puts self.name + ' ' + c.class.name + ' [fail]'
39           c.after
40           return :start
41         end
42       end
43       
44       nil
45     end
46     
47     def action(a)
48       case a
49       when :start
50         puts self.start
51         Dir.chdir(self.cwd) do
52           system(self.start)
53         end
54       end
55     end
56   end
57   
58 end