Sha256: faed4929ea57b6b26634274e6fc354b37703bc8c3941bb568315a8522fdb68df

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require 'dry/core/constants'

module Dry
  module Validation
    class Result
      include Core::Constants
      include Dry::Equalizer(:output, :errors)
      include Enumerable

      attr_reader :output
      attr_reader :results
      attr_reader :message_compiler
      attr_reader :path

      alias_method :to_hash, :output
      alias_method :to_h, :output # for MRI 2.0, remove it when drop support

      def initialize(output, results, message_compiler, path)
        @output = output
        @results = results
        @message_compiler = message_compiler
        @path = path
      end

      def each(&block)
        output.each(&block)
      end

      def [](name)
        output.fetch(name)
      end

      def success?
        results.empty?
      end

      def failure?
        !success?
      end

      def messages(options = EMPTY_HASH)
        message_set(options).dump
      end

      def errors(options = EMPTY_HASH)
        message_set(options.merge(hints: false)).dump
      end

      def hints(options = EMPTY_HASH)
        message_set(options.merge(failures: false)).dump
      end

      def message_set(options = EMPTY_HASH)
        message_compiler.with(options).(result_ast)
      end

      def to_ast
        if name
          [type, [name, [:set, result_ast]]]
        else
          ast
        end
      end

      def ast(*)
        [:set, result_ast]
      end

      def name
        Array(path).last
      end

      private

      def type
        success? ? :success : :failure
      end

      def result_ast
        @result_ast ||= results.map(&:to_ast)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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