Sha256: a491be88b19eba1ae828cb5c3db3c1b4c0567b22d614b80a670a3280f8e847db

Contents?: true

Size: 1.66 KB

Versions: 12

Compression:

Stored size: 1.66 KB

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

      def with(options)
        self.class.new(rule.with(options), deps)
      end

      private

      def deps_valid?(results)
        deps.all? do |path|
          result = nil
          Array(path).each do |name|
            curr = results[name]
            if curr
              result = curr.is_a?(Array) ? curr.first.success? : curr.success?
            end
          end
          result
        end
      end
    end

    class SchemaCompiler < Logic::RuleCompiler
      attr_reader :schema, :options

      def initialize(*args, options)
        super(*args)
        @options = options
        @schema = predicates.schema
      end

      def visit_rule(node)
        id, other = node
        visit(other).with(id: id)
      end

      def visit_predicate(node)
        super.eval_args(schema)
      end

      def visit_custom(node)
        id, predicate = node
        Logic::Rule.new(predicate).with(id: id).bind(schema)
      end

      def visit_schema(klass)
        opt_keys = klass.config.options.keys
        opt_vals = options.values_at(*opt_keys).compact

        if opt_vals.empty?
          klass.new
        else
          klass.new(klass.config.rules, Hash[opt_keys.zip(opt_vals)])
        end
      end

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

      def visit_type(type)
        type.rule
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dry-validation-0.13.3 lib/dry/validation/schema_compiler.rb
dry-validation-0.13.2 lib/dry/validation/schema_compiler.rb
dry-validation-0.13.1 lib/dry/validation/schema_compiler.rb
dry-validation-0.11.2 lib/dry/validation/schema_compiler.rb
dry-validation-0.12.3 lib/dry/validation/schema_compiler.rb
dry-validation-0.13.0 lib/dry/validation/schema_compiler.rb
dry-validation-0.12.2 lib/dry/validation/schema_compiler.rb
dry-validation-0.12.1 lib/dry/validation/schema_compiler.rb
dry-validation-0.12.0 lib/dry/validation/schema_compiler.rb
dry-validation-0.11.1 lib/dry/validation/schema_compiler.rb
dry-validation-0.11.0 lib/dry/validation/schema_compiler.rb
dry-validation-0.10.7 lib/dry/validation/schema_compiler.rb