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.



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

def exclude?(messages, opts)
  Array(messages).all? do |msg|
    hints = opts
      .hints
      .reject { |hint| msg == hint }
      .reject { |hint| hint.predicate == :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.



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

def hints?
  hints.equal?(true)
end

#initialize(*args) ⇒ 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.



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

def initialize(*args)
  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.



57
58
59
# File 'lib/dry/schema/extensions/hints/message_compiler_methods.rb', line 57

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.



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

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.



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

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.



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

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