Class: TTY::Shell::Question

Inherits:
Object
  • Object
show all
Includes:
ResponseDelegation
Defined in:
lib/tty/shell/question.rb,
lib/tty/shell/question/modifier.rb,
lib/tty/shell/question/validation.rb

Overview

A class representing a command line question

Defined Under Namespace

Classes: Modifier, Validation

Constant Summary collapse

PREFIX =
' + '
MULTIPLE_PREFIX =
'   * '
ERROR_PREFIX =
'  ERROR:'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResponseDelegation

#dispatch

Methods included from Delegatable

#delegatable_method

Constructor Details

#initialize(shell, options = {}) ⇒ Question

Initialize a Question



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

def initialize(shell, options = {})
  @shell         = shell || Shell.new
  @required      = options.fetch(:required) { false }
  @echo          = options.fetch(:echo) { true }
  @mask          = options.fetch(:mask) { false  }
  @character     = options.fetch(:character) { false }
  @in            = options.fetch(:in) { false }
  @modifier      = Modifier.new options.fetch(:modifier) { [] }
  @valid_values  = options.fetch(:valid) { [] }
  @validation    = Validation.new options.fetch(:validation) { nil }
  @default_value = nil
  @error         = false
  @range_converter = TTY::Conversion::RangeConverter.new
end

Instance Attribute Details

#characterObject (readonly)

Returns character mode



46
47
48
# File 'lib/tty/shell/question.rb', line 46

def character
  @character
end

#default_valueObject (readonly)

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.

Store default value.



24
25
26
# File 'lib/tty/shell/question.rb', line 24

def default_value
  @default_value
end

#errorObject (readonly)

Returns the value of attribute error.



41
42
43
# File 'lib/tty/shell/question.rb', line 41

def error
  @error
end

#modifierObject (readonly)

Controls character processing of the answer



34
35
36
# File 'lib/tty/shell/question.rb', line 34

def modifier
  @modifier
end

#statementObject

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.

Store statement.



19
20
21
# File 'lib/tty/shell/question.rb', line 19

def statement
  @statement
end

#valid_valuesObject (readonly)

Returns valid answers



39
40
41
# File 'lib/tty/shell/question.rb', line 39

def valid_values
  @valid_values
end

#validationObject (readonly)

Returns the value of attribute validation.



29
30
31
# File 'lib/tty/shell/question.rb', line 29

def validation
  @validation
end

Instance Method Details

#argument(value) ⇒ Question

Ensure that passed argument is present if required option

Returns:



106
107
108
109
110
111
112
113
114
# File 'lib/tty/shell/question.rb', line 106

def argument(value)
  case value
  when :required
    @required = true
  when :optional
    @required = false
  end
  self
end

#char(value = nil) ⇒ self

Set if the input is character based or not

Parameters:

  • value (Boolean) (defaults to: nil)

Returns:

  • (self)


230
231
232
233
234
# File 'lib/tty/shell/question.rb', line 230

def char(value = nil)
  return @character if value.nil?
  @character = value
  self
end

#character?Boolean

Check if character intput is set

Returns:

  • (Boolean)


241
242
243
# File 'lib/tty/shell/question.rb', line 241

def character?
  !!@character
end

#cleanObject

Reset question object.



152
153
154
155
156
157
# File 'lib/tty/shell/question.rb', line 152

def clean
  @statement     = nil
  @default_value = nil
  @required      = false
  @modifier      = nil
end

#default(value) ⇒ Object

Set default value.



86
87
88
89
90
# File 'lib/tty/shell/question.rb', line 86

def default(value)
  return self if value == ''
  @default_value = value
  self
end

#default?Boolean

Check if default value is set

Returns:

  • (Boolean)


97
98
99
# File 'lib/tty/shell/question.rb', line 97

def default?
  !!@default_value
end

#echo(value = nil) ⇒ Object

Turn terminal echo on or off. This is used to secure the display so that the entered characters are not echoed back to the screen.



188
189
190
191
192
# File 'lib/tty/shell/question.rb', line 188

def echo(value = nil)
  return @echo if value.nil?
  @echo = value
  self
end

#echo?Boolean

Chec if echo is set

Returns:

  • (Boolean)


197
198
199
# File 'lib/tty/shell/question.rb', line 197

def echo?
  !!@echo
end

#error?Boolean

Check if error behaviour is set

Returns:

  • (Boolean)


180
181
182
# File 'lib/tty/shell/question.rb', line 180

def error?
  !!@error
end

#evaluate_response(value) ⇒ 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.

Check if response matches all the requirements set by the question

Parameters:

  • value (Object)

Returns:

  • (Object)


272
273
274
275
276
277
278
279
280
281
# File 'lib/tty/shell/question.rb', line 272

def evaluate_response(value)
  return default_value if !value && default?
  check_required(value)
  return if value.nil?

  check_valid(value) unless valid_values.empty?
  within?(value)
  validation.valid_value?(value)
  modifier.apply_to(value)
end

#in(value = nil) ⇒ Object

Set expect range of values

Parameters:

  • value (String) (defaults to: nil)


250
251
252
253
254
# File 'lib/tty/shell/question.rb', line 250

def in(value = nil)
  return @in if value.nil?
  @in = @range_converter.convert(value)
  self
end

#in?Boolean

Check if range is set

Returns:

  • (Boolean)


261
262
263
# File 'lib/tty/shell/question.rb', line 261

def in?
  !!@in
end

#mask(char = nil) ⇒ self

Set character for masking the STDIN input

Parameters:

  • character (String)

Returns:

  • (self)


208
209
210
211
212
# File 'lib/tty/shell/question.rb', line 208

def mask(char = nil)
  return @mask if char.nil?
  @mask = char
  self
end

#mask?Boolean

Check if character mask is set

Returns:

  • (Boolean)


219
220
221
# File 'lib/tty/shell/question.rb', line 219

def mask?
  !!@mask
end

#modify(*rules) ⇒ Object

Modify string according to the rule given.

Parameters:

  • rule (Symbol)


164
165
166
167
# File 'lib/tty/shell/question.rb', line 164

def modify(*rules)
  @modifier = Modifier.new(*rules)
  self
end

#on_error(action = nil) ⇒ Object

Setup behaviour when error(s) occur



172
173
174
175
# File 'lib/tty/shell/question.rb', line 172

def on_error(action = nil)
  @error = action
  self
end

#prompt(message) ⇒ self

Set a new prompt

Parameters:

  • message (String)

Returns:

  • (self)


77
78
79
80
81
# File 'lib/tty/shell/question.rb', line 77

def prompt(message)
  self.statement = message
  shell.say shell.prefix + statement
  self
end

#required?Boolean

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.

Check if required argument present.

Returns:

  • (Boolean)


121
122
123
# File 'lib/tty/shell/question.rb', line 121

def required?
  required
end

#valid(values) ⇒ self

Set expected values

Parameters:

  • values (Array)

Returns:

  • (self)


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

def valid(values)
  @valid_values = values
  self
end

#validate(value = nil, &block) ⇒ Question

Set validation rule for an argument

Parameters:

  • value (Object) (defaults to: nil)

Returns:



132
133
134
135
# File 'lib/tty/shell/question.rb', line 132

def validate(value = nil, &block)
  @validation = Validation.new(value || block)
  self
end