lib/prompts/form.rb in prompts-0.3.0 vs lib/prompts/form.rb in prompts-0.3.1
- old
+ new
@@ -8,58 +8,63 @@
instance.submit
end
def initialize
@content = Prompts::Content.new
- @prompts = []
- @results = []
+ @index = 0
+ @prompts = {}
+ @results = {}
end
def content(&block)
yield @content
@content
end
- def text(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
+ def text(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, name: nil, &block)
prompt = TextPrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
- @prompts << prompt
+ key = name || (@index += 1)
+ @prompts[key] = prompt
end
- def select(label: nil, options: nil, prompt: "> ", hint: nil, default: nil, validate: nil, &block)
+ def select(label: nil, options: nil, prompt: "> ", hint: nil, default: nil, validate: nil, name: nil, &block)
prompt = SelectPrompt.new(label: label, options: options, prompt: prompt, hint: hint, default: default, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
- @prompts << prompt
+ key = name || (@index += 1)
+ @prompts[key] = prompt
end
- def pause(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
+ def pause(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, name: nil, &block)
prompt = PausePrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
- @prompts << prompt
+ key = name || (@index += 1)
+ @prompts[key] = prompt
end
- def confirm(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, &block)
+ def confirm(label: nil, prompt: "> ", hint: nil, default: nil, required: false, validate: nil, name: nil, &block)
prompt = ConfirmPrompt.new(label: label, prompt: prompt, hint: hint, default: default, required: required, validate: validate)
yield(prompt) if block
prepend_form_content_to_prompt(prompt)
- @prompts << prompt
+ key = name || (@index += 1)
+ @prompts[key] = prompt
end
def submit
- @prompts.each do |prompt|
- @results << prompt.ask
+ @prompts.each do |key, prompt|
+ @results[key] = prompt.ask
end
@results
end
private
def prepend_form_content_to_prompt(prompt)
prompt.prepare_content
- @content.gap
+ prompt.prepend_content([SPACE])
prompt.prepend_content(*@content.slots)
end
end
end