Class: TTY::Shell::Response

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(question, shell = nil) ⇒ Response

Initialize a Response



25
26
27
28
29
# File 'lib/tty/shell/response.rb', line 25

def initialize(question, shell=nil)
  @question = question
  @shell    = shell || Shell.new
  @reader   = Reader.new(shell)
end

Instance Method Details

#read(type = nil) ⇒ undefined

Read input from STDIN either character or line

Parameters:

  • type (Symbol) (defaults to: nil)

Returns:

  • (undefined)


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_charObject

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_dateObject

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_datetimeObject

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_emailString

Read string answer and validate against email regex

Returns:

  • (String)


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_inputObject

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) do
      question.character? ? reader.getc(question.mask) : reader.gets
    end
  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_multipleObject

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_passwordObject

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_rangeObject

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

Parameters:

  • error (String) (defaults to: nil)

    error to display on failed conversion 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).strip)
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_textObject

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