Sha256: af0c839f56e9f772fcefa260a9c98e606fa625cd58795a8092454f4e775c0965

Contents?: true

Size: 1.89 KB

Versions: 12

Compression:

Stored size: 1.89 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

12 entries across 12 versions & 3 rubygems

Version Path
resurrected_god-0.14.0 lib/god/conditions/complex.rb
mcproc-2016.2.20 lib/god/conditions/complex.rb
god-0.13.7 lib/god/conditions/complex.rb
god-0.13.6 lib/god/conditions/complex.rb
god-0.13.5 lib/god/conditions/complex.rb
god-0.13.4 lib/god/conditions/complex.rb
god-0.13.3 lib/god/conditions/complex.rb
god-0.13.2 lib/god/conditions/complex.rb
god-0.13.1 lib/god/conditions/complex.rb
god-0.13.0 lib/god/conditions/complex.rb
god-0.12.1 lib/god/conditions/complex.rb
god-0.12.0 lib/god/conditions/complex.rb