lib/dry/schema/message.rb in dry-schema-0.6.0 vs lib/dry/schema/message.rb in dry-schema-1.0.0
- old
+ new
@@ -14,20 +14,38 @@
class Message
include Dry::Equalizer(:text, :path, :predicate, :input)
extend Dry::Initializer
+ # @!attribute [r] text
+ # Message text representation created from a localized template
+ # @return [String]
option :text
+ # @!attribute [r] path
+ # Path to the value
+ # @return [String]
option :path
+ # @!attribute [r] predicate
+ # Predicate identifier that was used to produce a message
+ # @return [Symbol]
option :predicate
+ # @!attribute [r] args
+ # Optional list of arguments used by the predicate
+ # @return [Array]
option :args, default: proc { EMPTY_ARRAY }
+ # @!attribute [r] input
+ # The input value
+ # @return [Object]
option :input
+ # @!attribute [r] meta
+ # Arbitrary meta data
+ # @return [Hash]
option :meta, optional: true, default: proc { EMPTY_HASH }
# Dump the message to a representation suitable for the message set hash
#
# @return [String,Hash]
@@ -36,19 +54,30 @@
def dump
@dump ||= meta.empty? ? text : { text: text, **meta }
end
alias to_s dump
+ # See if another message is the same
+ #
+ # If a string is passed, it will be compared with the text
+ #
+ # @param [Message,String]
+ #
+ # @return [Boolean]
+ #
# @api private
def eql?(other)
other.is_a?(String) ? text == other : super
end
+ # See which message is higher in the hierarchy
+ #
+ # @api private
def <=>(other)
l_path = Path[path]
r_path = Path[other.path]
- unless l_path.include?(r_path)
+ unless l_path.same_root?(r_path)
raise ArgumentError, 'Cannot compare messages from different root paths'
end
l_path <=> r_path
end