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.
10
11
12
13
14
15
16
17
|
# File 'lib/god/conditions/complex.rb', line 10
def initialize
super
@oper_stack = []
@op_stack = []
@this = nil
end
|
Instance Method Details
#and(kind) {|oper| ... } ⇒ Object
39
40
41
42
|
# File 'lib/god/conditions/complex.rb', line 39
def and(kind)
oper = new_oper(kind, 0x1)
yield oper if block_given?
end
|
#and_not(kind) {|oper| ... } ⇒ Object
44
45
46
47
|
# File 'lib/god/conditions/complex.rb', line 44
def and_not(kind)
oper = new_oper(kind, 0x5)
yield oper if block_given?
end
|
#new_oper(kind, operand) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/god/conditions/complex.rb', line 27
def new_oper(kind, operand)
oper = Condition.generate(kind, watch)
@oper_stack.push(oper)
@op_stack.push(operand)
oper
end
|
#or(kind) {|oper| ... } ⇒ Object
49
50
51
52
|
# File 'lib/god/conditions/complex.rb', line 49
def or(kind)
oper = new_oper(kind, 0x2)
yield oper if block_given?
end
|
#or_not(kind) {|oper| ... } ⇒ Object
54
55
56
57
|
# File 'lib/god/conditions/complex.rb', line 54
def or_not(kind)
oper = new_oper(kind, 0x6)
yield oper if block_given?
end
|
#prepare ⇒ Object
23
24
25
|
# File 'lib/god/conditions/complex.rb', line 23
def prepare
@oper_stack.each(&:prepare)
end
|
#test ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/god/conditions/complex.rb', line 59
def test
res = if @this.nil?
@op_stack[0] & AND > 0
else
@this.test
end
@op_stack.each do |op|
cond = @oper_stack.shift
value = op & NOT > 0 ? !cond.test : cond.test
if op & AND > 0
res &&= value
else
res ||= value
end
@oper_stack.push cond
end
res
end
|
#this(kind) {|@this| ... } ⇒ Object
34
35
36
37
|
# File 'lib/god/conditions/complex.rb', line 34
def this(kind)
@this = Condition.generate(kind, watch)
yield @this if block_given?
end
|
#valid? ⇒ Boolean
19
20
21
|
# File 'lib/god/conditions/complex.rb', line 19
def valid?
@oper_stack.inject(true) { |acc, oper| acc & oper.valid? }
end
|