Sha256: 78ffb9061c56c934e09ae23b9d06e210d1bff414c482b66a1172db4b97a418dd

Contents?: true

Size: 1.22 KB

Versions: 19

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'dry/schema/constants'
require 'dry/schema/message'

module Dry
  module Schema
    # @api private
    class MessageCompiler
      # Optimized option hash used by visitor methods in message compiler
      #
      # @api private
      class VisitorOpts < Hash
        # @api private
        def self.new
          opts = super
          opts[:path] = EMPTY_ARRAY
          opts[:message_type] = :failure
          opts[:current_messages] = EMPTY_ARRAY.dup
          opts
        end

        # @api private
        def path
          self[:path]
        end

        # @api private
        def call(other)
          merge(other.update(path: [*path, *other[:path]]))
        end

        def dup(current_messages = EMPTY_ARRAY.dup)
          opts = super()
          opts[:current_messages] = current_messages
          opts
        end

        def key_failure?(path)
          failures.any? { |f| f.path == path && f.predicate.equal?(:key?) }
        end

        def failures
          current_messages.reject(&:hint?)
        end

        def hints
          current_messages.select(&:hint?)
        end

        def current_messages
          self[:current_messages]
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dry-schema-1.4.3 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.4.2 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.4.1 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.4.0 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.3.4 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.3.3 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.3.2 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.3.1 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.3.0 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.2.0 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.1.0 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.0.3 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.0.2 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.0.1 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-1.0.0 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-0.6.0 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-0.5.1 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-0.5.0 lib/dry/schema/message_compiler/visitor_opts.rb
dry-schema-0.4.0 lib/dry/schema/message_compiler/visitor_opts.rb