Sha256: a61647d80613e4f337fff942e41656ceeec1db6976ef83ccb7b9e7f4dbd24101

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'dry/equalizer'

module Dry
  module Schema
    # Message objects used by message sets
    #
    # @api public
    class Message
      include Dry::Equalizer(:predicate, :path, :text, :options)

      attr_reader :predicate, :path, :text, :rule, :args, :options

      # A message sub-type used by OR operations
      #
      # @api public
      class Or
        include Enumerable

        # @api private
        attr_reader :left

        # @api private
        attr_reader :right

        # @api private
        attr_reader :path

        # @api private
        attr_reader :messages

        # @api private
        def initialize(left, right, messages)
          @left = left
          @right = right
          @messages = messages
          @path = left.path
        end

        # 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
      #
      # @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.dup
        @text = text
        @options = options
        @rule = options[:rule]
        @args = options[:args] || EMPTY_ARRAY

        if predicate == :key?
          @path << rule
        end
      end

      # Return a string representation of the message
      #
      # @api public
      def to_s
        text
      end

      # @api private
      def eql?(other)
        other.is_a?(String) ? text == other : super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-0.2.0 lib/dry/schema/message.rb