Sha256: 05573a4f00c9cedd4585454d79b33e5833a93b813b3c9931b70d6f85ceee1fbc

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

module RShade
  module Filter
    class FilterComposition
      include Enumerable
      AND_OP = :and
      OR_OP = :or
      UNARY_OP = :unary
      attr_reader :op, :left, :right, :parent
      attr_accessor :parent

      # @param [#call, Enumerable] left
      # @param [#call, Enumerable] right
      def initialize(op=UNARY_OP, left=nil, right=nil)
        @op = op
        @left = left
        @right = right
      end

      def call(event)
        case op
        when UNARY_OP
          return left&.call(event)
        when AND_OP
          return left&.call(event) && right&.call(event)
        when OR_OP
          l = left&.call(event)
          r = right&.call(event)
          # puts "#{left} => #{l} OR #{right} => #{r}"
          return l || r
        else
          raise 'undefined op'
        end
      end

      def each(&block)
        if left&.respond_to?(:each)
          left&.each(&block)
        else
          yield left
        end

        if right&.respond_to?(:each)
          right&.each(&block)
        else
          yield right
        end
      end

      def config_filter(type, &block)
        filter = find do |filter|
          filter.is_a? type
        end
        filter.config(&block) if filter
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rshade-0.1.9.1 lib/rshade/filter/filter_composition.rb
rshade-0.1.9 lib/rshade/filter/filter_composition.rb