Sha256: 50129330be45a65e2742d4f2a85dde6c51cd139aea99088398959abe594fa1c7

Contents?: true

Size: 898 Bytes

Versions: 17

Compression:

Stored size: 898 Bytes

Contents

# 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

17 entries across 17 versions & 3 rubygems

Version Path
tty-prompt-0.23.1 lib/tty/prompt/result.rb
tty-prompt-0.23.0 lib/tty/prompt/result.rb
lotrd-0.1.9 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/result.rb
lotrd-0.1.8 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/result.rb
lotrd-0.1.6 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/result.rb
lotrd-0.1.5 vendor/cache/ruby/2.6.0/gems/tty-prompt-0.22.0/lib/tty/prompt/result.rb
tty-prompt-0.22.0 lib/tty/prompt/result.rb
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/tty-prompt-0.21.0/lib/tty/prompt/result.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/tty-prompt-0.21.0/lib/tty/prompt/result.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/tty-prompt-0.21.0/lib/tty/prompt/result.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/tty-prompt-0.21.0/lib/tty/prompt/result.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/tty-prompt-0.21.0/lib/tty/prompt/result.rb
tty-prompt-0.21.0 lib/tty/prompt/result.rb
tty-prompt-0.20.0 lib/tty/prompt/result.rb
tty-prompt-0.19.0 lib/tty/prompt/result.rb
tty-prompt-0.18.1 lib/tty/prompt/result.rb
tty-prompt-0.18.0 lib/tty/prompt/result.rb