Sha256: 59736e39b1d0b77c7e5cca5610223fa5cdfa2f35b6f1d9dac355185c14782dfa

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# 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(:text, :path, :predicate, :input)

      extend Dry::Initializer

      option :text

      option :path

      option :predicate

      option :args, default: proc { EMPTY_ARRAY }

      option :input

      option :meta, optional: true, default: proc { EMPTY_HASH }

      # Dump the message to a representation suitable for the message set hash
      #
      # @return [String,Hash]
      #
      # @api public
      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

      def <=>(other)
        l_path = Path[path]
        r_path = Path[other.path]

        unless l_path.include?(r_path)
          raise ArgumentError, 'Cannot compare messages from different root paths'
        end

        l_path <=> r_path
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-schema-0.6.0 lib/dry/schema/message.rb
dry-schema-0.5.1 lib/dry/schema/message.rb
dry-schema-0.5.0 lib/dry/schema/message.rb