deep_fetch 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +1 -0
- data/deep_fetch.gemspec +19 -0
- data/lib/deep_fetch.rb +6 -0
- data/lib/deep_fetch/core_ext.rb +14 -0
- data/lib/deep_fetch/version.rb +3 -0
- data/spec/deep_fetch_spec.rb +53 -0
- metadata +56 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Tomasz Pewiński
|
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,33 @@
|
|
1
|
+
# DeepFetch
|
2
|
+
|
3
|
+
Easily fetch values from nested ruby hashes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install deep_fetch
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
``` ruby
|
12
|
+
require 'deep_fetch'
|
13
|
+
|
14
|
+
big_hash = {
|
15
|
+
:foo => {
|
16
|
+
:bar => [ 'a', 'b', 'c' ],
|
17
|
+
:baz => :boo
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
big_hash.deep_fetch(:foo, :baz) # :boo
|
22
|
+
big_hash.deep_fetch(:foo, :boo) # raises KeyError
|
23
|
+
big_hash.deep_fetch(:foo, :boo) { "not found" } # "not found"
|
24
|
+
big_hash.deep_fetch(:foo, :bar, 1) # also fetch nested array
|
25
|
+
```
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/deep_fetch.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'deep_fetch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "deep_fetch"
|
8
|
+
gem.version = DeepFetch::VERSION
|
9
|
+
gem.authors = ["Tomasz Pewiński"]
|
10
|
+
gem.email = ["pewniak747@gmail.com"]
|
11
|
+
gem.description = %q{easily fetch values from nested ruby hashes}
|
12
|
+
gem.summary = %q{easily fetch values from nested ruby hashes}
|
13
|
+
gem.homepage = "https://github.com/pewniak747/deep_fetch"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/deep_fetch.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
class Hash
|
2
|
+
def deep_fetch *args, &block
|
3
|
+
return self if args.size == 0
|
4
|
+
|
5
|
+
value = fetch(args.shift, &block)
|
6
|
+
if value.kind_of?(Hash)
|
7
|
+
value.deep_fetch(*args, &block)
|
8
|
+
elsif value.kind_of?(Array) && args.size > 0
|
9
|
+
value = value[args.shift]
|
10
|
+
value.deep_fetch(*args, &block)
|
11
|
+
else value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
require_relative '../lib/deep_fetch'
|
5
|
+
|
6
|
+
describe Hash do
|
7
|
+
describe "deep_fetch" do
|
8
|
+
it "must_equal return value from hash" do
|
9
|
+
{:foo => :bar}.deep_fetch(:foo).must_equal :bar
|
10
|
+
end
|
11
|
+
|
12
|
+
it "must_equal return value from block if key not found" do
|
13
|
+
{:foo => :bar}.deep_fetch(:baz) { :boo } .must_equal :boo
|
14
|
+
end
|
15
|
+
|
16
|
+
it "must_equal rise KeyError if no block and key not found" do
|
17
|
+
lambda do
|
18
|
+
{:foo => :bar}.deep_fetch(:baz).must_equal
|
19
|
+
end.must_raise(KeyError)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "must_equal fetch from deep hash" do
|
23
|
+
{:foo => {:bar => :baz}}.deep_fetch(:foo, :bar).must_equal :baz
|
24
|
+
end
|
25
|
+
|
26
|
+
it "must_equal return value from block if key not found in deep hash" do
|
27
|
+
{:foo => {:bar => :baz}}.deep_fetch(:foo, :baz) { :boo } .must_equal :boo
|
28
|
+
end
|
29
|
+
|
30
|
+
it "must_equal raise KeyError if key not found in deep hash and no block" do
|
31
|
+
lambda do
|
32
|
+
{:foo => {:bar => :baz}}.deep_fetch(:foo, :baz)
|
33
|
+
end.must_raise(KeyError)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "must_equal return values from arrays in deep hash" do
|
37
|
+
{:foo => [nil, {:bar => :baz}]}.deep_fetch(:foo, 1, :bar).must_equal :baz
|
38
|
+
end
|
39
|
+
|
40
|
+
it "must_equal fetch hash from deep hash" do
|
41
|
+
{:foo => {:bar => :baz}}.deep_fetch(:foo).must_equal({:bar => :baz})
|
42
|
+
end
|
43
|
+
|
44
|
+
it "must_equal fetch array from deep hash" do
|
45
|
+
{:foo => {:bar => [:baz]}}.deep_fetch(:foo, :bar).must_equal [:baz]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "must_equal not evaluate a default block when fetching from deep hash" do
|
49
|
+
bomb = ->() { raise "BOOM" }
|
50
|
+
{:foo => {:bar => :baz}}.deep_fetch(:foo, :bar, &bomb).must_equal :baz
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deep_fetch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tomasz Pewiński
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: easily fetch values from nested ruby hashes
|
15
|
+
email:
|
16
|
+
- pewniak747@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- deep_fetch.gemspec
|
26
|
+
- lib/deep_fetch.rb
|
27
|
+
- lib/deep_fetch/core_ext.rb
|
28
|
+
- lib/deep_fetch/version.rb
|
29
|
+
- spec/deep_fetch_spec.rb
|
30
|
+
homepage: https://github.com/pewniak747/deep_fetch
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.23
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: easily fetch values from nested ruby hashes
|
54
|
+
test_files:
|
55
|
+
- spec/deep_fetch_spec.rb
|
56
|
+
has_rdoc:
|