Class: TTY::Prompt::AnswersCollector

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

Instance Method Summary collapse

Constructor Details

#initialize(prompt, options = {}) ⇒ AnswersCollector

Initialize answer collector



9
10
11
12
# File 'lib/tty/prompt/answers_collector.rb', line 9

def initialize(prompt, options = {})
  @prompt  = prompt
  @answers = options.fetch(:answers) { {} }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

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.



53
54
55
56
# File 'lib/tty/prompt/answers_collector.rb', line 53

def method_missing(method, *args, &block)
  answer = @prompt.public_send(method, *args, &block)
  add_answer(answer)
end

Instance Method Details

#add_answer(answer) ⇒ Object



46
47
48
# File 'lib/tty/prompt/answers_collector.rb', line 46

def add_answer(answer)
  @answers[@name] = answer
end

#call(&block) ⇒ Hash

Start gathering answers

Returns:

  • (Hash)

    the collection of all answers



20
21
22
23
# File 'lib/tty/prompt/answers_collector.rb', line 20

def call(&block)
  instance_eval(&block)
  @answers
end

#create_collectorObject



41
42
43
# File 'lib/tty/prompt/answers_collector.rb', line 41

def create_collector
  self.class.new(@prompt)
end

#key(name, &block) ⇒ Object

Create answer entry

Examples:

key(:name).ask('Name?')


31
32
33
34
35
36
37
38
# File 'lib/tty/prompt/answers_collector.rb', line 31

def key(name, &block)
  @name = name
  if block
    answer = create_collector.(&block)
    add_answer(answer)
  end
  self
end