Skip to content

Commit

Permalink
More question config options (#1)
Browse files Browse the repository at this point in the history
* Add more configuration options

* Reformat headers

* Further updates

* Adding more examples

* Value, not choice

* Adding more notes about the questions

* Address rubocop concerns

* Update README examples

* The more clear... the better

* Split the client into multiple classes inherited

* Refactoring code

* More refactoring

* Update gemspec

* Continuing all the refactoring

* Creating a default value for open-ended questions

* Add newlines after every submission
  • Loading branch information
emmahsax authored Mar 4, 2021
1 parent a682f0b commit 023f4df
Show file tree
Hide file tree
Showing 18 changed files with 1,160 additions and 256 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Metrics/BlockLength:
- spec/**/*
- highline_wrapper.gemspec

Metrics/MethodLength:
Max: 15

Style/AccessModifierDeclarations:
EnforcedStyle: inline

Expand Down
282 changes: 269 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Or install it yourself as:
gem install highline_wrapper
```

### Usage
## Usage

Once this gem is installed, you can then initiate a new `HighlineWrapper` object:

Expand All @@ -41,53 +41,309 @@ Then, add this to the top of your file (or to your gem):
require 'highline_wrapper'
```

Then, you can call its questions to receive answers:
Then, you can call its questions to receive answers. There's several configuration options for each type of question. Look below for the different options for each type of question, and what they each return.

### Open-ended questions

Question configuration options:
* `secret`: defaults to `false`
* `default`: defaults to `''`
* `required`: defaults to `false`

<details><summary>Examples</summary>

```ruby
> HighlineWrapper.new.ask('What is your favorite number?')
What is your favorite number?
four

=> "four"

> HighlineWrapper.new.ask('What is your favorite number?', {required: true})
What is your favorite number?

This question is required.

What is your favorite number?

This question is required.

What is your favorite number?
2

=> "2"

> HighlineWrapper.new.ask('What is your favorite color?')
What is your favorite color?

=> ""

> HighlineWrapper.new.ask('What is your favorite color?', {default: 'orange'})
What is your favorite color?

=> "orange"

> HighlineWrapper.new.ask('Please type your private token:', {secret: true})
Please type your private token?
****************

=> "MY-PRIVATE-TOKEN"

> HighlineWrapper.new.ask('What is your private token?', {secret: true, required: true})
What is your private token?

This question is required.

What is your private token?

This question is required.

What is your private token?

This question is required.

What is your private token?
****************

=> "MY-PRIVATE-TOKEN"
```

</details>

### Yes/No questions

Question configuration options:
* `default`: defaults to `true` (aka 'yes')
* `required`: defaults to `false`

<details><summary>Examples</summary>

```ruby
> HighlineWrapper.new.ask_yes_no('Do you like Ruby?')
Do you like Ruby?
no

=> false

> HighlineWrapper.new.ask_yes_no('Do you like Ruby?')
Do you like Ruby?
yes

=> true

HighlineWrapper.new.ask_multiple_choice('What is your favorite number of t
hese?', ['one', 'two', 'three'])
> HighlineWrapper.new.ask_yes_no('Do you like Ruby?', {default: false})
Do you like Ruby?

=> false

> HighlineWrapper.new.ask_yes_no('Do you like Ruby?', {required: true})
Do you like Ruby?

This question is required.

Do you like Ruby?
No

=> 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
```

</details>

### Multiple choice question

Question configuration options:
* `with_index`: defaults to `false` (particularly handy when there may be duplicate-named but different items in the list—think Sally with ID 45 and Sally with ID 72)
* `default`: defaults to `nil`
* `required`: defaults to `false`

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 `nil`, but could be set to whatever and the code won't care... the question is required)
* If `default` is `nil` and `required` is `false`, and the user skips the question, the answer will be `nil`
* If `with_index` is `true`, a hash will be returned with the choice AND the index of the selection in the original `choices` array
* e.g. `{ value: 'c', index: 2 }`
* If `with_index` is `false`, then a hash of one item will be returned
* e.g. `{ value: 'c' }`

<details><summary>Examples</summary>

```ruby
> HighlineWrapper.new.ask_multiple_choice('What is your favorite number of these?', ['one', 'two', 'three'])
What is your favorite number of these?
1. one
2. two
3. three
2

=> {:value=>"two"}

> HighlineWrapper.new.ask_multiple_choice('What is your favorite number of these?', ['one', 'two', 'three'], {with_index: true})
What is your favorite number of these?
1. one
2. two
3. three
2
=> "two"

> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?
", ['one', 'two','three'])
=> {:value=>"two", :index=>1}

> HighlineWrapper.new.ask_multiple_choice('What is your favorite number of these?', ['one', 'two', 'three'], {with_index: true, default: 'one'})
What is your favorite number of these?
1. one
2. two
3. three

=> {:value=>"one", :index=>0}

> HighlineWrapper.new.ask_multiple_choice('What is your favorite number of these?', ['one', 'two', 'three'], {default: 'three', required: true})
What is your favorite number of these?
1. one
2. two
3. three

This question is required.

What is your favorite number of these?
1. one
2. two
3. three

This question is required.

What is your favorite number of these?
1. one
2. two
3. three
2

=> {:value=>"two"}

> HighlineWrapper.new.ask_multiple_choice('What is your favorite number of these?', ['one', 'two', 'three'], {default: nil})
What is your favorite number of these?
1. one
2. two
3. three

=> nil

> HighlineWrapper.new.ask_multiple_choice('What is your favorite number of these?', ['one', 'two', 'three'], {default: nil, with_index: true})
What is your favorite number of these?
1. one
2. two
3. three

=> nil
```

</details>

### Multiple choice "checkbox" question

Question configuration options:
* `with_indexes`: defaults to `false` (particularly handy when there may be duplicate-named but different items in the list—think Sally with ID 45 and Sally with ID 72)
* `defaults`: defaults to `[]`
* `required`: defaults to `false`

Notes:
* If `required` is `true`, the question will repeat until the user answers the question
* If `required` is `true`, then the `defaults` value will be ignored (this value is defaulting to `[]`, but could be set to whatever and the code won't care... the question is required)
* If `defaults` is `[]` and `required` is `false`, then the method will return an empty array
* If `with_indexes` is `true`, an array of hashes will be returned with the choice AND the index (of the selection in the original `choices` array) in each hash
* e.g. `[{ value: 'a', index: 0 }, { value: 'c', index: 2 }]`
* If `with_indexes` is `false`, then an hashes will be returned where each hash only has a value
* e.g. `[{ value: 'a' }, { value: 'c' }]`

<details><summary>Examples</summary>

```ruby
> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?", ['one', 'two','three'])
What are your favorite numbers of these?
1. one
2. two
3. three
1,3
=> ["one", "three"]
1, 3

=> [{:value=>"one"}, {:value=>"three"}]

> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?
", ['one', 'two','three'], with_indexes: true)
> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?", ['one', 'two','three'], {with_indexes: true})
What are your favorite numbers of these?
1. one
2. two
3. three
1, 3
=> [{:choice=>"one", :index=>0}, {:choice=>"three", :index=>2}]

=> [{:value=>"one", :index=>0}, {:value=>"three", :index=>2}]

> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?", ['one', 'two','three'], {defaults: ['two', 'three']})
What are your favorite numbers of these?
1. one
2. two
3. three

=> [{:value=>"two"}, {:value=>"three"}]

> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?", ['one', 'two','three'], {required: true, with_indexes: true})
What are your favorite numbers of these?
1. one
2. two
3. three

This question is required.

What are your favorite numbers of these?
1. one
2. two
3. three
2

=> [{:value=>"two", :index=>1}]

> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?", ['one', 'two','three'], {required: true, with_indexes: false})
What are your favorite numbers of these?
1. one
2. two
3. three

This question is required.

What are your favorite numbers of these?
1. one
2. two
3. three
1

=> [{:value=>"one"}]

> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?", ['one', 'two','three'], {defaults: []})
What are your favorite numbers of these?
1. one
2. two
3. three

=> []

> HighlineWrapper.new.ask_checkbox("What are your favorite numbers of these?", ['one', 'two','three'], {defaults: [], with_indexes: true})
What are your favorite numbers of these?
1. one
2. two
3. three

=> []
```

### Tests
</details>

## Tests

To run the tests, run `bundle exec rspec` from the command line. GitHub Actions will also run the tests upon every commit to make sure they're up to date and that everything is working correctly. Locally, you can also run `bundle exec guard` to automatically run tests as you develop!

Expand Down
4 changes: 2 additions & 2 deletions highline_wrapper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Gem::Specification.new do |gem|
gem.name = 'highline_wrapper'
gem.version = HighlineWrapper::VERSION
gem.authors = ['Emma Sax']
gem.summary = 'A little wrapper for Highline'
gem.summary = 'A little wrapper for HighLine'
gem.description = 'Making it easier to ask simple questions, such as multiple choice ' \
'questions, yes/no questions, etc, using Highline'
'questions, yes/no questions, etc, using HighLine'
gem.homepage = 'https://github.com/emmahsax/highline_wrapper'
gem.license = 'MIT'
gem.required_ruby_version = '>= 1.9.3'
Expand Down
Loading

0 comments on commit 023f4df

Please sign in to comment.