0.0
No commit activity in last 3 years
No release in over 3 years
OptionPicker allows you to create a list of allowed options, and specify a fallback. When given a value the picker will then return the value if it is in the allowed list, otherwise it will return the default value.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
>= 0
 Project Readme

OptionPicker

When building query strings to pass on to an API it is nice to be able to manage the allowed options for each parameter in a nice way.

Installation

Add this line to your application's Gemfile:

gem 'option_picker'

And then execute:

$ bundle

Or install it yourself as:

$ gem install option_picker

Usage

Define your default option and an array of allowed options:

SORT_DIR_OPTIONS = OptionPicker::Options.new('asc', ['asc', 'desc'])

Then pass in the value you want to test. The value will be returned if it is allowed, otherwise the default value will be returned.

SORT_DIR_OPTIONS['asc'] # => 'asc' SORT_DIR_OPTIONS['desc'] # => 'desc' SORT_DIR_OPTIONS['foo'] # => 'asc'

You can always index by String or Symbol. So the following are identical:

SORT_DIR_OPTIONS['desc'] # => 'desc' SORT_DIR_OPTIONS[:desc] # => 'desc'

Option Translation

Sometimes the option values you accept locally are not the same as those that you need to pass on. To a 3rd party API, for example. To aid in this, instead of a simple array of allowed values, you can use a Hash as the second argument to perform a translation:

SORT_BY_OPTIONS = OptionPicker::Options.new('creationTimestamp', { created_on: 'creationTimestamp', published_on: 'publishedTimestamp', title: 'title' })

SORT_BY_OPTIONS['created_on'] # => 'creationTimestamp' SORT_BY_OPTIONS['title'] # => 'title' SORT_BY_OPTIONS['foo'] # => 'creationTimestamp'

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request