Class: God::Conditions::Complex

Inherits:
PollCondition show all
Defined in:
lib/god/conditions/complex.rb

Constant Summary collapse

AND =
0x1
OR =
0x2
NOT =
0x4

Instance Attribute Summary

Attributes inherited from PollCondition

#interval

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

Methods inherited from PollCondition

#after, #before

Methods inherited from God::Condition

#friendly_name, generate, valid?

Methods inherited from Behavior

#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #friendly_name, generate

Methods included from God::Configurable

#base_name, #complain, complain, #friendly_name, #reset

Constructor Details

#initializeComplex

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

Yields:

  • (oper)


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

Yields:

  • (oper)


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

Yields:

  • (oper)


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

Yields:

  • (oper)


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

#prepareObject



22
23
24
# File 'lib/god/conditions/complex.rb', line 22

def prepare
  @oper_stack.each { |oper| oper.prepare }
end

#testObject



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?
    # Although this() makes sense semantically and therefore
    # encourages easy-to-read conditions, being able to omit it
    # allows for more DRY code in some cases, so we deal with a
    # nil @this here by initially setting res to true or false,
    # depending on whether the first operator used is AND or OR
    # respectively.
    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

Yields:



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

Returns:

  • (Boolean)


18
19
20
# File 'lib/god/conditions/complex.rb', line 18

def valid?
  @oper_stack.inject(true) { |acc, oper| acc & oper.valid? }
end