class Dry::Schema::MessageSet
A set of messages used to generate errors
@see Result#message_set
@api public
Attributes
messages[R]
A list of compiled message objects
@return [Array<Message>]
options[R]
Options hash
@return [Hash]
Public Class Methods
[](messages, options = EMPTY_HASH)
click to toggle source
@api private
# File lib/dry/schema/message_set.rb, line 27 def self.[](messages, options = EMPTY_HASH) new(messages.flatten, options) end
new(messages, options = EMPTY_HASH)
click to toggle source
@api private
# File lib/dry/schema/message_set.rb, line 32 def initialize(messages, options = EMPTY_HASH) @messages = messages @options = options end
Public Instance Methods
[](key)
click to toggle source
Get a list of message texts for the given key
@param [Symbol] key
@return [Array<String>]
@api public
# File lib/dry/schema/message_set.rb, line 71 def [](key) to_h[key] end
each(&block)
click to toggle source
Iterate over messages
@example
result.errors.each do |message| puts message.text end
@return [Array]
@api public
# File lib/dry/schema/message_set.rb, line 47 def each(&block) return self if empty? return to_enum unless block messages.each(&block) end
empty?()
click to toggle source
Check if a message set is empty
@return [Boolean]
@api public
# File lib/dry/schema/message_set.rb, line 93 def empty? @empty ||= messages.empty? end
fetch(key)
click to toggle source
Get a list of message texts for the given key
@param [Symbol] key
@return [Array<String>]
@raise KeyError
@api public
# File lib/dry/schema/message_set.rb, line 84 def fetch(key) self[key] || raise(KeyError, "+#{key}+ message was not found") end
freeze()
click to toggle source
@api private
Calls superclass method
# File lib/dry/schema/message_set.rb, line 98 def freeze to_h empty? super end
to_h()
click to toggle source
Dump message set to a hash
@return [Hash<Symbol=>Array<String>>]
@api public
# File lib/dry/schema/message_set.rb, line 59 def to_h @to_h ||= messages_map end
Also aliased as: to_hash
Private Instance Methods
combine_message_hashes(hashes)
click to toggle source
@api private
# File lib/dry/schema/message_set.rb, line 112 def combine_message_hashes(hashes) hashes.reduce(EMPTY_HASH.dup) do |a, e| a.merge(e) do |_, *values| combine_message_values(values) end end end
combine_message_values(values)
click to toggle source
@api private
# File lib/dry/schema/message_set.rb, line 121 def combine_message_values(values) hashes, other = partition_message_values(values) combined = combine_message_hashes(hashes) flattened = other.flatten if flattened.empty? combined elsif combined.empty? flattened else [flattened, combined] end end
messages_map(messages = self.messages)
click to toggle source
@api private
# File lib/dry/schema/message_set.rb, line 107 def messages_map(messages = self.messages) combine_message_hashes(messages.map(&:to_h)).freeze end
partition_message_values(values)
click to toggle source
@api private
# File lib/dry/schema/message_set.rb, line 136 def partition_message_values(values) values .map { |value| value.is_a?(Array) ? value : [value] } .reduce(EMPTY_ARRAY.dup, :+) .partition { |value| value.is_a?(Hash) && !value[:text].is_a?(String) } end