Class: TTY::Prompt::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/prompt/result.rb

Overview

Accumulates errors

Direct Known Subclasses

Failure, Success

Defined Under Namespace

Classes: Failure, Success

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question, value, errors = []) ⇒ Result

Returns a new instance of Result.


9
10
11
12
13
# File 'lib/tty/prompt/result.rb', line 9

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors


7
8
9
# File 'lib/tty/prompt/result.rb', line 7

def errors
  @errors
end

#questionObject (readonly)

Returns the value of attribute question


7
8
9
# File 'lib/tty/prompt/result.rb', line 7

def question
  @question
end

#valueObject (readonly)

Returns the value of attribute value


7
8
9
# File 'lib/tty/prompt/result.rb', line 7

def value
  @value
end

Instance Method Details

#failure?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/tty/prompt/result.rb', line 31

def failure?
  is_a?(Failure)
end

#success?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/tty/prompt/result.rb', line 27

def success?
  is_a?(Success)
end

#with(condition = nil, &block) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tty/prompt/result.rb', line 15

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