Sha256: 2e5ef4d2b88f729ab2cbd3b057c2913496d7ca66f6574b4f681c4e80f2d260a9

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Dry
  module Validation
    class Schema
      class Rule
        attr_reader :name, :node

        class Check < Rule
          def method_missing(meth, *)
            self.class.new(name, [:check, [name, [:predicate, [name, [meth]]]]])
          end
        end

        def initialize(name, node)
          @name = name
          @node = node
        end

        def to_ary
          node
        end
        alias_method :to_a, :to_ary

        def to_check
          Rule::Check.new(name, [:check, [name, [:predicate, [name, []]]]])
        end

        def not
          self.class.new(:"not_#{name}", [:not, node])
        end

        def and(other)
          self.class.new(:"#{name}_and_#{other.name}", [:and, [node, other.to_ary]])
        end
        alias_method :&, :and

        def or(other)
          self.class.new(:"#{name}_or_#{other.name}", [:or, [node, other.to_ary]])
        end
        alias_method :|, :or

        def xor(other)
          self.class.new(:"#{name}_xor_#{other.name}", [:xor, [node, other.to_ary]])
        end
        alias_method :^, :xor

        def then(other)
          self.class.new(:"#{name}_then_#{other.name}", [:implication, [node, other.to_ary]])
        end
        alias_method :>, :then
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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