Commit 805be62f authored by Olivier Halligon's avatar Olivier Halligon

Moving `choose_from_array` to UserInterface

parent 547e7e89
......@@ -30,23 +30,6 @@ module Pod
help! 'A valid regular expression is required.'
end
# @return [Fixnum] the index of the chosen array item
#
def choose_from_array(array, message)
array.each_with_index do |item, index|
UI.puts "#{ index + 1 }: #{ item }"
end
UI.puts message
index = UI.gets.chomp.to_i - 1
if index < 0 || index > array.count - 1
raise Informative, "#{ index + 1 } is invalid [1-#{ array.count }]"
else
index
end
end
# @param [String] spec
# The name of the specification.
#
......
......@@ -37,7 +37,7 @@ module Pod
query = @use_regex ? @query : Regexp.escape(@query)
filepath = if @show_all
specs = get_path_of_spec(query, @show_all).split(/\n/)
index = choose_from_array(specs, "Which spec would you like to print [1-#{ specs.count }]? ")
index = UI.choose_from_array(specs, "Which spec would you like to print [1-#{ specs.count }]? ")
specs[index]
else
get_path_of_spec(query)
......
......@@ -38,7 +38,7 @@ module Pod
if @show_all
specs = get_path_of_spec(query, @show_all).split(/\n/)
message = "Which spec would you like to edit [1-#{specs.count}]? "
index = choose_from_array(specs, message)
index = UI.choose_from_array(specs, message)
filepath = specs[index]
else
filepath = get_path_of_spec(query)
......
......@@ -285,6 +285,31 @@ module Pod
end
end
# Asks the use to choose an option from a list
#
# @param [Array<String>] array
# The list of options to choose from
#
# @param [String] message
# The prompt to show as the question to choose from the list
#
# @return [Fixnum] the index of the chosen array item
#
def choose_from_array(array, message)
array.each_with_index do |item, index|
UI.puts "#{ index + 1 }: #{ item }"
end
UI.puts message
index = UI.gets.chomp.to_i - 1
if index < 0 || index > array.count - 1
raise Informative, "#{ index + 1 } is invalid [1-#{ array.count }]"
else
index
end
end
public
# @!group Basic methods
......
......@@ -364,22 +364,6 @@ module Pod
path.should == fixture('spec-repos') + 'master/Specs/AFNetworking/2.4.1/AFNetworking.podspec.json'
end
end
describe '#choose_from_array' do
it 'should return a valid index for the given array' do
UI.next_input = "1\n"
index = @command.send(:choose_from_array, %w(item1 item2 item3), 'A message')
UI.output.should.include "1: item1\n2: item2\n3: item3\nA message\n"
index.should == 0
end
it 'should raise when the index is out of bounds' do
UI.next_input = "4\n"
lambda { @command.send(:choose_from_array, %w(item1 item2 item3), 'A message') }.should.raise Pod::Informative
UI.next_input = "0\n"
lambda { @command.send(:choose_from_array, %w(item1 item2 item3), 'A message') }.should.raise Pod::Informative
end
end
end
#-------------------------------------------------------------------------#
......
......@@ -71,5 +71,21 @@ module Pod
UI.output.should == "#{' ' * 10}- label:\n" + values.map { |v| "#{' ' * 12}- #{v}\n" }.join
end
end
describe '#choose_from_array' do
it 'should return a valid index for the given array' do
UI.next_input = "1\n"
index = UI.choose_from_array(%w(item1 item2 item3), 'A message')
UI.output.should.include "1: item1\n2: item2\n3: item3\nA message\n"
index.should == 0
end
it 'should raise when the index is out of bounds' do
UI.next_input = "4\n"
lambda { UI.choose_from_array(%w(item1 item2 item3), 'A message') }.should.raise Pod::Informative
UI.next_input = "0\n"
lambda { UI.choose_from_array(%w(item1 item2 item3), 'A message') }.should.raise Pod::Informative
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment