finish lifecycle conditions handling and add flapper condition
[god.git] / lib / god / conditions / tries.rb
blob191b1ed8453300369d2d1f750a9aabcaae967434
1 module God
2   module Conditions
3     
4     class Tries < PollCondition
5       attr_accessor :times, :within
6       
7       def prepare
8         @timeline = Timeline.new(self.times)
9       end
10     
11       def valid?
12         valid = true
13         valid &= complain("You must specify the 'times' attribute for :tries") if self.times.nil?
14         valid
15       end
16     
17       def test
18         @timeline << Time.now
19         
20         concensus = (@timeline.size == self.times)
21         duration = self.within.nil? || (@timeline.last - @timeline.first) < self.within
22         
23         if concensus && duration
24           @timeline.clear if within.nil?
25           return true
26         else
27           return false
28         end
29       end
30     end
31     
32   end
33 end