Class: InputsTTYMethods::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/clir/TTY-Prompt.cls.rb

Overview

— Class InputsTTYMethods::Responder —

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt, type, *args, &block) ⇒ Responder

Returns a new instance of Responder.



218
219
220
221
222
223
# File 'lib/clir/TTY-Prompt.cls.rb', line 218

def initialize(prompt, type, *args, &block)
  @prompt = prompt
  @type   = type
  @args   = args
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



212
213
214
# File 'lib/clir/TTY-Prompt.cls.rb', line 212

def args
  @args
end

#inputObject (readonly)

The input for this responder



216
217
218
# File 'lib/clir/TTY-Prompt.cls.rb', line 216

def input
  @input
end

#promptObject (readonly)

Returns the value of attribute prompt.



212
213
214
# File 'lib/clir/TTY-Prompt.cls.rb', line 212

def prompt
  @prompt
end

#typeObject (readonly)

Returns the value of attribute type.



212
213
214
# File 'lib/clir/TTY-Prompt.cls.rb', line 212

def type
  @type
end

Instance Method Details

#__askObject

— Twin TTY::Prompt methods — — They treat input value as required —



258
259
260
# File 'lib/clir/TTY-Prompt.cls.rb', line 258

def __ask
  # nothing to do (even default value is treated above)
end

#__multi_selectObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/clir/TTY-Prompt.cls.rb', line 282

def __multi_select
  return unless input.is_a?(Hash)
  @input = 
    if input.key?('names')
      find_in_choices(/^(#{input['names'].join('|')})$/i)
    elsif input.key?('items') || input.key?('index')
      (input['items']||input['index']).map { |n| choices[n.to_i - 1][:value] }
    elsif input.key?('rname')
      find_in_choices(/#{input['rname']}/i)
    elsif input.key?('rnames')
      find_in_choices(/(#{input['rnames'].join('|')})/i)
    else
      input
    end
end

#__multilineObject



262
263
264
265
266
# File 'lib/clir/TTY-Prompt.cls.rb', line 262

def __multiline
  case input
  when /CTRL[ _\-]D/, '^D' then @input = ''
  end
end

#__noObject



301
302
303
304
# File 'lib/clir/TTY-Prompt.cls.rb', line 301

def __no
  self.__yes
  @input = !@input 
end

#__selectObject



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/clir/TTY-Prompt.cls.rb', line 268

def __select
  return unless input.is_a?(Hash)
  @input =
    if input.key?('name')
      find_in_choices(/^#{input['name']}$/i).first
    elsif input.key?('rname')
      find_in_choices(/#{input['rname']}/).first
    elsif input.key?('item') || input.key?('index')
      choices[(input['item']||input['index']) - 1][:value]
    else
      input
    end
end

#__sliderObject



306
307
308
# File 'lib/clir/TTY-Prompt.cls.rb', line 306

def __slider
  @input = input.to_i
end

#__yesObject



298
299
300
# File 'lib/clir/TTY-Prompt.cls.rb', line 298

def __yes
  @input = ['o','y','true',"\n",'1','oui','yes'].include?(input.to_s.downcase)
end

#choice(menu, value = nil, options = nil) ⇒ Object

For select or multi-select, to add a choice



359
360
361
362
# File 'lib/clir/TTY-Prompt.cls.rb', line 359

def choice menu, value = nil, options = nil
  @choices ||= []
  @choices << {name:menu, value:value||menu, options:options}
end

#choices(vals = nil) ⇒ Object

To define and get select choices



347
348
349
350
351
352
353
354
# File 'lib/clir/TTY-Prompt.cls.rb', line 347

def choices(vals = nil)
  if vals.nil?
    return @choices ||= []
  else
    @choices ||= []
    @choices += vals
  end
end

#convert(*arg, &block) ⇒ Object

For real TTYPrompt compatibility



400
401
402
403
404
405
406
# File 'lib/clir/TTY-Prompt.cls.rb', line 400

def convert(*arg, &block)
  # Ne rien faire
  if block_given?
    # TODO Plus tard on pourra vérifier les conversions aussi
    # Mais attention : c'est pas forcément donné par block
  end
end

#default(value) ⇒ Object

To define the default value



375
376
377
# File 'lib/clir/TTY-Prompt.cls.rb', line 375

def default(value)
  @default = value
end

#default_valueObject

Returns the default value.

Returns:

  • the default value



331
332
333
334
335
336
337
338
339
# File 'lib/clir/TTY-Prompt.cls.rb', line 331

def default_value
  if defined?(@default)
    return @default
  elsif args[-1].is_a?(Hash)
    args.last[:default]
  else
    nil
  end
end

#enum(*args) ⇒ Object

For real TTYPrompt compatibility



381
382
383
# File 'lib/clir/TTY-Prompt.cls.rb', line 381

def enum(*args)
  # Ne rien faire
end

#find_in_choices(search) ⇒ Object

Returns all values that match search in choices.

Returns:

  • all values that match search in choices



313
314
315
316
317
318
319
# File 'lib/clir/TTY-Prompt.cls.rb', line 313

def find_in_choices(search)
  @choices.select do |choix|
    choix[:name].match?(search)
  end.map do |choix|
    choix[:value]
  end
end

#help(str) ⇒ Object

For real TTYPrompt compatibility



387
388
389
# File 'lib/clir/TTY-Prompt.cls.rb', line 387

def help(str)
  # Ne rien faire
end

#per_page(*args) ⇒ Object

To define the number of items displayed with a select or multiselect



368
369
370
# File 'lib/clir/TTY-Prompt.cls.rb', line 368

def per_page(*args)
  # Rien à faire ici
end

#responseObject

Main method to evaluate the respond to give (the respond that user would have given)

value has been given, return the default value)

Returns:

  • the fake-user input transformed (for example, if no



231
232
233
234
235
236
237
# File 'lib/clir/TTY-Prompt.cls.rb', line 231

def response
  @input  = prompt.next_input
  if treat_special_input_values
    self.send(tty_method)
  end
  return input
end

#treat_special_input_valuesObject

below)

Returns:

  • false if no more treatment (no send to tty_method



245
246
247
248
249
250
251
252
253
# File 'lib/clir/TTY-Prompt.cls.rb', line 245

def treat_special_input_values
  case input.to_s.upcase
  when /CTRL[ _\-]C/, 'EXIT', '^C' then exit 0
  when 'DEFAULT', 'DÉFAUT'
    @input = default_value
    return false
  end
  return true
end

#tty_methodObject

p.e. ‘__ask’ ou ‘__select’

Returns:

  • self Twin TTY method



323
324
325
# File 'lib/clir/TTY-Prompt.cls.rb', line 323

def tty_method
  @tty_method ||= "__#{type}".to_sym
end

#validate(*arg, &block) ⇒ Object

For real TTYPrompt compatibility



393
394
395
396
# File 'lib/clir/TTY-Prompt.cls.rb', line 393

def validate(*arg, &block)
  # Ne rien faire
  # TODO Plus tard on pourra vérifier les validations aussi
end