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.
26 27 28 29 30 |
# File 'lib/dry/validation/message_set.rb', line 26 def initialize(, = EMPTY_HASH) @locale = [:locale] @source_messages = .fetch(:source) { .dup } super end |
Instance Attribute Details
#locale ⇒ Symbol (readonly)
Configured locale
23 24 25 |
# File 'lib/dry/validation/message_set.rb', line 23 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
16 17 18 |
# File 'lib/dry/validation/message_set.rb', line 16 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
53 54 55 56 57 58 |
# File 'lib/dry/validation/message_set.rb', line 53 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.
75 76 77 78 79 80 |
# File 'lib/dry/validation/message_set.rb', line 75 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.
83 84 85 86 87 88 89 90 91 |
# File 'lib/dry/validation/message_set.rb', line 83 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
37 38 39 40 41 42 43 44 |
# File 'lib/dry/validation/message_set.rb', line 37 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 |