Class: TTY::Shell::Response
- Inherits:
-
Object
- Object
- TTY::Shell::Response
- Defined in:
- lib/tty/shell/response.rb
Overview
A class representing a shell response
Constant Summary collapse
- VALID_TYPES =
[ :boolean, :string, :symbol, :integer, :float, :date, :datetime ]
Instance Method Summary collapse
-
#initialize(question, shell = Shell.new) ⇒ Response
constructor
Initialize a Response.
-
#read(type = nil) ⇒ undefined
Read input from STDIN either character or line.
-
#read_bool(error = nil) ⇒ Object
Read boolean.
-
#read_char ⇒ Object
Read answer’s first character.
-
#read_choice(type = nil) ⇒ Object
Read answer from predifined choicse.
-
#read_date ⇒ Object
Read date.
-
#read_datetime ⇒ Object
Read datetime.
-
#read_email ⇒ String
Read string answer and validate against email regex.
-
#read_file(error = nil) ⇒ Object
Read file contents.
-
#read_float(error = nil) ⇒ Object
Read float value.
- #read_input ⇒ Object private
-
#read_int(error = nil) ⇒ Object
Read integer value.
-
#read_multiple ⇒ Object
Read answer provided on multiple lines.
-
#read_password ⇒ Object
Read password.
-
#read_range ⇒ Object
Read range expression.
-
#read_regex(error = nil) ⇒ Object
Read regular expression.
-
#read_string(error = nil) ⇒ Object
Read answer and cast to String type.
-
#read_symbol(error = nil) ⇒ Object
Read ansewr and cast to Symbol type.
-
#read_text ⇒ Object
Read multiple line answer and cast to String type.
Constructor Details
Instance Method Details
#read(type = nil) ⇒ undefined
Read input from STDIN either character or line
45 46 47 |
# File 'lib/tty/shell/response.rb', line 45 def read(type = nil) question.evaluate_response read_input end |
#read_bool(error = nil) ⇒ Object
Read boolean
147 148 149 |
# File 'lib/tty/shell/response.rb', line 147 def read_bool(error = nil) question.evaluate_response TTY::Coercer::Boolean.coerce read_input end |
#read_char ⇒ Object
Read answer’s first character
75 76 77 78 |
# File 'lib/tty/shell/response.rb', line 75 def read_char question.char(true) question.evaluate_response String(read_input).chars.to_a[0] end |
#read_choice(type = nil) ⇒ Object
Read answer from predifined choicse
97 98 99 100 |
# File 'lib/tty/shell/response.rb', line 97 def read_choice(type = nil) question.argument(:required) unless question.default? question.evaluate_response read_input end |
#read_date ⇒ Object
Read date
133 134 135 |
# File 'lib/tty/shell/response.rb', line 133 def read_date question.evaluate_response Date.parse(read_input) end |
#read_datetime ⇒ Object
Read datetime
140 141 142 |
# File 'lib/tty/shell/response.rb', line 140 def read_datetime question.evaluate_response DateTime.parse(read_input) end |
#read_email ⇒ String
Read string answer and validate against email regex
163 164 165 166 167 |
# File 'lib/tty/shell/response.rb', line 163 def read_email question.validate(/^[a-z0-9._%+-]+@([a-z0-9-]+\.)+[a-z]{2,6}$/i) question.prompt(question.statement) if question.error with_exception { read_string } end |
#read_file(error = nil) ⇒ Object
Read file contents
154 155 156 |
# File 'lib/tty/shell/response.rb', line 154 def read_file(error = nil) question.evaluate_response File.open(File.join(directory, read_input)) end |
#read_float(error = nil) ⇒ Object
Read float value
112 113 114 |
# File 'lib/tty/shell/response.rb', line 112 def read_float(error = nil) question.evaluate_response TTY::Coercer::Float.coerce(read_input) end |
#read_input ⇒ 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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tty/shell/response.rb', line 50 def read_input reader = Reader.new(shell) if question.mask? && question.echo? reader.getc(question.mask) else TTY.terminal.echo(question.echo) do question.character? ? reader.getc(question.mask) : reader.gets end end end |
#read_int(error = nil) ⇒ Object
Read integer value
105 106 107 |
# File 'lib/tty/shell/response.rb', line 105 def read_int(error = nil) question.evaluate_response TTY::Coercer::Integer.coerce(read_input) end |
#read_multiple ⇒ Object
Read answer provided on multiple lines
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/tty/shell/response.rb', line 172 def read_multiple response = '' loop do value = question.evaluate_response read_input break if !value || value == '' next if value !~ /\S/ response << value end response end |
#read_password ⇒ Object
Read password
186 187 188 189 |
# File 'lib/tty/shell/response.rb', line 186 def read_password question.echo false question.evaluate_response read_input end |
#read_range ⇒ Object
Read range expression
126 127 128 |
# File 'lib/tty/shell/response.rb', line 126 def read_range question.evaluate_response TTY::Coercer::Range.coerce(read_input) end |
#read_regex(error = nil) ⇒ Object
Read regular expression
119 120 121 |
# File 'lib/tty/shell/response.rb', line 119 def read_regex(error = nil) question.evaluate_response Kernel.send(:Regex, read_input) end |
#read_string(error = nil) ⇒ Object
Read answer and cast to String type
68 69 70 |
# File 'lib/tty/shell/response.rb', line 68 def read_string(error = nil) question.evaluate_response(String(read_input).strip) end |
#read_symbol(error = nil) ⇒ Object
Read ansewr and cast to Symbol type
90 91 92 |
# File 'lib/tty/shell/response.rb', line 90 def read_symbol(error = nil) question.evaluate_response read_input.to_sym end |
#read_text ⇒ Object
Read multiple line answer and cast to String type
83 84 85 |
# File 'lib/tty/shell/response.rb', line 83 def read_text question.evaluate_response String(read_input) end |