Sha256: 67c4442fa532c06ed9e4a75f03d595680a392c0594d7e6f4f4f22bb99ef1ad30

Contents?: true

Size: 916 Bytes

Versions: 5

Compression:

Stored size: 916 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module TTY
  class Prompt
    # Accumulates errors
    class Result
      attr_reader :question, :value, :errors

      def initialize(question, value, errors = [])
        @question = question
        @value  = value
        @errors = errors
      end

      def with(condition = nil, &block)
        validator = (condition || block)
        (new_value, validation_error) = validator.call(question, value)
        accumulated_errors = errors + Array(validation_error)

        if accumulated_errors.empty?
          Success.new(question, new_value)
        else
          Failure.new(question, new_value, accumulated_errors)
        end
      end

      def success?
        is_a?(Success)
      end

      def failure?
        is_a?(Failure)
      end

      class Success < Result
      end

      class Failure < Result
      end
    end
  end # Prompt
end # TTY

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tty-prompt-0.17.2 lib/tty/prompt/result.rb
tty-prompt-0.17.1 lib/tty/prompt/result.rb
tty-prompt-0.17.0 lib/tty/prompt/result.rb
tty-prompt-0.16.1 lib/tty/prompt/result.rb
tty-prompt-0.16.0 lib/tty/prompt/result.rb