Class: TTY::Shell::Response

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

Constructor Details

#initialize(question, shell = Shell.new) ⇒ Response

Initialize a Response



32
33
34
35
36
# File 'lib/tty/shell/response.rb', line 32

def initialize(question, shell = Shell.new)
  @question = question
  @shell    = shell
  @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)


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_charObject

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_dateObject

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_datetimeObject

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_emailString

Read string answer and validate against email regex

Returns:

  • (String)


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_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.



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_multipleObject

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_passwordObject

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_rangeObject

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

Parameters:

  • error (String) (defaults to: nil)

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

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