Class: God::Conditions::Complex
Constant Summary
collapse
- AND =
0x1
- OR =
0x2
- NOT =
0x4
Instance Attribute Summary
#interval
#info, #notify, #phase, #transition
Attributes inherited from Behavior
#watch
Instance Method Summary
collapse
#after, #before
#friendly_name, generate, valid?
Methods inherited from Behavior
#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #friendly_name, generate
#base_name, #complain, complain, #friendly_name, #reset
Constructor Details
Returns a new instance of Complex.
9
10
11
12
13
14
15
16
|
# File 'lib/god/conditions/complex.rb', line 9
def initialize()
super
@oper_stack = []
@op_stack = []
@this = nil
end
|
Instance Method Details
#and(kind) {|oper| ... } ⇒ Object
38
39
40
41
|
# File 'lib/god/conditions/complex.rb', line 38
def and(kind)
oper = new_oper(kind, 0x1)
yield oper if block_given?
end
|
#and_not(kind) {|oper| ... } ⇒ Object
43
44
45
46
|
# File 'lib/god/conditions/complex.rb', line 43
def and_not(kind)
oper = new_oper(kind, 0x5)
yield oper if block_given?
end
|
#new_oper(kind, op) ⇒ Object
26
27
28
29
30
31
|
# File 'lib/god/conditions/complex.rb', line 26
def new_oper(kind, op)
oper = Condition.generate(kind, self.watch)
@oper_stack.push(oper)
@op_stack.push(op)
oper
end
|
#or(kind) {|oper| ... } ⇒ Object
48
49
50
51
|
# File 'lib/god/conditions/complex.rb', line 48
def or(kind)
oper = new_oper(kind, 0x2)
yield oper if block_given?
end
|
#or_not(kind) {|oper| ... } ⇒ Object
53
54
55
56
|
# File 'lib/god/conditions/complex.rb', line 53
def or_not(kind)
oper = new_oper(kind, 0x6)
yield oper if block_given?
end
|
#prepare ⇒ Object
22
23
24
|
# File 'lib/god/conditions/complex.rb', line 22
def prepare
@oper_stack.each { |oper| oper.prepare }
end
|
#test ⇒ Object
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
|
# File 'lib/god/conditions/complex.rb', line 58
def test
if @this.nil?
if 0 < @op_stack[0] & AND
res = true
else
res = false
end
else
res = @this.test
end
@op_stack.each do |op|
cond = @oper_stack.shift
eval "res " + ((0 < op & AND) ? "&&" : "||") + "= " + ((0 < op & NOT) ? "!" : "") + "cond.test"
@oper_stack.push cond
end
res
end
|
#this(kind) {|@this| ... } ⇒ Object
33
34
35
36
|
# File 'lib/god/conditions/complex.rb', line 33
def this(kind)
@this = Condition.generate(kind, self.watch)
yield @this if block_given?
end
|
#valid? ⇒ Boolean
18
19
20
|
# File 'lib/god/conditions/complex.rb', line 18
def valid?
@oper_stack.inject(true) { |acc, oper| acc & oper.valid? }
end
|