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
21 22 23 |
# File 'lib/tty/shell/question/validation.rb', line 21 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.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tty/shell/question/validation.rb', line 32 def coerce(validation) case validation when Proc validation when Regexp, String Regexp.new(validation.to_s) else raise TTY::ValidationCoercion, "Wrong type, got #{validation.class}" end end |
#valid_value?(value) ⇒ undefined
Test if the value matches the validation
63 64 65 |
# File 'lib/tty/shell/question/validation.rb', line 63 def valid_value?(value) check_validation(value) end |
#validate? ⇒ Boolean
Check if validation is required
48 49 50 |
# File 'lib/tty/shell/question/validation.rb', line 48 def validate? !!validation end |