Sha256: 77dfab38f27191fc1bd5c384c3165819c1057ec2b7e522273feae45037285ce7

Contents?: true

Size: 1.99 KB

Versions: 68

Compression:

Stored size: 1.99 KB

Contents

module God
  module Conditions
    
    class Complex < PollCondition
      AND = 0x1 
      OR  = 0x2
      NOT = 0x4
        
      def initialize()
        super
        
        @oper_stack = []
        @op_stack = []
        
        @this = nil
      end
      
      def valid?
        @oper_stack.inject(true) { |acc, oper| acc & oper.valid? }
      end
      
      def prepare
        @oper_stack.each { |oper| oper.prepare }
      end
      
      def new_oper(kind, op)
        oper = Condition.generate(kind, self.watch)
        @oper_stack.push(oper)
        @op_stack.push(op)
        oper
      end
      
      def this(kind)
        @this = Condition.generate(kind, self.watch)
        yield @this if block_given?
      end
      
      def and(kind)
        oper = new_oper(kind, 0x1)
        yield oper if block_given?
      end
      
      def and_not(kind)
        oper = new_oper(kind, 0x5)
        yield oper if block_given?
      end
      
      def or(kind)
        oper = new_oper(kind, 0x2)
        yield oper if block_given?
      end
      
      def or_not(kind)
        oper = new_oper(kind, 0x6)
        yield oper if block_given?
      end
      
      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
    end
    
  end
end

Version data entries

68 entries across 68 versions & 21 rubygems

Version Path
mojombo-god-0.7.11 lib/god/conditions/complex.rb
mojombo-god-0.7.12 lib/god/conditions/complex.rb
mojombo-god-0.7.13 lib/god/conditions/complex.rb
mojombo-god-0.7.14 lib/god/conditions/complex.rb
mojombo-god-0.7.15 lib/god/conditions/complex.rb
mojombo-god-0.7.16 lib/god/conditions/complex.rb
mojombo-god-0.7.20 lib/god/conditions/complex.rb
mojombo-god-0.7.7 lib/god/conditions/complex.rb
mojombo-god-0.7.9 lib/god/conditions/complex.rb
obitum-god-0.7.14.1 lib/god/conditions/complex.rb
obitum-god-0.7.14 lib/god/conditions/complex.rb
pjhyett-god-0.7.12 lib/god/conditions/complex.rb
relevance-god-0.7.12.1 lib/god/conditions/complex.rb
relevance-god-0.7.12 lib/god/conditions/complex.rb
saimonmoore-god-0.7.9 lib/god/conditions/complex.rb
samhendley-god-0.7.13 lib/god/conditions/complex.rb
strobemonkey-god-0.7.13 lib/god/conditions/complex.rb
firenxis-god-0.11.0 lib/god/conditions/complex.rb
god-0.11.0 lib/god/conditions/complex.rb
god-0.10.1 lib/god/conditions/complex.rb