Sha256: 05f6704e4f36177bcc62146da038eae4de84fda6432aad6d6cb44f1560450fb7

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

require 'dry/core/constants'

module Dry
  module Validation
    class Message
      include Core::Constants
      include Dry::Equalizer(:predicate, :path, :text, :options)

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

      class Or
        attr_reader :left

        attr_reader :right

        attr_reader :path

        attr_reader :messages

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

        def hint?
          false
        end

        def root?
          path.empty?
        end

        def to_s
          [left, right].uniq.join(" #{messages[:or]} ")
        end
      end

      class Check < Message
        def initialize(*args)
          super
          @path = [rule] unless rule.to_s.end_with?('?') || path.include?(rule)
        end
      end

      def self.[](predicate, path, text, options)
        if options[:check]
          Message::Check.new(predicate, path, text, options)
        else
          Message.new(predicate, path, text, options)
        end
      end

      def initialize(predicate, path, text, options)
        @predicate = predicate
        @path = path
        @text = text
        @options = options
        @rule = options[:rule]
        @args = options[:args] || EMPTY_ARRAY

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

      def to_s
        text
      end

      def signature
        @signature ||= [predicate, args, path].hash
      end

      def hint?
        false
      end

      def root?
        path.empty?
      end

      def eql?(other)
        other.is_a?(String) ? text == other : super
      end
    end

    class Hint < Message
      def self.[](predicate, path, text, options)
        Hint.new(predicate, path, text, options)
      end

      def hint?
        true
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-validation-0.10.3 lib/dry/validation/message.rb
dry-validation-0.10.2 lib/dry/validation/message.rb
dry-validation-0.10.1 lib/dry/validation/message.rb
dry-validation-0.10.0 lib/dry/validation/message.rb