Class: TTY::Shell::Question::Validation
- Inherits:
-
Object
- Object
- TTY::Shell::Question::Validation
- Defined in:
- lib/tty/shell/question/validation.rb
Overview
A class representing question validation.
Instance Method Summary collapse
-
#coerce(validation) ⇒ Object
private
Convert validation into known type.
-
#initialize(validation = nil) ⇒ undefined
constructor
private
Initialize a Validation.
-
#valid_value?(value) ⇒ undefined
Test if the value matches the validation.
-
#validate? ⇒ Boolean
Check if validation is required.
Constructor Details
#initialize(validation = nil) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a Validation
19 20 21 |
# File 'lib/tty/shell/question/validation.rb', line 19 def initialize(validation = nil) @validation = validation ? coerce(validation) : validation end |
Instance Method Details
#coerce(validation) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert validation into known type.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tty/shell/question/validation.rb', line 30 def coerce(validation) case validation when Proc validation when Regexp, String Regexp.new(validation.to_s) else fail TTY::ValidationCoercion, "Wrong type, got #{validation.class}" end end |
#valid_value?(value) ⇒ undefined
Test if the value matches the validation
61 62 63 |
# File 'lib/tty/shell/question/validation.rb', line 61 def valid_value?(value) check_validation(value) end |
#validate? ⇒ Boolean
Check if validation is required
46 47 48 |
# File 'lib/tty/shell/question/validation.rb', line 46 def validate? !!validation end |