lib/dry/schema/message_set.rb in dry-schema-0.6.0 vs lib/dry/schema/message_set.rb in dry-schema-1.0.0
- old
+ new
@@ -11,11 +11,25 @@
# @api public
class MessageSet
include Enumerable
include Dry::Equalizer(:messages, :options)
- attr_reader :messages, :placeholders, :options
+ # A list of compiled message objects
+ #
+ # @return [Array<Message>]
+ attr_reader :messages
+
+ # An internal hash that is filled in with dumped messages
+ # when a message set is coerced to a hash
+ #
+ # @return [Hash<Symbol=>[Array,Hash]>]
+ attr_reader :placeholders
+
+ # Options hash
+ #
+ # @return [Hash]
+ attr_reader :options
# @api private
def self.[](messages, options = EMPTY_HASH)
new(messages.flatten, options)
end
@@ -25,34 +39,65 @@
@messages = messages
@options = options
initialize_placeholders!
end
+ # Iterate over messages
+ #
+ # @example
+ # result.errors.each do |message|
+ # puts message.text
+ # end
+ #
+ # @return [Array]
+ #
# @api public
def each(&block)
return self if empty?
return to_enum unless block
messages.each(&block)
end
+ # Dump message set to a hash
+ #
+ # @return [Hash<Symbol=>Array<String>>]
+ #
# @api public
def to_h
@to_h ||= messages_map
end
alias_method :to_hash, :to_h
+ # Get a list of message texts for the given key
+ #
+ # @param [Symbol] key
+ #
+ # @return [Array<String>]
+ #
# @api public
def [](key)
to_h[key]
end
+ # Get a list of message texts for the given key
+ #
+ # @param [Symbol] key
+ #
+ # @return [Array<String>]
+ #
+ # @raise KeyError
+ #
# @api public
def fetch(key)
self[key] || raise(KeyError, "+#{key}+ message was not found")
end
- # @api private
+ # Check if a message set is empty
+ #
+ # @return [Boolean]
+ #
+ # @api public
def empty?
@empty ||= messages.empty?
end
# @api private