kernel-fetch_in 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +4 -0
- data/kernel-fetch_in.gemspec +21 -0
- data/lib/fetch_in/version.rb +3 -0
- data/lib/kernel/fetch_in.rb +15 -0
- data/spec/kernel/fetch_in_spec.rb +47 -0
- data/spec/spec_helper.rb +89 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
N2ZiMjU0NWM0Y2Q4ZmNkY2IyNDMwMmM5ODIyZTY3YzVlNWQ1ODgxMw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OThkNGE0NjEzZjc0ODE5MTVkNzUxMWY3OGJlZDllMjEzYTlkYTNmNQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YWIzMzYyZjRmZjVlZmUxMjY5ZjUyOWI1Y2FhNTMxM2NlYjQwNTJmYWM2NTVk
|
10
|
+
YWZhZWU0ZWY4Yzc3ZmFjNGFhNjNkMDA5MWU0YmI3MTBmZGYzYTUzMjZkMWM5
|
11
|
+
NmFmNWY5NzU4MjA2MTQ0NzE5MjUzN2U2MzAxYjUwNWY3MDAyZGU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NDFkOTVlOWU2NjZhM2FmM2MzNGRhZWY4MjE0Y2QxMWNhZmZlODc5NjU1MTFk
|
14
|
+
YWQxZDcwYWNkZTc5YTY4ODQ2MjM1MDlmOWVlZGYzOWNiNzkxZGRjMTg1MDFj
|
15
|
+
ZDM1MDM2Y2I4NzQyNDI2YTMwYTRjOGVmZGRiMjJmOTlmMjJhYzg=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 2.0.0
|
5
|
+
- 2.1.5
|
6
|
+
deploy:
|
7
|
+
provider: rubygems
|
8
|
+
api_key:
|
9
|
+
secure: bgwOZUqdIvxdsvTqxTAp/Ay1QzWlMCcs8cDZ5/EJQWogeHObD9RAeHGchMYJeon1vKgkFBx1pQW/rVzcDzsXii8Cn7u2IYqHeVMWoYkoB/svavY6gGd/amM40HhvyK4HNZpXxZJuqZpJryUD3igNkbdskbYS5iTvE8IWQixmiMU=
|
10
|
+
gem: kernel-fetch_in
|
11
|
+
on:
|
12
|
+
tags: true
|
13
|
+
repo: nicknovitski/kernel-fetch_in
|
14
|
+
all_branches: true
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nick Novitski
|
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,73 @@
|
|
1
|
+
# Kernel#fetch_in
|
2
|
+
|
3
|
+
This gem is for people who need to leap boldy into deep and treacherous hashes.
|
4
|
+
It adds a single method to Kernel, `#fetch_in`. If you know Clojure, you can
|
5
|
+
think of it as a Ruby version of `get-in`.
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# from this:
|
9
|
+
customer = response['customer']
|
10
|
+
billing = customer_hash['billing_address'] if customer
|
11
|
+
country_code = billing['country'] if billing
|
12
|
+
do_something(with: country_code) if country_code
|
13
|
+
|
14
|
+
# to this:
|
15
|
+
country_code = fetch_in(response, 'customer', 'billing_address', 'country') { false }
|
16
|
+
do_something(with: country_code) if country_code
|
17
|
+
|
18
|
+
# or maybe even
|
19
|
+
do_something(with: fetch_in(response, 'customer', 'billing_address', 'country') { 'N/A' }
|
20
|
+
```
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Stick this in your Gemfile:
|
25
|
+
```ruby
|
26
|
+
gem 'kernel-fetch_in'
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```
|
32
|
+
fetch_in(collection, *keys) → obj
|
33
|
+
fetch_in(collection, *keys) { |key| block } → obj
|
34
|
+
```
|
35
|
+
|
36
|
+
`fetch_in` looks up values in nested Hashes (and Arrays, and anything else with
|
37
|
+
a `#fetch` method). First it fetches the value in the passed collection at the first key,
|
38
|
+
then it fetches the value in that value at the next key, if any, and so on for
|
39
|
+
all keys passed, returning the last value fetched.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
bob = {
|
43
|
+
powers: {
|
44
|
+
strength: 'super',
|
45
|
+
toughness: 'extreme'
|
46
|
+
}
|
47
|
+
}
|
48
|
+
helen = {
|
49
|
+
powers: {
|
50
|
+
flexibility: 'extreme',
|
51
|
+
flight: 'expert'
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
fetch_in(bob, :powers, :strength) #=> 'super'
|
56
|
+
fetch_in(helen, :powers, :flight) #=> 'expert'
|
57
|
+
```
|
58
|
+
|
59
|
+
If the optional block parameter is passed, it is evaluated for a default value
|
60
|
+
when any of the passed keys are not present.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
fetch_in(bob, :powers, :flexibility) { 'normal' } #=> 'normal'
|
64
|
+
fetch_in(helen, :weaknesses, :physical) { 'unknown' } #=> 'unknown'
|
65
|
+
```
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. [Fork it](https://github.com/nicknovitski/kernel-fetch_in/fork)
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fetch_in/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'kernel-fetch_in'
|
8
|
+
spec.version = FetchIn::VERSION
|
9
|
+
spec.authors = ['Nick Novitski']
|
10
|
+
spec.email = ['nicknovitski@gmail.com']
|
11
|
+
spec.summary = 'Nested #fetch in functional and OO flavors'
|
12
|
+
spec.homepage = 'https://github.com/nicknovitski/kernel-fetch_in'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(/^spec\//)
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
20
|
+
spec.add_development_dependency 'rspec', '~> 3.1'
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'kernel/fetch_in'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
def fetch_in(collection, *keys)
|
5
|
+
last_key = nil
|
6
|
+
keys.reduce(collection) do |c, k|
|
7
|
+
last_key = k
|
8
|
+
c.fetch(k)
|
9
|
+
end
|
10
|
+
rescue IndexError, KeyError
|
11
|
+
raise unless block_given?
|
12
|
+
yield last_key
|
13
|
+
end
|
14
|
+
private :fetch_in
|
15
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'kernel/fetch_in'
|
2
|
+
|
3
|
+
RSpec.describe Kernel do
|
4
|
+
let(:collection) { double }
|
5
|
+
let(:key) { double }
|
6
|
+
let(:value) { double }
|
7
|
+
|
8
|
+
describe '#fetch_in' do
|
9
|
+
context 'passed a single key argument' do
|
10
|
+
it 'returns what #fetch on the collection argument returns' do
|
11
|
+
allow(collection).to receive(:fetch).with(key) { value }
|
12
|
+
expect(fetch_in(collection, key)).to be value
|
13
|
+
end
|
14
|
+
|
15
|
+
[IndexError, KeyError].each do |err|
|
16
|
+
context "when #fetch raises a #{err}" do
|
17
|
+
before { allow(collection).to receive(:fetch).with(key) { raise err } }
|
18
|
+
|
19
|
+
it 'raises that Error' do
|
20
|
+
expect { fetch_in(collection, key) }.to raise_error err
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'and a block argument is provided' do
|
24
|
+
it 'returns the return value of that block argument' do
|
25
|
+
expect(fetch_in(collection, key) { value }).to be value
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'yields the the key to that block' do
|
29
|
+
expect { |b| fetch_in(collection, key, &b) }.to yield_with_args(key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'passed multiple key arguments' do
|
37
|
+
it 'chains #fetch calls on the collection' do
|
38
|
+
allow(collection).to receive(:fetch).with(key) { value }
|
39
|
+
outer_collection = double
|
40
|
+
key2 = double
|
41
|
+
allow(outer_collection).to receive(:fetch).with(key2) { collection }
|
42
|
+
|
43
|
+
expect(fetch_in(outer_collection, key2, key)).to be value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,89 @@
|
|
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
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
RSpec.configure do |config|
|
18
|
+
# rspec-expectations config goes here. You can use an alternate
|
19
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
20
|
+
# assertions if you prefer.
|
21
|
+
config.expect_with :rspec do |expectations|
|
22
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
23
|
+
# and `failure_message` of custom matchers include text for helper methods
|
24
|
+
# defined using `chain`, e.g.:
|
25
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
26
|
+
# # => "be bigger than 2 and smaller than 4"
|
27
|
+
# ...rather than:
|
28
|
+
# # => "be bigger than 2"
|
29
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
30
|
+
end
|
31
|
+
|
32
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
33
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
34
|
+
config.mock_with :rspec do |mocks|
|
35
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
36
|
+
# a real object. This is generally recommended, and will default to
|
37
|
+
# `true` in RSpec 4.
|
38
|
+
mocks.verify_partial_doubles = true
|
39
|
+
end
|
40
|
+
|
41
|
+
# The settings below are suggested to provide a good initial experience
|
42
|
+
# with RSpec, but feel free to customize to your heart's content.
|
43
|
+
=begin
|
44
|
+
# These two settings work together to allow you to limit a spec run
|
45
|
+
# to individual examples or groups you care about by tagging them with
|
46
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
47
|
+
# get run.
|
48
|
+
config.filter_run :focus
|
49
|
+
config.run_all_when_everything_filtered = true
|
50
|
+
|
51
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
52
|
+
# For more details, see:
|
53
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
54
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
55
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
56
|
+
config.disable_monkey_patching!
|
57
|
+
|
58
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
59
|
+
# be too noisy due to issues in dependencies.
|
60
|
+
config.warnings = true
|
61
|
+
|
62
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
63
|
+
# file, and it's useful to allow more verbose output when running an
|
64
|
+
# individual spec file.
|
65
|
+
if config.files_to_run.one?
|
66
|
+
# Use the documentation formatter for detailed output,
|
67
|
+
# unless a formatter has already been configured
|
68
|
+
# (e.g. via a command-line flag).
|
69
|
+
config.default_formatter = 'doc'
|
70
|
+
end
|
71
|
+
|
72
|
+
# Print the 10 slowest examples and example groups at the
|
73
|
+
# end of the spec run, to help surface which specs are running
|
74
|
+
# particularly slow.
|
75
|
+
config.profile_examples = 10
|
76
|
+
|
77
|
+
# Run specs in random order to surface order dependencies. If you find an
|
78
|
+
# order dependency and want to debug it, you can fix the order by providing
|
79
|
+
# the seed, which is printed after each run.
|
80
|
+
# --seed 1234
|
81
|
+
config.order = :random
|
82
|
+
|
83
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
84
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
85
|
+
# test failures related to randomization by passing the same `--seed` value
|
86
|
+
# as the one that triggered the failure.
|
87
|
+
Kernel.srand config.seed
|
88
|
+
=end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kernel-fetch_in
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Novitski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.1'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- nicknovitski@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- .travis.yml
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- kernel-fetch_in.gemspec
|
56
|
+
- lib/fetch_in/version.rb
|
57
|
+
- lib/kernel/fetch_in.rb
|
58
|
+
- spec/kernel/fetch_in_spec.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
homepage: https://github.com/nicknovitski/kernel-fetch_in
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: ! 'Nested #fetch in functional and OO flavors'
|
84
|
+
test_files:
|
85
|
+
- spec/kernel/fetch_in_spec.rb
|
86
|
+
- spec/spec_helper.rb
|