option_picker 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/lib/option_picker.rb +22 -0
- data/lib/option_picker/version.rb +3 -0
- data/option_picker.gemspec +24 -0
- data/spec/lib/option_picker_spec.rb +35 -0
- data/spec/spec_helper.rb +25 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 58424d3796eca05fc1997df504cdc250b3dede55
|
4
|
+
data.tar.gz: 30dcae4e941fdf9042fd90cb60ac0f344315739b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbaad0c770fe0397c5e499254268ca6ed581f5d1d348d1e5b3117f7d3ab84de8db94364ded7b81925a63b09cfc23bc8be315f4106bbd345e20e5b8a71b01e7b0
|
7
|
+
data.tar.gz: 1effd154f06578b58f827f766576525b0dcc764fc0f56a03160584396ffe834ac1e078078bc80c0fb4623a3307f4b257658828c520c8d08dacf9a880dbb225ea
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Tony Marklove
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# OptionPicker
|
2
|
+
|
3
|
+
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.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'option_picker'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install option_picker
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Define your default option and an array of allowed options:
|
22
|
+
|
23
|
+
SORT_DIR_OPTIONS = OptionPicker::Options.new('asc', ['asc', 'desc'])
|
24
|
+
|
25
|
+
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.
|
26
|
+
|
27
|
+
SORT_DIR_OPTIONS['asc'] # => 'asc'
|
28
|
+
SORT_DIR_OPTIONS['desc'] # => 'desc'
|
29
|
+
SORT_DIR_OPTIONS['foo'] # => 'asc'
|
30
|
+
|
31
|
+
You can always index by `String` or `Symbol`. So the following are identical:
|
32
|
+
|
33
|
+
SORT_DIR_OPTIONS['desc'] # => 'desc'
|
34
|
+
SORT_DIR_OPTIONS[:desc] # => 'desc'
|
35
|
+
|
36
|
+
### Option Translation
|
37
|
+
|
38
|
+
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:
|
39
|
+
|
40
|
+
SORT_BY_OPTIONS = OptionPicker::Options.new('creationTimestamp', {
|
41
|
+
created_on: 'creationTimestamp',
|
42
|
+
published_on: 'publishedTimestamp',
|
43
|
+
title: 'title'
|
44
|
+
})
|
45
|
+
|
46
|
+
SORT_BY_OPTIONS['created_on'] # => 'creationTimestamp'
|
47
|
+
SORT_BY_OPTIONS['title'] # => 'title'
|
48
|
+
SORT_BY_OPTIONS['foo'] # => 'creationTimestamp'
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "option_picker/version"
|
2
|
+
|
3
|
+
module OptionPicker
|
4
|
+
|
5
|
+
class Options
|
6
|
+
|
7
|
+
def initialize(default_value, options={})
|
8
|
+
options_hash = options.is_a?(Array) ? Hash[options.map {|o| [o,o] }] : options
|
9
|
+
|
10
|
+
@options = options_hash.each_with_object(Hash.new(default_value)) do |(key, value), memo|
|
11
|
+
memo[key.to_sym] = value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](index)
|
16
|
+
index = index.to_sym if index.respond_to?(:to_sym)
|
17
|
+
@options[index]
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'option_picker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "option_picker"
|
8
|
+
spec.version = OptionPicker::VERSION
|
9
|
+
spec.authors = ["Tony Marklove"]
|
10
|
+
spec.email = ["tony@new-bamboo.co.uk"]
|
11
|
+
spec.description = %q{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.}
|
12
|
+
spec.summary = %q{Pick from a list of allowed options, otherwise fallback to a default value.}
|
13
|
+
spec.homepage = "https://github.com/jjbananas/option_picker"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe OptionPicker::Options do
|
4
|
+
|
5
|
+
let(:options) { OptionPicker::Options.new('foo') }
|
6
|
+
|
7
|
+
it "returns the default option for any missing keys" do
|
8
|
+
expect(options[:something]).to eql('foo')
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with a hash of mappings" do
|
12
|
+
let(:options) { OptionPicker::Options.new('two', foo: 'one', bar: 'two') }
|
13
|
+
|
14
|
+
it "maps the input key to the expected output" do
|
15
|
+
expect(options[:foo]).to eql('one')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "with an array of options" do
|
20
|
+
let(:options) { OptionPicker::Options.new('two', ['one', 'two']) }
|
21
|
+
|
22
|
+
it "returns the key if it exists in the list of options" do
|
23
|
+
expect(options['one']).to eql('one')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "mixed key types" do
|
28
|
+
let(:options) { OptionPicker::Options.new('two', foo: 'one', bar: 'two') }
|
29
|
+
|
30
|
+
it "finds the matching key" do
|
31
|
+
expect(options['foo']).to eql('one')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
require 'dotenv'
|
8
|
+
require 'vcr'
|
9
|
+
require 'factory_girl'
|
10
|
+
require 'webmock/rspec'
|
11
|
+
require 'rspec_structure_matcher'
|
12
|
+
|
13
|
+
require File.dirname(__FILE__) + '/../lib/option_picker.rb'
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
17
|
+
config.run_all_when_everything_filtered = true
|
18
|
+
config.filter_run :focus
|
19
|
+
|
20
|
+
# Run specs in random order to surface order dependencies. If you find an
|
21
|
+
# order dependency and want to debug it, you can fix the order by providing
|
22
|
+
# the seed, which is printed after each run.
|
23
|
+
# --seed 1234
|
24
|
+
config.order = 'random'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: option_picker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Marklove
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: OptionPicker allows you to create a list of allowed options, and specify
|
56
|
+
a fallback. When given a value the picker will then return the value if it is in
|
57
|
+
the allowed list, otherwise it will return the default value.
|
58
|
+
email:
|
59
|
+
- tony@new-bamboo.co.uk
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/option_picker.rb
|
70
|
+
- lib/option_picker/version.rb
|
71
|
+
- option_picker.gemspec
|
72
|
+
- spec/lib/option_picker_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/jjbananas/option_picker
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.1.11
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Pick from a list of allowed options, otherwise fallback to a default value.
|
98
|
+
test_files:
|
99
|
+
- spec/lib/option_picker_spec.rb
|
100
|
+
- spec/spec_helper.rb
|