diff --git a/.rubocop.yml b/.rubocop.yml index bef5f3d..58f6f20 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,7 @@ AllCops: NewCops: enable SuggestExtensions: false - TargetRubyVersion: 2.4 + TargetRubyVersion: 2.5 Exclude: - vendor/**/* diff --git a/README.md b/README.md index f8f5d3e..c184014 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ Notes: * If `required` is `true`, the question will repeat until the user answers the question * If `required` is `true`, then the `default` value will be ignored (defaults to `true`, but could be set to whatever and the code won't care... the question is required) * If `default` is `true` and `required` is `false`, and the user skips the question, the answer will be `true` +* The response to the question MUST be given either as `y`/`n`/`yes`/`no` or with any capitalization... anything else given as a response will be unparseable
Examples @@ -174,7 +175,7 @@ Do you like Ruby? Do you like Ruby? --- This question is required --- Do you like Ruby? -no +N => false > HighlineWrapper.new.ask_yes_no('Do you like Ruby?') @@ -182,7 +183,15 @@ Do you like Ruby? uh-huh --- This question is required --- Do you like Ruby? +YES +=> true + +> HighlineWrapper.new.ask_yes_no('Do you like Ruby?') +Do you like Ruby? yep +--- This question is required --- +Do you like Ruby? +yes => true ``` diff --git a/lib/highline_wrapper/version.rb b/lib/highline_wrapper/version.rb index a7fa765..f8a8040 100644 --- a/lib/highline_wrapper/version.rb +++ b/lib/highline_wrapper/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class HighlineWrapper - VERSION = '1.1.0' + VERSION = '1.2.0' end diff --git a/lib/highline_wrapper/yes_no_question.rb b/lib/highline_wrapper/yes_no_question.rb index b11e427..d2938d7 100644 --- a/lib/highline_wrapper/yes_no_question.rb +++ b/lib/highline_wrapper/yes_no_question.rb @@ -16,10 +16,14 @@ def ask(prompt, options) end private def parse(answer, prompt, options) - return true if answer.include?('y') - return false if answer.include?('n') - - recurse(prompt, nil, options) + case answer + when 'yes', 'y' + true + when 'no', 'n' + false + else + recurse(prompt, nil, options) + end end private def print_default_message(options) diff --git a/spec/highline_wrapper/yes_no_question_spec.rb b/spec/highline_wrapper/yes_no_question_spec.rb index ebc143a..1dc8ab0 100644 --- a/spec/highline_wrapper/yes_no_question_spec.rb +++ b/spec/highline_wrapper/yes_no_question_spec.rb @@ -28,7 +28,7 @@ end it 'should ask the highline client ask' do - expect(highline).to receive(:ask) + expect(highline).to receive(:ask).and_return('Y') subject.ask(Faker::Lorem.sentence, options) end @@ -49,6 +49,12 @@ expect(subject).to receive(:print_default_message) subject.ask(Faker::Lorem.sentence, options) end + + it 'should recurse if the answer given is unparseable' do + allow(highline).to receive(:ask).and_return('yep', 'yessss', 'yes') + expect(subject).to receive(:recurse).exactly(2).times.and_call_original + subject.ask(Faker::Lorem.sentence, options) + end end context 'with required set to true' do