Class: Dry::Validation::MessageSet
- Inherits:
-
Schema::MessageSet
- Object
- Schema::MessageSet
- Dry::Validation::MessageSet
- Defined in:
- lib/dry/validation/message_set.rb
Overview
MessageSet is a specialized message set for handling validation messages
Instance Attribute Summary collapse
-
#locale ⇒ Symbol
readonly
Configured locale.
-
#source_messages ⇒ Array<Message, Message::Localized, Schema::Message>
readonly
private
Return the source set of messages used to produce final evaluated messages.
Instance Method Summary collapse
-
#add(message) ⇒ MessageSet
private
Add a new message.
-
#filter(*predicates) ⇒ MessageSet
Filter message set using provided predicates.
- #freeze ⇒ Object private
-
#initialize(messages, options = EMPTY_HASH) ⇒ MessageSet
constructor
private
A new instance of MessageSet.
-
#with(other, new_options = EMPTY_HASH) ⇒ MessageSet
private
Return a new message set using updated options.
Constructor Details
#initialize(messages, options = EMPTY_HASH) ⇒ MessageSet
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 a new instance of MessageSet.
29 30 31 32 33 |
# File 'lib/dry/validation/message_set.rb', line 29 def initialize(, = EMPTY_HASH) @locale = [:locale] @source_messages = .fetch(:source) { .dup } super end |
Instance Attribute Details
#locale ⇒ Symbol (readonly)
Configured locale
26 27 28 |
# File 'lib/dry/validation/message_set.rb', line 26 def locale @locale end |
#source_messages ⇒ Array<Message, Message::Localized, Schema::Message> (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.
Return the source set of messages used to produce final evaluated messages
19 20 21 |
# File 'lib/dry/validation/message_set.rb', line 19 def @source_messages end |
Instance Method Details
#add(message) ⇒ MessageSet
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.
Add a new message
This is used when result is being prepared
56 57 58 59 60 61 |
# File 'lib/dry/validation/message_set.rb', line 56 def add() @empty = nil << << self end |
#filter(*predicates) ⇒ MessageSet
Filter message set using provided predicates
This method is open to any predicate because messages can be anything that implements Message API, thus they can implement whatever predicates you may need.
78 79 80 81 82 83 |
# File 'lib/dry/validation/message_set.rb', line 78 def filter(*predicates) = select { |msg| predicates.all? { |predicate| msg.respond_to?(predicate) && msg.public_send(predicate) } } self.class.new() end |
#freeze ⇒ 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.
86 87 88 89 90 91 92 93 94 |
# File 'lib/dry/validation/message_set.rb', line 86 def freeze .select { |err| err.respond_to?(:evaluate) }.each do |err| idx = .index(err) || .index(err) msg = err.evaluate(locale: locale, full: [:full]) [idx] = msg end to_h self end |
#with(other, new_options = EMPTY_HASH) ⇒ MessageSet
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.
Return a new message set using updated options
40 41 42 43 44 45 46 47 |
# File 'lib/dry/validation/message_set.rb', line 40 def with(other, = EMPTY_HASH) return self if .empty? && other.eql?() self.class.new( other | select { |err| err.is_a?(Message) }, .merge(source: , **) ).freeze end |