Sha256: 3d72b64d5b7c747afdeecd59128f2a1c6da4adcc070016721cb7df24ac0b23c7

Contents?: true

Size: 886 Bytes

Versions: 18

Compression:

Stored size: 886 Bytes

Contents

# encoding: utf-8

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

18 entries across 18 versions & 2 rubygems

Version Path
tty-prompt-0.15.0 lib/tty/prompt/result.rb
tty-prompt-0.14.0 lib/tty/prompt/result.rb
tty-prompt-0.13.2 lib/tty/prompt/result.rb
tty-prompt-0.13.1 lib/tty/prompt/result.rb
tty-prompt-0.13.0 lib/tty/prompt/result.rb
austb-tty-prompt-0.13.0 lib/tty/prompt/result.rb
tty-prompt-0.12.0 lib/tty/prompt/result.rb
tty-prompt-0.11.0 lib/tty/prompt/result.rb
tty-prompt-0.10.1 lib/tty/prompt/result.rb
tty-prompt-0.10.0 lib/tty/prompt/result.rb
tty-prompt-0.9.0 lib/tty/prompt/result.rb
tty-prompt-0.8.0 lib/tty/prompt/result.rb
tty-prompt-0.7.1 lib/tty/prompt/result.rb
tty-prompt-0.7.0 lib/tty/prompt/result.rb
tty-prompt-0.6.0 lib/tty/prompt/result.rb
tty-prompt-0.5.0 lib/tty/prompt/result.rb
tty-prompt-0.4.0 lib/tty/prompt/result.rb
tty-prompt-0.3.0 lib/tty/prompt/result.rb