Skip to content

Commit

Permalink
Require specific responses for yes/no questions (#4)
Browse files Browse the repository at this point in the history
Require specific responses for yes/no questions
  • Loading branch information
emmahsax authored Jun 3, 2021
1 parent bebb9a5 commit 9db8221
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 2.4
TargetRubyVersion: 2.5
Exclude:
- vendor/**/*

Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<details><summary>Examples</summary>

Expand Down Expand Up @@ -174,15 +175,23 @@ 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?')
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
```

Expand Down
2 changes: 1 addition & 1 deletion lib/highline_wrapper/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class HighlineWrapper
VERSION = '1.1.0'
VERSION = '1.2.0'
end
12 changes: 8 additions & 4 deletions lib/highline_wrapper/yes_no_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion spec/highline_wrapper/yes_no_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 9db8221

Please sign in to comment.