Class: TTY::Shell::Response
- Inherits:
-
Object
- Object
- TTY::Shell::Response
- Defined in:
- lib/tty/shell/response.rb
Overview
A class representing a question.
Constant Summary collapse
- VALID_TYPES =
[:boolean, :string, :symbol, :integer, :float, :date, :datetime]
Instance Method Summary collapse
-
#initialize(question, shell = nil) ⇒ 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
38 39 40 |
# File 'lib/tty/shell/response.rb', line 38 def read(type=nil) question.evaluate_response read_input end |
#read_bool(error = nil) ⇒ Object
Read boolean
140 141 142 |
# File 'lib/tty/shell/response.rb', line 140 def read_bool(error=nil) question.evaluate_response TTY::Coercer::Boolean.coerce read_input end |
#read_char ⇒ Object
Read answer’s first character
68 69 70 71 |
# File 'lib/tty/shell/response.rb', line 68 def read_char question.character true question.evaluate_response String(read_input).chars.to_a[0] end |
#read_choice(type = nil) ⇒ Object
Read answer from predifined choicse
90 91 92 93 |
# File 'lib/tty/shell/response.rb', line 90 def read_choice(type=nil) question.argument(:required) unless question.default? question.evaluate_response read_input end |
#read_date ⇒ Object
Read date
126 127 128 |
# File 'lib/tty/shell/response.rb', line 126 def read_date question.evaluate_response Date.parse(read_input) end |
#read_datetime ⇒ Object
Read datetime
133 134 135 |
# File 'lib/tty/shell/response.rb', line 133 def read_datetime question.evaluate_response DateTime.parse(read_input) end |
#read_email ⇒ String
Read string answer and validate against email regex
156 157 158 159 160 161 162 |
# File 'lib/tty/shell/response.rb', line 156 def read_email question.validate(/^[a-z0-9._%+-]+@([a-z0-9-]+\.)+[a-z]{2,6}$/i) if question.error question.prompt question.statement end with_exception { read_string } end |
#read_file(error = nil) ⇒ Object
Read file contents
147 148 149 |
# File 'lib/tty/shell/response.rb', line 147 def read_file(error=nil) question.evaluate_response File.open(File.join(directory, read_input)) end |
#read_float(error = nil) ⇒ Object
Read float value
105 106 107 |
# File 'lib/tty/shell/response.rb', line 105 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.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tty/shell/response.rb', line 43 def read_input reader = Reader.new(shell) if question.mask? && question.echo? reader.getc(question.mask) else TTY.terminal.echo(question.echo) { question.character? ? reader.getc(question.mask) : reader.gets } end end |
#read_int(error = nil) ⇒ Object
Read integer value
98 99 100 |
# File 'lib/tty/shell/response.rb', line 98 def read_int(error=nil) question.evaluate_response TTY::Coercer::Integer.coerce(read_input) end |
#read_multiple ⇒ Object
Read answer provided on multiple lines
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/tty/shell/response.rb', line 167 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
181 182 183 184 |
# File 'lib/tty/shell/response.rb', line 181 def read_password question.echo false question.evaluate_response read_input end |
#read_range ⇒ Object
Read range expression
119 120 121 |
# File 'lib/tty/shell/response.rb', line 119 def read_range question.evaluate_response TTY::Coercer::Range.coerce(read_input) end |
#read_regex(error = nil) ⇒ Object
Read regular expression
112 113 114 |
# File 'lib/tty/shell/response.rb', line 112 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
61 62 63 |
# File 'lib/tty/shell/response.rb', line 61 def read_string(error=nil) question.evaluate_response String(read_input) end |
#read_symbol(error = nil) ⇒ Object
Read ansewr and cast to Symbol type
83 84 85 |
# File 'lib/tty/shell/response.rb', line 83 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
76 77 78 |
# File 'lib/tty/shell/response.rb', line 76 def read_text question.evaluate_response String(read_input) end |