Class: TTY::Shell::Question
- Inherits:
-
Object
- Object
- TTY::Shell::Question
- Includes:
- ResponseDelegation
- Defined in:
- lib/tty/shell/question.rb,
lib/tty/shell/question/modifier.rb,
lib/tty/shell/question/validation.rb
Overview
A class representing a question.
Defined Under Namespace
Classes: Modifier, Validation
Constant Summary collapse
- PREFIX =
" + "
- MULTIPLE_PREFIX =
" * "
- ERROR_PREFIX =
" ERROR:"
Instance Attribute Summary collapse
-
#character(value = (not_set=true)) ⇒ self
readonly
Set if the input is character based or not.
-
#default_value ⇒ Object
readonly
private
Store default value.
-
#echo(value = (not_set=true)) ⇒ Object
readonly
Turn terminal echo on or off.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#mask(character = (not_set=true)) ⇒ self
readonly
Set character for masking the STDIN input.
-
#modifier ⇒ Object
readonly
Controls character processing of the answer.
-
#statement ⇒ Object
private
Store statement.
-
#valid_values ⇒ Object
readonly
Returns valid answers.
-
#validation ⇒ Object
readonly
Returns the value of attribute validation.
Instance Method Summary collapse
-
#argument(value) ⇒ Question
Ensure that passed argument is present if required option.
-
#character? ⇒ Boolean
Check if character intput is set.
-
#clean ⇒ Object
Reset question object.
-
#default(value) ⇒ Object
Set default value.
-
#default? ⇒ Boolean
Check if default value is set.
-
#echo? ⇒ Boolean
Chec if echo is set.
-
#error? ⇒ Boolean
Check if error behaviour is set.
-
#evaluate_response(value) ⇒ Object
private
Check if response matches all the requirements set by the question.
-
#in(value = (not_set=true)) ⇒ Object
Set expect range of values.
-
#in? ⇒ Boolean
Check if range is set.
-
#initialize(shell, options = {}) ⇒ Question
constructor
Initialize a Question.
-
#mask? ⇒ Boolean
Check if character mask is set.
-
#modify(*rules) ⇒ Object
Modify string according to the rule given.
-
#on_error(action = nil) ⇒ Object
Setup behaviour when error(s) occur.
-
#prompt(message) ⇒ self
Set a new prompt.
-
#required? ⇒ Boolean
private
Check if required argument present.
-
#valid(values) ⇒ self
Set expected values.
-
#validate(value = nil, &block) ⇒ Question
Set validation rule for an argument.
Methods included from ResponseDelegation
Methods included from Delegatable
Constructor Details
#initialize(shell, options = {}) ⇒ Question
Initialize a Question
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tty/shell/question.rb', line 66 def initialize(shell, ={}) @shell = shell || Shell.new @required = .fetch(:required) { false } @echo = .fetch(:echo) { true } @mask = .fetch(:mask) { false } @character = .fetch(:character) { false } @in = .fetch(:in) { false } @modifier = Modifier.new .fetch(:modifier) { [] } @valid_values = .fetch(:valid) { [] } @validation = Validation.new .fetch(:validation) { nil } end |
Instance Attribute Details
#character(value = (not_set=true)) ⇒ self (readonly)
Set if the input is character based or not
57 58 59 |
# File 'lib/tty/shell/question.rb', line 57 def character @character end |
#default_value ⇒ Object (readonly)
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.
Store default value.
25 26 27 |
# File 'lib/tty/shell/question.rb', line 25 def default_value @default_value end |
#echo(value = (not_set=true)) ⇒ Object (readonly)
Turn terminal echo on or off. This is used to secure the display so that the entered characters are not echoed back to the screen.
47 48 49 |
# File 'lib/tty/shell/question.rb', line 47 def echo @echo end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
42 43 44 |
# File 'lib/tty/shell/question.rb', line 42 def error @error end |
#mask(character = (not_set=true)) ⇒ self (readonly)
Set character for masking the STDIN input
52 53 54 |
# File 'lib/tty/shell/question.rb', line 52 def mask @mask end |
#modifier ⇒ Object (readonly)
Controls character processing of the answer
35 36 37 |
# File 'lib/tty/shell/question.rb', line 35 def modifier @modifier end |
#statement ⇒ 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.
Store statement.
20 21 22 |
# File 'lib/tty/shell/question.rb', line 20 def statement @statement end |
#valid_values ⇒ Object (readonly)
Returns valid answers
40 41 42 |
# File 'lib/tty/shell/question.rb', line 40 def valid_values @valid_values end |
#validation ⇒ Object (readonly)
Returns the value of attribute validation.
30 31 32 |
# File 'lib/tty/shell/question.rb', line 30 def validation @validation end |
Instance Method Details
#argument(value) ⇒ Question
Ensure that passed argument is present if required option
113 114 115 116 117 118 119 120 121 |
# File 'lib/tty/shell/question.rb', line 113 def argument(value) case value when :required @required = true when :optional @required = false end self end |
#character? ⇒ Boolean
Check if character intput is set
249 250 251 |
# File 'lib/tty/shell/question.rb', line 249 def character? !!@character end |
#clean ⇒ Object
Reset question object.
160 161 162 163 164 165 |
# File 'lib/tty/shell/question.rb', line 160 def clean @statement = nil @default_value = nil @required = false @modifier = nil end |
#default(value) ⇒ Object
Set default value.
93 94 95 96 97 |
# File 'lib/tty/shell/question.rb', line 93 def default(value) return self if value == "" @default_value = value self end |
#default? ⇒ Boolean
Check if default value is set
104 105 106 |
# File 'lib/tty/shell/question.rb', line 104 def default? !!@default_value end |
#echo? ⇒ Boolean
Chec if echo is set
205 206 207 |
# File 'lib/tty/shell/question.rb', line 205 def echo? !!@echo end |
#error? ⇒ Boolean
Check if error behaviour is set
188 189 190 |
# File 'lib/tty/shell/question.rb', line 188 def error? !!@error end |
#evaluate_response(value) ⇒ 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.
Check if response matches all the requirements set by the question
280 281 282 283 284 285 286 287 288 |
# File 'lib/tty/shell/question.rb', line 280 def evaluate_response(value) return default_value if !value && default? check_required value check_valid value unless valid_values.empty? within? value validation.valid_value? value modifier.apply_to value end |
#in(value = (not_set=true)) ⇒ Object
Set expect range of values
258 259 260 261 262 |
# File 'lib/tty/shell/question.rb', line 258 def in(value=(not_set=true)) return @in if not_set @in = TTY::Coercer::Range.coerce value self end |
#in? ⇒ Boolean
Check if range is set
269 270 271 |
# File 'lib/tty/shell/question.rb', line 269 def in? !!@in end |
#mask? ⇒ Boolean
Check if character mask is set
227 228 229 |
# File 'lib/tty/shell/question.rb', line 227 def mask? !!@mask end |
#modify(*rules) ⇒ Object
Modify string according to the rule given.
172 173 174 175 |
# File 'lib/tty/shell/question.rb', line 172 def modify(*rules) @modifier = Modifier.new *rules self end |
#on_error(action = nil) ⇒ Object
Setup behaviour when error(s) occur
180 181 182 183 |
# File 'lib/tty/shell/question.rb', line 180 def on_error(action=nil) @error = action self end |
#prompt(message) ⇒ self
Set a new prompt
84 85 86 87 88 |
# File 'lib/tty/shell/question.rb', line 84 def prompt() self.statement = shell.say shell.prefix + statement self end |
#required? ⇒ Boolean
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.
Check if required argument present.
128 129 130 |
# File 'lib/tty/shell/question.rb', line 128 def required? required end |
#valid(values) ⇒ self
Set expected values
151 152 153 154 |
# File 'lib/tty/shell/question.rb', line 151 def valid(values) @valid_values = values self end |
#validate(value = nil, &block) ⇒ Question
Set validation rule for an argument
139 140 141 142 |
# File 'lib/tty/shell/question.rb', line 139 def validate(value=nil, &block) @validation = Validation.new(value || block) self end |