Module: Dry::Schema::Extensions::Hints::MessageCompilerMethods Private

Included in:
MessageCompiler
Defined in:
lib/dry/schema/extensions/hints/message_compiler_methods.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Adds support for processing [:hint, …] nodes produced by dry-logic

Constant Summary collapse

HINT_TYPE_EXCLUSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i[
  key? nil? bool? str? int? float? decimal?
  date? date_time? time? hash? array?
].freeze
HINT_OTHER_EXCLUSION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i[format? filled?].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hintsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 19

def hints
  @hints
end

Instance Method Details

#exclude?(messages, opts) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/PerceivedComplexity rubocop: disable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 41

def exclude?(messages, opts)
  Array(messages).all? do |msg|
    hints = opts.hints.reject { |h|
      msg.eql?(h) || h.predicate.eql?(:filled?)
    }

    key_failure = opts.key_failure?(msg.path)
    predicate = msg.predicate

    (HINT_TYPE_EXCLUSION.include?(predicate) && !key_failure) ||
      (msg.predicate == :filled? && key_failure) ||
      (!key_failure && HINT_TYPE_EXCLUSION.include?(predicate) &&
        !hints.empty? && hints.any? { |hint| hint.path == msg.path }) ||
      HINT_OTHER_EXCLUSION.include?(predicate)
  end
end

#filter(messages, opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 33

def filter(messages, opts)
  Array(messages).flatten.map { |msg| msg unless exclude?(msg, opts) }.compact.uniq
end

#hints?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


28
29
30
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 28

def hints?
  hints.equal?(true)
end

#initializeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 22

def initialize(*, **)
  super
  @hints = @options.fetch(:hints, true)
end

#message_type(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 62

def message_type(options)
  options[:message_type].equal?(:hint) ? Hint : Message
end

#visit_each(_node, _opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 81

def visit_each(_node, _opts)
  # TODO: we can still generate a hint for elements here!
  []
end

#visit_hint(node, opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
70
71
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 67

def visit_hint(node, opts)
  if hints?
    filter(visit(node, opts.(message_type: :hint)), opts)
  end
end

#visit_predicate(node, opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
77
78
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 74

def visit_predicate(node, opts)
  message = super
  opts.current_messages << message
  message
end