Sha256: 77abc1664961f94ad06dbf13926e49afdd7630140d1aa9132aa886df92d8cfc0

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

require 'dry/validation/error_compiler'

module Dry
  module Validation
    class HintCompiler < ErrorCompiler
      attr_reader :messages, :rules, :options

      def initialize(messages, options = {})
        @messages = messages
        @options = Hash[options]
        @rules = @options.delete(:rules)
      end

      def with(new_options)
        super(new_options.merge(rules: rules))
      end

      def call
        messages = Hash.new { |h, k| h[k] = [] }

        rules.map { |node| visit(node) }.compact.each do |hints|
          name, msgs = hints
          messages[name].concat(msgs)
        end

        messages
      end

      def visit_or(node)
        left, right = node
        [visit(left), Array(visit(right)).flatten.compact].compact
      end

      def visit_and(node)
        left, right = node
        [visit(left), Array(visit(right)).flatten.compact].compact
      end

      def visit_val(node)
        name, predicate = node
        visit(predicate, name)
      end

      def visit_predicate(node, name)
        predicate_name, args = node

        lookup_options = options.merge(rule: name, arg_type: args[0].class)

        template = messages[predicate_name, lookup_options]
        predicate_opts = visit(node, args)

        return unless predicate_opts

        tokens = predicate_opts.merge(name: name)

        template % tokens
      end

      def visit_key(node)
        name, _ = node
        name
      end

      def method_missing(name, *args)
        nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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