Sha256: 8850b88b57ce45d096fe94a8d3161d6bf5d9c9d6db92c5e6dc4cfe0431583f3f

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module Dry
  module Logic
    class Rule::Composite < Rule
      include Dry::Equalizer(:left, :right)

      attr_reader :name, :left, :right

      def initialize(left, right)
        @left = left
        @right = right
      end

      def name
        :"#{left.name}_#{type}_#{right.name}"
      end

      def to_ary
        [type, [left.to_ary, right.to_ary]]
      end
      alias_method :to_a, :to_ary
    end

    class Rule::Implication < Rule::Composite
      def call(*args)
        left.(*args) > right
      end

      def type
        :implication
      end
    end

    class Rule::Conjunction < Rule::Composite
      def call(*args)
        left.(*args).and(right)
      end

      def type
        :and
      end
    end

    class Rule::Disjunction < Rule::Composite
      def call(*args)
        left.(*args).or(right)
      end

      def type
        :or
      end
    end

    class Rule::ExclusiveDisjunction < Rule::Composite
      def call(*args)
        left.(*args).xor(right)
      end

      def type
        :xor
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-logic-0.1.2 lib/dry/logic/rule/composite.rb
dry-logic-0.1.1 lib/dry/logic/rule/composite.rb
dry-logic-0.1.0 lib/dry/logic/rule/composite.rb