Class: TTY2::Prompt::Result
- Inherits:
-
Object
- Object
- TTY2::Prompt::Result
- Defined in:
- lib/tty2/prompt/result.rb
Overview
Accumulates errors
Defined Under Namespace
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#question ⇒ Object
readonly
Returns the value of attribute question.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(question, value, errors = []) ⇒ Result
constructor
A new instance of Result.
- #success? ⇒ Boolean
- #with(condition = nil, &block) ⇒ Object
Constructor Details
#initialize(question, value, errors = []) ⇒ Result
Returns a new instance of Result.
9 10 11 12 13 |
# File 'lib/tty2/prompt/result.rb', line 9 def initialize(question, value, errors = []) @question = question @value = value @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/tty2/prompt/result.rb', line 7 def errors @errors end |
#question ⇒ Object (readonly)
Returns the value of attribute question.
7 8 9 |
# File 'lib/tty2/prompt/result.rb', line 7 def question @question end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
7 8 9 |
# File 'lib/tty2/prompt/result.rb', line 7 def value @value end |
Instance Method Details
#failure? ⇒ Boolean
31 32 33 |
# File 'lib/tty2/prompt/result.rb', line 31 def failure? is_a?(Failure) end |
#success? ⇒ Boolean
27 28 29 |
# File 'lib/tty2/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/tty2/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 |