Skip to content

Commit

Permalink
remove options support for array, multiple choice, and hash
Browse files Browse the repository at this point in the history
  • Loading branch information
brewster1134 committed May 14, 2016
1 parent 2a7a769 commit 3d2ff03
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
2 changes: 1 addition & 1 deletion i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ en:

# TYPE SPECIFIC MESSAGES
multiple_choice:
already_selected: "Option %{choice} has already been selected"
prompt: 'Choose one of the following options:'
selected_choices: Selected Options

Expand All @@ -37,6 +36,7 @@ en:
keys: "You are missing values for %{keys} %{description}"
multiple_choice:
length: "You have selected %{value_length} options, but you need between %{allowed_values} %{description}"
delete_required_key: "You cannot delete the required key %{key}"
range:
string:
symbol:
51 changes: 36 additions & 15 deletions lib/cli_miami/ask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def request_until_valid options, allow_empty_string = false
alias_method :request_string, :request_until_valid
alias_method :request_symbol, :request_until_valid

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def request_array options
array = []
value_options = CliMiami.get_options(options[:value_options] || {})
Expand All @@ -96,9 +96,16 @@ def request_array options
while array.length < options[:max]
response = request_until_valid value_options, true

# user attempting to finish entering items
if response.empty?
break if CliMiami::Validation.new(array, options).valid?
redo

# remove item if user enters it twice
elsif array.include? response
array.delete response

# add response to the list
else
array << response
end
Expand All @@ -109,8 +116,9 @@ def request_array options

array
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
def request_multiple_choice options
selected_choices = nil
selected_choice_indexes = []
Expand Down Expand Up @@ -143,14 +151,12 @@ def request_multiple_choice options
# convert human readable response to array index
response_index = response - 1

# prevent the same option from being added multiple times
# add choice to array
if selected_choice_indexes.include? response_index
CliMiami::S.ay I18n.t('cli_miami.core.multiple_choice.already_selected', choice: response), :cli_miami_fail
redo
selected_choice_indexes.delete response_index
else
selected_choice_indexes << response_index
end

# add choice to array
selected_choice_indexes << response_index
end

# update user
Expand All @@ -164,9 +170,9 @@ def request_multiple_choice options

selected_choices
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

# rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/PerceivedComplexity
# rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
def request_hash options
hash = {}
options[:keys] ||= []
Expand Down Expand Up @@ -202,9 +208,24 @@ def request_hash options
break if CliMiami::Validation.new(hash, options).valid?
redo
else
# request value
CliMiami::S.ay I18n.t('cli_miami.core.enter_value_for', key: user_key), :cli_miami_instruction
hash[user_key] = request_until_valid value_options, true

# request value
user_value = request_until_valid value_options, true

if user_value.empty?
if options[:keys].include? user_key
# prevent deleting required keys
CliMiami::S.ay I18n.t('cli_miami.errors.multiple_choice.delete_required_key', key: user_key), :cli_miami_fail
redo
else
# delete user-specified key
hash.delete user_key
end
else
# set user-defined key
hash[user_key] = user_value
end
end
end

Expand All @@ -213,9 +234,9 @@ def request_hash options

hash
end
# rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/PerceivedComplexity
# rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
def request_range options
start_value = nil
end_value = nil
Expand Down Expand Up @@ -246,5 +267,5 @@ def request_range options
request_range options
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
end
36 changes: 30 additions & 6 deletions spec/lib/cli_miami/ask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
expect(ask.value).to eq %w(foo bar)
end

it 'should allow user to remove entered values' do
allow($stdin).to receive(:gets).and_return 'foo', 'bar', 'foo', ''
ask = CliMiami::A.sk @q, type: :array
expect(ask.value).to eq %w(bar)
end

it 'should prevent user from entering less than the minimum' do
allow($stdin).to receive(:gets).and_return 'foo', '', 'bar', ''
ask = CliMiami::A.sk @q, type: :array, min: 2
Expand All @@ -49,6 +55,12 @@
end

describe 'MULTIPLE_CHOICE' do
it 'should allow a user to remove selections' do
allow($stdin).to receive(:gets).and_return '1', '2', '1', ''
ask = CliMiami::A.sk @q, type: :multiple_choice, choices: ['option 1', 'option 2', 'option 3']
expect(ask.value).to eq ['option 2']
end

it 'should not allow a user to select less than the min amount of choices' do
allow($stdin).to receive(:gets).and_return '2', '', '1', ''
ask = CliMiami::A.sk @q, type: :multiple_choice, min: 2, choices: ['option 1', 'option 2', 'option 3']
Expand Down Expand Up @@ -78,15 +90,21 @@
ask = CliMiami::A.sk @q, type: :multiple_choice, choices: ['option 1', 'option 2', 'option 3']
expect(ask.value).to eq ['option 3', 'option 1', 'option 2']
end

it 'should not allow a user to select the same option twice' do
allow($stdin).to receive(:gets).and_return '2', '2', '1', ''
ask = CliMiami::A.sk @q, type: :multiple_choice, choices: ['option 1', 'option 2', 'option 3']
expect(ask.value).to eq ['option 2', 'option 1']
end
end

describe 'HASH' do
it 'should allow user to overwrite keys' do
allow($stdin).to receive(:gets).and_return 'foo1', '1', 'bar1', '2', 'foo1', '3', ''
ask = CliMiami::A.sk @q, type: :hash
expect(ask.value).to eq(foo1: '3', bar1: '2')
end

it 'should allow user to remove keys' do
allow($stdin).to receive(:gets).and_return 'foo1', '1', 'bar1', '2', 'foo1', '', ''
ask = CliMiami::A.sk @q, type: :hash
expect(ask.value).to eq(bar1: '2')
end

it 'should allow user to enter keys & values until they enter an empty key string' do
allow($stdin).to receive(:gets).and_return 'foo1', '1', 'bar1', '2', ''
ask = CliMiami::A.sk @q, type: :hash
Expand All @@ -111,6 +129,12 @@
ask = CliMiami::A.sk @q, type: :hash, keys: [:foo4]
expect(ask.value).to eq(foo4: '1', bar4: '2')
end

it 'should not allow user to delete required keys' do
allow($stdin).to receive(:gets).and_return 'foo1', 'foo', '', 'bar', 'bar1', ''
ask = CliMiami::A.sk @q, type: :hash, keys: [:foo]
expect(ask.value).to eq(foo: 'foo1', bar: 'bar1')
end
end
end

Expand Down

0 comments on commit 3d2ff03

Please sign in to comment.