class TTY::Prompt::AnswersCollector
Public Class Methods
new(prompt, **options)
click to toggle source
Initialize answer collector
@api public
# File lib/tty/prompt/answers_collector.rb, line 8 def initialize(prompt, **options) @prompt = prompt @answers = options.fetch(:answers) { {} } end
Public Instance Methods
add_answer(answer)
click to toggle source
@api public
# File lib/tty/prompt/answers_collector.rb, line 60 def add_answer(answer) if @answers[@name].is_a?(Array) @answers[@name] << answer else @answers[@name] = answer end end
call(&block)
click to toggle source
Start gathering answers
@return [Hash]
the collection of all answers
@api public
# File lib/tty/prompt/answers_collector.rb, line 19 def call(&block) instance_eval(&block) @answers end
create_collector()
click to toggle source
@api public
# File lib/tty/prompt/answers_collector.rb, line 55 def create_collector self.class.new(@prompt) end
key(name, &block)
click to toggle source
Create answer entry
@example
key(:name).ask("Name?")
@api public
# File lib/tty/prompt/answers_collector.rb, line 30 def key(name, &block) @name = name if block answer = create_collector.call(&block) add_answer(answer) end self end
values(&block)
click to toggle source
Change to collect all values for a key
@example
key(:colors).values.ask("Color?")
@api public
# File lib/tty/prompt/answers_collector.rb, line 45 def values(&block) @answers[@name] = Array(@answers[@name]) if block answer = create_collector.call(&block) add_answer(answer) end self end
Private Instance Methods
method_missing(method, *args, **options, &block)
click to toggle source
@api private
# File lib/tty/prompt/answers_collector.rb, line 71 def method_missing(method, *args, **options, &block) answer = @prompt.public_send(method, *args, **options, &block) add_answer(answer) end