Class: UserInput::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/userinput/prompt.rb

Overview

Prompt object

Direct Known Subclasses

Boolean

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ Prompt

Build new prompt object and set defaults



9
10
11
12
13
14
15
16
17
# File 'lib/userinput/prompt.rb', line 9

def initialize(params = {}, &block)
  @attempts = params[:attempts]
  @message = params[:message] || ''
  @separator = params[:separator] || '?'
  @default = params[:default]
  @secret = params[:secret] || false
  @fd = params[:fd] || STDOUT
  @validation = block || params[:validation]
end

Instance Method Details

#askObject

Request user input



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/userinput/prompt.rb', line 21

def ask
  @fd.print full_message
  disable_echo if @secret

  input = _ask
  return input if valid(input)

  check_counter
  ask
ensure
  enable_echo if @secret
end