hashfilter 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 +19 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/hash_filter.gemspec +27 -0
- data/lib/hash_filter/version.rb +3 -0
- data/lib/hash_filter.rb +33 -0
- data/spec/lib/hash_filter_spec.rb +41 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8e2cfc3f59384e15b6b8388816e33b8aefb1c460
|
4
|
+
data.tar.gz: 431c503ccb34aae514a5f23a785f0ff90d9cba49
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45635fc49d2a978c4b46099240bf2482eb95ba284dfb60d32c0c3b9fda5b07ec85150b85a3854be0e3bf3216db486efea2e3c965ff01cff2ffc0217c1f9dc381
|
7
|
+
data.tar.gz: 82e5d7cf877abb8b6749229e0cff39bfe0cfa7eb2e4f4d1f0c19e88b9faf821935459284cf5cb888c58329205265df151918c82bc18589b904a57f38d05e2273
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format nested --color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 svs
|
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,54 @@
|
|
1
|
+
# HashFilter
|
2
|
+
|
3
|
+
Recursively filter hashes
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hash_filter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hash_filter
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
hash =
|
23
|
+
{
|
24
|
+
:data => {
|
25
|
+
:a => 1,
|
26
|
+
:b => [1,2,3],
|
27
|
+
:c => {a: 1, b: 2}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
keep_filter = {data: {b: true, c: {b: true}}}
|
34
|
+
|
35
|
+
HashFilter.keep(hash, keep_filter) # {data: {b: [1,2,3], c: {b: 2}}}
|
36
|
+
|
37
|
+
drop_filter = {data: { c: {b: true}}}
|
38
|
+
|
39
|
+
HashFilter.drop(hash, drop_filter) # :data => {
|
40
|
+
# :a => 1,
|
41
|
+
# :b => [1,2,3],
|
42
|
+
# :c => {a: 1}}
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( http://github.com/<my-github-username>/hash_filter/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/hash_filter.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_filter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hashfilter"
|
8
|
+
spec.version = HashFilter::VERSION
|
9
|
+
spec.authors = ["svs"]
|
10
|
+
spec.email = ["svs@svs.io"]
|
11
|
+
spec.summary = %q{Recursively filter hashes}
|
12
|
+
spec.homepage = "https://github.com/svs/hash_filter"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'activesupport'
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "awesome_print"
|
26
|
+
spec.add_development_dependency "pry-byebug"
|
27
|
+
end
|
data/lib/hash_filter.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "hash_filter/version"
|
2
|
+
require 'active_support/core_ext/hash'
|
3
|
+
module HashFilter
|
4
|
+
|
5
|
+
|
6
|
+
def self.keep(hash, filter)
|
7
|
+
Hash[hash.map do |k,v|
|
8
|
+
if filter[k]
|
9
|
+
if filter[k].is_a?(Hash)
|
10
|
+
[k,self.keep(v, filter[k])]
|
11
|
+
else
|
12
|
+
[k, v]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end.compact]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.drop(hash, filter)
|
19
|
+
Hash[hash.map do |k,v|
|
20
|
+
if filter[k]
|
21
|
+
if filter[k].is_a?(Hash)
|
22
|
+
[k,self.drop(v, filter[k])]
|
23
|
+
else
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
else
|
27
|
+
[k,v]
|
28
|
+
end
|
29
|
+
end.compact]
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'hash_filter'
|
2
|
+
require 'awesome_print'
|
3
|
+
describe HashFilter do
|
4
|
+
|
5
|
+
let(:hash) {
|
6
|
+
{
|
7
|
+
:data => {
|
8
|
+
:a => 1,
|
9
|
+
:b => [1,2,3],
|
10
|
+
:c => {a: 1, b: 2}
|
11
|
+
}
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
|
16
|
+
let(:keep_filter) {
|
17
|
+
{data: {b: true, c: {b: true}}}
|
18
|
+
}
|
19
|
+
|
20
|
+
let(:drop_filter) {
|
21
|
+
{data: { c: {b: true}}}
|
22
|
+
}
|
23
|
+
|
24
|
+
|
25
|
+
it "should keep properly" do
|
26
|
+
expect(HashFilter.keep(hash,keep_filter)).to eql({data: {b: [1,2,3], c: {b: 2}}})
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should drop properly" do
|
30
|
+
expect(HashFilter.drop(hash,drop_filter)).to eql( {
|
31
|
+
:data => {
|
32
|
+
:a => 1,
|
33
|
+
:b => [1,2,3],
|
34
|
+
:c => {a: 1}
|
35
|
+
}
|
36
|
+
})
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashfilter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- svs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: awesome_print
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- svs@svs.io
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- hash_filter.gemspec
|
111
|
+
- lib/hash_filter.rb
|
112
|
+
- lib/hash_filter/version.rb
|
113
|
+
- spec/lib/hash_filter_spec.rb
|
114
|
+
homepage: https://github.com/svs/hash_filter
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.3.0
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Recursively filter hashes
|
138
|
+
test_files:
|
139
|
+
- spec/lib/hash_filter_spec.rb
|
140
|
+
has_rdoc:
|