Allow `ask' action to customize choices prompt
This commit is contained in:
parent
7e062e06a1
commit
0d0ed541c8
@ -4,9 +4,9 @@ Feature: `ask' recipe keyword
|
|||||||
Given a recipe with:
|
Given a recipe with:
|
||||||
"""
|
"""
|
||||||
task :ask_letter do
|
task :ask_letter do
|
||||||
letter = ask 'Which letter?', %w[A B]
|
letter = ask 'Which letter?', [[:a, ?A], [:b, ?B]]
|
||||||
|
|
||||||
echo letter
|
echo letter.inspect
|
||||||
end
|
end
|
||||||
"""
|
"""
|
||||||
When I execute the recipe interactively
|
When I execute the recipe interactively
|
||||||
@ -17,6 +17,6 @@ Feature: `ask' recipe keyword
|
|||||||
0: A
|
0: A
|
||||||
1: B
|
1: B
|
||||||
Choice:
|
Choice:
|
||||||
B
|
:b
|
||||||
"""
|
"""
|
||||||
And the exit status must be 0
|
And the exit status must be 0
|
||||||
|
@ -10,11 +10,11 @@ module Producer
|
|||||||
|
|
||||||
def prompt(question, choices)
|
def prompt(question, choices)
|
||||||
cs = choices.each_with_index.inject('') do |m, (c, i)|
|
cs = choices.each_with_index.inject('') do |m, (c, i)|
|
||||||
m += "#{i}: #{c}\n"
|
m += "#{i}: #{c.last}\n"
|
||||||
end
|
end
|
||||||
output.puts "#{question}\n#{cs}Choice:"
|
output.puts "#{question}\n#{cs}Choice:"
|
||||||
choice = input.gets
|
choice = input.gets
|
||||||
choices[choice.to_i]
|
choices[choice.to_i].first
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -19,7 +19,7 @@ module Producer::Core
|
|||||||
|
|
||||||
describe '#prompt' do
|
describe '#prompt' do
|
||||||
let(:question) { 'Which letter?' }
|
let(:question) { 'Which letter?' }
|
||||||
let(:choices) { %w[A B] }
|
let(:choices) { [[:a, ?A], [:b, ?B]] }
|
||||||
|
|
||||||
it 'prompts choices' do
|
it 'prompts choices' do
|
||||||
prompter.prompt question, choices
|
prompter.prompt question, choices
|
||||||
@ -34,7 +34,7 @@ module Producer::Core
|
|||||||
it 'returns value for entry chosen by user' do
|
it 'returns value for entry chosen by user' do
|
||||||
input.puts '1'
|
input.puts '1'
|
||||||
input.rewind
|
input.rewind
|
||||||
expect(prompter.prompt question, choices).to eq 'B'
|
expect(prompter.prompt question, choices).to eq :b
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -85,7 +85,7 @@ module Producer::Core
|
|||||||
|
|
||||||
describe '#ask' do
|
describe '#ask' do
|
||||||
let(:question) { 'Which letter?' }
|
let(:question) { 'Which letter?' }
|
||||||
let(:choices) { %w[A B] }
|
let(:choices) { [[:a, ?A], [:b, ?B]] }
|
||||||
let(:prompter_class) { double('prompter class').as_null_object }
|
let(:prompter_class) { double('prompter class').as_null_object }
|
||||||
subject(:ask) { dsl.ask question, choices,
|
subject(:ask) { dsl.ask question, choices,
|
||||||
prompter: prompter_class }
|
prompter: prompter_class }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user