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
37
38
39
40
41
# File 'lib/tty/shell/response.rb', line 32

def initialize(question, shell = Shell.new)
  @bool_converter = Conversion::BooleanConverter.new
  @float_converter = Conversion::FloatConverter.new
  @range_converter = Conversion::RangeConverter.new
  @int_converter = Conversion::IntegerConverter.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)


50
51
52
# File 'lib/tty/shell/response.rb', line 50

def read(type = nil)
  question.evaluate_response read_input
end

#read_bool(error = nil) ⇒ Object

Read boolean



152
153
154
# File 'lib/tty/shell/response.rb', line 152

def read_bool(error = nil)
  question.evaluate_response(@bool_converter.convert(read_input))
end

#read_char ⇒ Object

Read answer's first character



80
81
82
83
# File 'lib/tty/shell/response.rb', line 80

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



102
103
104
105
# File 'lib/tty/shell/response.rb', line 102

def read_choice(type = nil)
  question.argument(:required) unless question.default?
  question.evaluate_response read_input
end

#read_date ⇒ Object

Read date



138
139
140
# File 'lib/tty/shell/response.rb', line 138

def read_date
  question.evaluate_response Date.parse(read_input)
end

#read_datetime ⇒ Object

Read datetime



145
146
147
# File 'lib/tty/shell/response.rb', line 145

def read_datetime
  question.evaluate_response DateTime.parse(read_input)
end

#read_email ⇒ String

Read string answer and validate against email regex

Returns:

  • (String)


168
169
170
171
172
# File 'lib/tty/shell/response.rb', line 168

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



159
160
161
# File 'lib/tty/shell/response.rb', line 159

def read_file(error = nil)
  question.evaluate_response File.open(File.join(directory, read_input))
end

#read_float(error = nil) ⇒ Object

Read float value



117
118
119
# File 'lib/tty/shell/response.rb', line 117

def read_float(error = nil)
  question.evaluate_response(@float_converter.convert(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.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tty/shell/response.rb', line 55

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



110
111
112
# File 'lib/tty/shell/response.rb', line 110

def read_int(error = nil)
  question.evaluate_response(@int_converter.convert(read_input))
end

#read_multiple ⇒ Object

Read answer provided on multiple lines



177
178
179
180
181
182
183
184
185
186
# File 'lib/tty/shell/response.rb', line 177

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



191
192
193
194
# File 'lib/tty/shell/response.rb', line 191

def read_password
  question.echo false
  question.evaluate_response read_input
end

#read_range ⇒ Object

Read range expression



131
132
133
# File 'lib/tty/shell/response.rb', line 131

def read_range
  question.evaluate_response(@range_converter.convert(read_input))
end

#read_regex(error = nil) ⇒ Object

Read regular expression



124
125
126
# File 'lib/tty/shell/response.rb', line 124

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



73
74
75
# File 'lib/tty/shell/response.rb', line 73

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



95
96
97
# File 'lib/tty/shell/response.rb', line 95

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



88
89
90
# File 'lib/tty/shell/response.rb', line 88

def read_text
  question.evaluate_response String(read_input)
end