optioning 0.0.1.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +227 -0
- data/Rakefile +4 -0
- data/lib/optioning/version.rb +3 -0
- data/lib/optioning.rb +5 -0
- data/optioning.gemspec +24 -0
- data/tasks/lines.rake +8 -0
- data/tasks/test.rake +26 -0
- data/test/test_helper.rb +5 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fc150f3f4fa42753cb3c1011ad5b600ee99161ba
|
4
|
+
data.tar.gz: 81fab4b72bd887253736ef73fb4f410c46b70bac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e1755b2e41aa8dd4cb98a9aa04603e9c0670722954ccb711ef6480317ba8d4a070cf30cb01587239a98d99ee8279c4ce670db7ea2bb46a46cc305c3112312c1b
|
7
|
+
data.tar.gz: c1072d7a3dd4b834ef7587027f9afc3da134998b5feaa0eae7f3a1da4436dded1b2add9edc2130e71847ce392af650668c39415d947dab658aaddb5ca9733d63
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ricardo Valeriano
|
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,227 @@
|
|
1
|
+
# Optioning
|
2
|
+
|
3
|
+
An easy way to retrieve, store, filter, transform and deprecate `options` passed
|
4
|
+
to a method. Where `options` are the keys our beloved `Hash` as last parameter
|
5
|
+
for a method call.
|
6
|
+
|
7
|
+
## Status
|
8
|
+
[](http://badge.fury.io/rb/hashing)
|
9
|
+
[](http://travis-ci.org/ricardovaleriano/hashing?branch=master)
|
10
|
+
[](https://codeclimate.com/github/ricardovaleriano/hashing)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'optioning'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install optioning
|
25
|
+
|
26
|
+
## Contact
|
27
|
+
|
28
|
+
* API Doc: http://rdoc.info/gems/optioning
|
29
|
+
* Bugs, issues and feature requests: https://github.com/ricardovaleriano/optioning/issues
|
30
|
+
* Support: http://stackoverflow.com/questions/tagged/optioning-ruby
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Given the following `File` class:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class File
|
38
|
+
def initialize(path, commit = nil, content = nil)
|
39
|
+
@path, @commit, @content = path, commit, content
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
And a module called `Hashing`, which defines a method `.serialize` that allow
|
45
|
+
you configure which `ivars` should be used to convert instances of `File` into a
|
46
|
+
`Hash` like this:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
{
|
50
|
+
path: @path,
|
51
|
+
commit: @commit,
|
52
|
+
content: @content
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
And I can configure a transformation in the values of `path`, `commit` and
|
57
|
+
`content` when transforming it into a `Hash` like so:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
require 'hashing'
|
61
|
+
|
62
|
+
class File
|
63
|
+
extend Hashing
|
64
|
+
|
65
|
+
hasherize :path, :commit, to_hash: ->(value) { value.downcase }
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
As the implementor of this module and the `.hasherize` method, I want to be able
|
70
|
+
to use an instance of `Optioning`, so I can store and retrieve the `ivars` and
|
71
|
+
the `options` passed to be used along those `ivars`:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
module Hashing
|
75
|
+
def hasherize(*ivars_and_options)
|
76
|
+
@options = Optioning.new ivars_and_options
|
77
|
+
|
78
|
+
# ...
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
Now in the `Optioning` instance, I can call the following (among others)
|
84
|
+
methods:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
@options.raw
|
88
|
+
# => [:path, :commit, {to_hash: #<Proc:0x007fa4120bd318@(irb):1 (lambda)>}]
|
89
|
+
|
90
|
+
@options.values
|
91
|
+
# => [:path, :commit]
|
92
|
+
|
93
|
+
@options.on :to_hash
|
94
|
+
# => #<Proc:0x007fa4120bd318@(irb):1 (lambda)>
|
95
|
+
```
|
96
|
+
|
97
|
+
### Deprecating options
|
98
|
+
|
99
|
+
Now, following our example, if you need to deprecat the `:to_hash` option in
|
100
|
+
favor of the new `:to` option, you could do:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
def hasherize(*ivars_and_options)
|
104
|
+
@options = Optioning.new ivars_and_options
|
105
|
+
@options.deprecate :to_hash, :to, Date.new(2015, 05, 01)
|
106
|
+
|
107
|
+
# ...
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
This will replace the deprecated option `:to_hash` for the new one named `:to`
|
112
|
+
so you can do the following invocation to recover the value passed to the
|
113
|
+
deprecated `option`:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
@options.on :to_hash
|
117
|
+
|
118
|
+
# => #<Proc:0x007fa4120bd318@(irb):1 (lambda)>
|
119
|
+
```
|
120
|
+
|
121
|
+
#### Deprecation warnings
|
122
|
+
|
123
|
+
You can alert your user about those deprecations using the `#deprecated_warn`
|
124
|
+
method:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
def hasherize(*ivars_and_options)
|
128
|
+
@options = Optioning.new ivars_and_options
|
129
|
+
@options.deprecate :to_hash, :to
|
130
|
+
@options.deprecated_warn
|
131
|
+
|
132
|
+
# ...
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
You can inform the date when the deprecation will not be available anymore.
|
137
|
+
These date will be parte of the deprecation message:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
@options.deprecate :to_hash, :to, Date.new(2015, 05, 01)
|
141
|
+
```
|
142
|
+
|
143
|
+
Or if you prefer, you can specify a version of your software that pretend to
|
144
|
+
remove the deprecated thing:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
@options.deprecate :to_hash, :to, "v2.0.0"
|
148
|
+
```
|
149
|
+
|
150
|
+
### Ignoring unrecongnized options
|
151
|
+
|
152
|
+
If you need, you could fitler the options to mantain just the recognized ones
|
153
|
+
available. To configure the options that matters to your program, use the method
|
154
|
+
`#recognize`. And to warn the user in case a unrecognized option is used, call
|
155
|
+
the `#unrecognized_warn` method:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
def hasherize(*ivars_and_options)
|
159
|
+
@options = Optioning.new ivars_and_options
|
160
|
+
@options.recognize :to_hash
|
161
|
+
@options.unrecognized_warn
|
162
|
+
|
163
|
+
# ...
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
Now, if a user pass an option different than the `:to_hash` one, a warning will
|
168
|
+
inform that the option is not recognized and will be ignored.
|
169
|
+
|
170
|
+
#### Do I Need to register deprecated options as recognized?
|
171
|
+
|
172
|
+
Fortunatelly no. You just need to register your deprecations as usual:
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
def hasherize(*ivars_and_options)
|
176
|
+
@options = Optioning.new ivars_and_options
|
177
|
+
@options.recognize :from
|
178
|
+
@options.deprecate :to_hash, :to
|
179
|
+
@options.deprecated_warn
|
180
|
+
@options.unrecognized_warn
|
181
|
+
|
182
|
+
# ...
|
183
|
+
end
|
184
|
+
```
|
185
|
+
|
186
|
+
The `#deprecate` method already knows what to do (that is register the `option`
|
187
|
+
`:to_hash` as recognized. To sum up, in this last example, the options `:from`
|
188
|
+
and `:to` are already recongnized by the `Optioning` instance.
|
189
|
+
|
190
|
+
### `#process`
|
191
|
+
|
192
|
+
The `#process` method will replace all deprecations, warn about them and warn
|
193
|
+
about unrecognized options all at once, so you can use it like this:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
def hasherize(*ivars_and_options)
|
197
|
+
@options = Optioning.new ivars_and_options
|
198
|
+
@options.recognize :from
|
199
|
+
@options.deprecate :to_hash, :to
|
200
|
+
@options.process
|
201
|
+
|
202
|
+
# ...
|
203
|
+
end
|
204
|
+
```
|
205
|
+
|
206
|
+
### Fluent interface
|
207
|
+
|
208
|
+
And finally, just for a matter of taste, `#deprecate`, `#recognize` and
|
209
|
+
`#process` returns the `Optioning` instance itself, so you can write the last
|
210
|
+
example like this (if you want)
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
def hasherize(*ivars_and_options)
|
214
|
+
@options = Optioning.new(ivars_and_options).recognize(:from)
|
215
|
+
@options.deprecate(:to_hash, :to).process
|
216
|
+
|
217
|
+
# ...
|
218
|
+
end
|
219
|
+
```
|
220
|
+
|
221
|
+
## Contributing
|
222
|
+
|
223
|
+
1. Fork it ( http://github.com/<my-github-username>/optioning/fork )
|
224
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
225
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
226
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
227
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/optioning.rb
ADDED
data/optioning.gemspec
ADDED
@@ -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 'optioning/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "optioning"
|
8
|
+
spec.version = Optioning::VERSION
|
9
|
+
spec.authors = ["Ricardo Valeriano"]
|
10
|
+
spec.email = ["ricardo.valeriano@gmail.com"]
|
11
|
+
spec.summary = %q{An object oriented way to treat our beloved last parameter: Hash.}
|
12
|
+
spec.description = %q{An easy way to retrieve, store, filter and deprecate `options` passed to a method. Where `options` are the keys on our beloved `Hash` as last parameter for a method call.}
|
13
|
+
spec.homepage = ""
|
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.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.0"
|
24
|
+
end
|
data/tasks/lines.rake
ADDED
data/tasks/test.rake
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
desc "run all tests"
|
2
|
+
|
3
|
+
args = ARGV
|
4
|
+
args.shift
|
5
|
+
files = args
|
6
|
+
|
7
|
+
desc "run all the tests"
|
8
|
+
|
9
|
+
task :test do
|
10
|
+
lib = File.expand_path("../lib", __FILE__)
|
11
|
+
$: << lib
|
12
|
+
|
13
|
+
loader = ->(files_list){
|
14
|
+
files_list.each do |f|
|
15
|
+
if File.directory?(f)
|
16
|
+
loader.call(Dir.glob(f + "/**/*_test.rb"))
|
17
|
+
else
|
18
|
+
load f
|
19
|
+
end
|
20
|
+
end
|
21
|
+
}
|
22
|
+
|
23
|
+
require_relative "../test/test_helper"
|
24
|
+
files = Dir.glob("test/**/*_test.rb") unless files.size > 0
|
25
|
+
loader.call(files)
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optioning
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.beta
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ricardo Valeriano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-07 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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: minitest-reporters
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
description: An easy way to retrieve, store, filter and deprecate `options` passed
|
56
|
+
to a method. Where `options` are the keys on our beloved `Hash` as last parameter
|
57
|
+
for a method call.
|
58
|
+
email:
|
59
|
+
- ricardo.valeriano@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- .travis.yml
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/optioning.rb
|
71
|
+
- lib/optioning/version.rb
|
72
|
+
- optioning.gemspec
|
73
|
+
- tasks/lines.rake
|
74
|
+
- tasks/test.rake
|
75
|
+
- test/test_helper.rb
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>'
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.3.1
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.1.11
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: 'An object oriented way to treat our beloved last parameter: Hash.'
|
100
|
+
test_files:
|
101
|
+
- test/test_helper.rb
|