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 command line question
Defined Under Namespace
Classes: Modifier, Validation
Constant Summary collapse
- PREFIX =
' + '
- MULTIPLE_PREFIX =
' * '
- ERROR_PREFIX =
' ERROR:'
Instance Attribute Summary collapse
-
#character ⇒ Object
readonly
Returns character mode.
-
#default_value ⇒ Object
readonly
private
Store default value.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#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.
-
#char(value = nil) ⇒ self
Set if the input is character based or not.
-
#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(value = nil) ⇒ Object
Turn terminal echo on or off.
-
#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 = nil) ⇒ Object
Set expect range of values.
-
#in? ⇒ Boolean
Check if range is set.
-
#initialize(shell, options = {}) ⇒ Question
constructor
Initialize a Question.
-
#mask(char = nil) ⇒ self
Set character for masking the STDIN input.
-
#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.
-
#raw(value = nil) ⇒ Object
Turn raw mode on or off.
-
#raw? ⇒ Boolean
Check if raw mode is set.
-
#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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tty/shell/question.rb', line 55 def initialize(shell, = {}) @shell = shell || Shell.new @required = .fetch(:required) { false } @echo = .fetch(:echo) { true } @raw = .fetch(:raw) { false } @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 } @default_value = nil @error = false @converter = Necromancer.new end |
Instance Attribute Details
#character ⇒ Object (readonly)
Returns character mode
46 47 48 |
# File 'lib/tty/shell/question.rb', line 46 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.
24 25 26 |
# File 'lib/tty/shell/question.rb', line 24 def default_value @default_value end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
41 42 43 |
# File 'lib/tty/shell/question.rb', line 41 def error @error end |
#modifier ⇒ Object (readonly)
Controls character processing of the answer
34 35 36 |
# File 'lib/tty/shell/question.rb', line 34 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.
19 20 21 |
# File 'lib/tty/shell/question.rb', line 19 def statement @statement end |
#valid_values ⇒ Object (readonly)
Returns valid answers
39 40 41 |
# File 'lib/tty/shell/question.rb', line 39 def valid_values @valid_values end |
#validation ⇒ Object (readonly)
Returns the value of attribute validation.
29 30 31 |
# File 'lib/tty/shell/question.rb', line 29 def validation @validation end |
Instance Method Details
#argument(value) ⇒ Question
Ensure that passed argument is present if required option
107 108 109 110 111 112 113 114 115 |
# File 'lib/tty/shell/question.rb', line 107 def argument(value) case value when :required @required = true when :optional @required = false end self end |
#char(value = nil) ⇒ self
Set if the input is character based or not
247 248 249 250 251 |
# File 'lib/tty/shell/question.rb', line 247 def char(value = nil) return @character if value.nil? @character = value self end |
#character? ⇒ Boolean
Check if character intput is set
258 259 260 |
# File 'lib/tty/shell/question.rb', line 258 def character? !!@character end |
#clean ⇒ Object
Reset question object.
153 154 155 156 157 158 |
# File 'lib/tty/shell/question.rb', line 153 def clean @statement = nil @default_value = nil @required = false @modifier = nil end |
#default(value) ⇒ Object
Set default value.
87 88 89 90 91 |
# File 'lib/tty/shell/question.rb', line 87 def default(value) return self if value == '' @default_value = value self end |
#default? ⇒ Boolean
Check if default value is set
98 99 100 |
# File 'lib/tty/shell/question.rb', line 98 def default? !!@default_value end |
#echo(value = nil) ⇒ Object
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.
189 190 191 192 193 |
# File 'lib/tty/shell/question.rb', line 189 def echo(value = nil) return @echo if value.nil? @echo = value self end |
#echo? ⇒ Boolean
Chec if echo is set
198 199 200 |
# File 'lib/tty/shell/question.rb', line 198 def echo? !!@echo end |
#error? ⇒ Boolean
Check if error behaviour is set
181 182 183 |
# File 'lib/tty/shell/question.rb', line 181 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
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/tty/shell/question.rb', line 289 def evaluate_response(value) return default_value if !value && default? check_required(value) return if value.nil? check_valid(value) unless valid_values.empty? within?(value) validation.valid_value?(value) modifier.apply_to(value) end |
#in(value = nil) ⇒ Object
Set expect range of values
267 268 269 270 271 |
# File 'lib/tty/shell/question.rb', line 267 def in(value = nil) return @in if value.nil? @in = @converter.convert(value).to(:range, strict: true) self end |
#in? ⇒ Boolean
Check if range is set
278 279 280 |
# File 'lib/tty/shell/question.rb', line 278 def in? !!@in end |
#mask(char = nil) ⇒ self
Set character for masking the STDIN input
225 226 227 228 229 |
# File 'lib/tty/shell/question.rb', line 225 def mask(char = nil) return @mask if char.nil? @mask = char self end |
#mask? ⇒ Boolean
Check if character mask is set
236 237 238 |
# File 'lib/tty/shell/question.rb', line 236 def mask? !!@mask end |
#modify(*rules) ⇒ Object
Modify string according to the rule given.
165 166 167 168 |
# File 'lib/tty/shell/question.rb', line 165 def modify(*rules) @modifier = Modifier.new(*rules) self end |
#on_error(action = nil) ⇒ Object
Setup behaviour when error(s) occur
173 174 175 176 |
# File 'lib/tty/shell/question.rb', line 173 def on_error(action = nil) @error = action self end |
#prompt(message) ⇒ self
Set a new prompt
78 79 80 81 82 |
# File 'lib/tty/shell/question.rb', line 78 def prompt() self.statement = shell.say shell.prefix + statement self end |
#raw(value = nil) ⇒ Object
Turn raw mode on or off. This enables character-based input.
205 206 207 208 209 |
# File 'lib/tty/shell/question.rb', line 205 def raw(value = nil) return @raw if value.nil? @raw = value self end |
#raw? ⇒ Boolean
Check if raw mode is set
214 215 216 |
# File 'lib/tty/shell/question.rb', line 214 def raw? !!@raw 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.
122 123 124 |
# File 'lib/tty/shell/question.rb', line 122 def required? required end |
#valid(values) ⇒ self
Set expected values
145 146 147 148 |
# File 'lib/tty/shell/question.rb', line 145 def valid(values) @valid_values = values self end |
#validate(value = nil, &block) ⇒ Question
Set validation rule for an argument
133 134 135 136 |
# File 'lib/tty/shell/question.rb', line 133 def validate(value = nil, &block) @validation = Validation.new(value || block) self end |