Sha256: 4fe42344baba38a2067e0d8def72be0ede219224f6c532a1cd5f2d3decb76179

Contents?: true

Size: 718 Bytes

Versions: 3

Compression:

Stored size: 718 Bytes

Contents

require 'dry/logic/rule_compiler'

module Dry
  module Validation
    class Guard
      attr_reader :rule, :deps

      def initialize(rule, deps)
        @rule = rule
        @deps = deps
      end

      def call(input, results)
        rule.(input) if deps_valid?(results)
      end

      private

      def deps_valid?(results)
        deps.all? do |path|
          result = Array(path).reduce(results) { |a, e| a[e] }
          result.success? if result
        end
      end
    end

    class SchemaCompiler < Logic::RuleCompiler
      def visit_schema(klass)
        klass.new
      end

      def visit_guard(node)
        deps, other = node
        Guard.new(visit(other), deps)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-validation-0.7.2 lib/dry/validation/schema_compiler.rb
dry-validation-0.7.1 lib/dry/validation/schema_compiler.rb
dry-validation-0.7.0 lib/dry/validation/schema_compiler.rb