Sha256: 8e18a457b7f5be4661da1369dab93121c3e8a3785059a01c46e23cb631bfb57b

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module Dry
  module Validation
    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

2 entries across 2 versions & 1 rubygems

Version Path
dry-validation-0.4.1 lib/dry/validation/rule/composite.rb
dry-validation-0.4.0 lib/dry/validation/rule/composite.rb