lib/dry/schema/message.rb in dry-schema-0.4.0 vs lib/dry/schema/message.rb in dry-schema-0.5.0
- old
+ new
@@ -1,85 +1,43 @@
# frozen_string_literal: true
+require 'dry/initializer'
require 'dry/equalizer'
+
require 'dry/schema/path'
+require 'dry/schema/message/or'
module Dry
module Schema
# Message objects used by message sets
#
# @api public
class Message
- include Dry::Equalizer(:predicate, :path, :text, :options)
+ include Dry::Equalizer(:text, :path, :predicate, :input)
- attr_reader :predicate, :path, :text, :args, :options
+ extend Dry::Initializer
- # A message sub-type used by OR operations
- #
- # @api public
- class Or
- include Enumerable
+ option :text
- # @api private
- attr_reader :left
+ option :path
- # @api private
- attr_reader :right
+ option :predicate
- # @api private
- attr_reader :path
+ option :args, default: proc { EMPTY_ARRAY }
- # @api private
- attr_reader :messages
+ option :input
- # @api private
- def initialize(left, right, messages)
- @left = left
- @right = right
- @messages = messages
- @path = left.path
- end
+ option :meta, optional: true, default: proc { EMPTY_HASH }
- # Return a string representation of the message
- #
- # @api public
- def to_s
- uniq.join(" #{messages[:or]} ")
- end
-
- # @api private
- def each(&block)
- to_a.each(&block)
- end
-
- # @api private
- def to_a
- [left, right]
- end
- end
-
- # Build a new message object
+ # Dump the message to a representation suitable for the message set hash
#
- # @api private
- def self.[](predicate, path, text, options)
- Message.new(predicate, path, text, options)
- end
-
- # @api private
- def initialize(predicate, path, text, options)
- @predicate = predicate
- @path = path
- @text = text
- @options = options
- @args = options[:args] || EMPTY_ARRAY
- end
-
- # Return a string representation of the message
+ # @return [String,Hash]
#
# @api public
- def to_s
- text
+ def dump
+ @dump ||= meta.empty? ? text : { text: text, **meta }
end
+ alias to_s dump
# @api private
def eql?(other)
other.is_a?(String) ? text == other : super
end