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.



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

Yields:

  • (oper)


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

Yields:

  • (oper)


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

Yields:

  • (oper)


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

Yields:

  • (oper)


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

#prepareObject



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

def prepare
  @oper_stack.each(&:prepare)
end

#testObject



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?
          # 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.
          @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

Yields:



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

Returns:

  • (Boolean)


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

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