deep_slice 0.1.2
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/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +71 -0
- data/Rakefile +10 -0
- data/deep_slice.gemspec +22 -0
- data/lib/deep_slice.rb +29 -0
- data/lib/deep_slice/version.rb +3 -0
- data/spec/deep_slice_spec.rb +42 -0
- data/spec/spec_helper.rb +4 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6fcb10055bbec2a9b4ecac6411f1e17ab827f1f0
|
4
|
+
data.tar.gz: 63153c046220f6033ea60b7029720398184c4a6b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e455bc7d56182a56c31d03f7b023df368dada03c6762a71e4154b40057b8cc07fd5bc428c4111de924ce3c0b348fea2cb8461a95dc59d8cecd65f82e9dbbd7f5
|
7
|
+
data.tar.gz: 002b864aa2ddd8bb24194223a3c233525c48265ef85fd279768621bc2b967f2f00cbd2e9ecc530af26ee7942f15bbcd05a01a5aefa04ea6f40171680b108338b
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
deep_slice
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p576
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 SERMO
|
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,71 @@
|
|
1
|
+
# deep_slice
|
2
|
+
|
3
|
+
[](https://travis-ci.org/sermo/deep_slice)
|
4
|
+
|
5
|
+
Like `Hash.slice` from ActiveSupport, but allows for slicing nested keys.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'deep_slice'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install deep_slice
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Take the following hash as an example
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
h ={
|
27
|
+
:a => 1,
|
28
|
+
:b => { :c => 2, :d => 3, :z => -1 },
|
29
|
+
:e => {
|
30
|
+
:f => { :g => 4, :h => 5 }
|
31
|
+
},
|
32
|
+
:i => [
|
33
|
+
{:j => 6, :k => 7},
|
34
|
+
{:j => 8, :k => 9}
|
35
|
+
],
|
36
|
+
:d => nil
|
37
|
+
}
|
38
|
+
```
|
39
|
+
|
40
|
+
Normal hash slicing works:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
h.deep_slice(:a, :d)
|
44
|
+
=> {:a => 1, :d => nil}
|
45
|
+
```
|
46
|
+
|
47
|
+
You can slice to any depth:
|
48
|
+
```ruby
|
49
|
+
h.deep_slice(:e => {:f => :g})
|
50
|
+
=> {:e => {:f => {:g => 4}}}
|
51
|
+
```
|
52
|
+
|
53
|
+
Arrays of hash keys can be specified at any depth:
|
54
|
+
```ruby
|
55
|
+
h.deep_slice(:a, :b => [:c, :d])
|
56
|
+
=> {:a => 1, :b => {:c => 2, :d => 3}}
|
57
|
+
```
|
58
|
+
|
59
|
+
Keys can be sliced out of arrays at any depth:
|
60
|
+
```ruby
|
61
|
+
h.deep_slice(:i => :k)
|
62
|
+
=> {:i => [{:k => 7}, {:k => 9}]}
|
63
|
+
```
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
1. Fork it ( http://github.com/<my-github-username>/deep_slice/fork )
|
68
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
69
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
70
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
71
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/deep_slice.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'deep_slice/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'deep_slice'
|
8
|
+
spec.version = DeepSlice::VERSION
|
9
|
+
spec.authors = ['John Iacona']
|
10
|
+
spec.email = ['john.iacona@sermo.com']
|
11
|
+
spec.summary = %q{Like `Hash.slice` from ActiveSupport, but allows for slicing nested keys.}
|
12
|
+
spec.homepage = 'http://sermo.com'
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency 'rake', ' ~> 10.3'
|
20
|
+
spec.add_development_dependency 'pry', '~> 0.9'
|
21
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
22
|
+
end
|
data/lib/deep_slice.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'deep_slice/version'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
def deep_slice(*keys)
|
5
|
+
# use self.class so sublclasses of Hash can be deep_sliced
|
6
|
+
hash_klass = self.class
|
7
|
+
result = hash_klass.new
|
8
|
+
|
9
|
+
keys.each do |key|
|
10
|
+
if key.is_a?(hash_klass)
|
11
|
+
key.keys.each do |sub_key|
|
12
|
+
if has_key?(sub_key)
|
13
|
+
recursive_keys = key[sub_key]
|
14
|
+
result[sub_key] = \
|
15
|
+
if self[sub_key].is_a?(Array)
|
16
|
+
self[sub_key].map { |val| val.deep_slice(*[recursive_keys].flatten) }
|
17
|
+
else
|
18
|
+
self[sub_key].deep_slice(*[recursive_keys].flatten)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
elsif has_key?(key)
|
23
|
+
result[key] = self[key]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
result
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
let(:hash) do
|
5
|
+
{
|
6
|
+
:a => 1,
|
7
|
+
:b => { :c => 2, :d => 3, :z => -1 },
|
8
|
+
:e => {
|
9
|
+
:f => { :g => 4, :h => 5 }
|
10
|
+
},
|
11
|
+
:i => [
|
12
|
+
{:j => 6, :k => 7},
|
13
|
+
{:j => 8, :k => 9}
|
14
|
+
],
|
15
|
+
:d => nil
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'slices depth one keys' do
|
20
|
+
expect(hash.deep_slice(:a)).to eq({:a => 1})
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'slices depth two keys' do
|
24
|
+
expect(hash.deep_slice(:b => :c)).to eq({:b => {:c => 2}})
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'slices depth three keys' do
|
28
|
+
expect(hash.deep_slice(:e => {:f => :g})).to eq({:e => {:f => {:g => 4}}})
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'slices nil valued keys' do
|
32
|
+
expect(hash.deep_slice(:d)).to eq({:d => nil})
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'handles arrays of slice values' do
|
36
|
+
expect(hash.deep_slice(:b => [:c, :z])).to eq({:b => {:c => 2, :z => -1}})
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'slices values out of arrays' do
|
40
|
+
expect(hash.deep_slice(:i => :j)).to eq({:i => [{:j => 6}, {:j => 8}]})
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deep_slice
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Iacona
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-25 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- john.iacona@sermo.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .ruby-gemset
|
64
|
+
- .ruby-version
|
65
|
+
- .travis.yml
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- deep_slice.gemspec
|
71
|
+
- lib/deep_slice.rb
|
72
|
+
- lib/deep_slice/version.rb
|
73
|
+
- spec/deep_slice_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: http://sermo.com
|
76
|
+
licenses: []
|
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.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Like `Hash.slice` from ActiveSupport, but allows for slicing nested keys.
|
98
|
+
test_files:
|
99
|
+
- spec/deep_slice_spec.rb
|
100
|
+
- spec/spec_helper.rb
|