nested_hash_cleaner 0.1.0 → 0.2.0
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 +4 -4
- data/.vscode/settings.json +3 -0
- data/README.md +89 -2
- data/lib/nested_hash_cleaner.rb +30 -8
- data/lib/nested_hash_cleaner/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 304a9893796a5e6ff81f3009a0b8e2800039af05
|
4
|
+
data.tar.gz: 42000a5abc70e7ede27e7693cd3210b00737d155
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bae52dd7a550dceba344764a6f216de2df418c13d99b25f03e43df5f71e176517e348145235cb6a4a7373200c6ccd36b1e6bfbea20a4c91b867d49e647acc5b
|
7
|
+
data.tar.gz: 5750950753aac0058e89cbf0a152126feb031a181615ea1ff130ef208a3069f7037ed70a2a44013dddded337848d1e80ffdebde2d6d5015b5d82b88264fa9d07
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
### Clean nested hash with array of hashes inside
|
23
|
+
### Clean nested hash with array of hashes inside based on key
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
carpool = { jenny: {
|
@@ -92,7 +92,7 @@ carpool = NestedHashCleaner.clean(carpool, :max)
|
|
92
92
|
|
93
93
|
```
|
94
94
|
|
95
|
-
### Clean array of hashes
|
95
|
+
### Clean array of hashes based on key
|
96
96
|
|
97
97
|
```ruby
|
98
98
|
# Array of hashes
|
@@ -106,6 +106,93 @@ list = NestedHashCleaner.clean(list, :age)
|
|
106
106
|
# => [{:alex=>{:city=>"New York"}}, {:jenny=>{:city=>"New York"}}]
|
107
107
|
```
|
108
108
|
|
109
|
+
### Clean nested hash with array of hashes inside based on value
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
carpool = { jenny: {
|
113
|
+
driving_days: [{
|
114
|
+
day: 'monday',
|
115
|
+
coworker: {
|
116
|
+
max: true,
|
117
|
+
justine: true,
|
118
|
+
alex: true
|
119
|
+
}
|
120
|
+
}]
|
121
|
+
},
|
122
|
+
max: {
|
123
|
+
driving_days: [{
|
124
|
+
day: 'thuesday',
|
125
|
+
coworker: {
|
126
|
+
max: true,
|
127
|
+
justine: true,
|
128
|
+
jenny: true
|
129
|
+
}
|
130
|
+
}]
|
131
|
+
},
|
132
|
+
alex: {
|
133
|
+
driving_days: [{
|
134
|
+
day: 'thursday',
|
135
|
+
coworker: {
|
136
|
+
max: true,
|
137
|
+
justine: false,
|
138
|
+
jenny: true
|
139
|
+
}
|
140
|
+
},
|
141
|
+
{
|
142
|
+
day: 'friday',
|
143
|
+
coworker: {
|
144
|
+
max: false,
|
145
|
+
justine: false,
|
146
|
+
jenny: true
|
147
|
+
}
|
148
|
+
}]
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
# remove false entries from carpool hash:
|
153
|
+
carpool = NestedHashCleaner.clean_with_value(carpool, false)
|
154
|
+
|
155
|
+
# {:jenny=>{
|
156
|
+
# :driving_days=>[{
|
157
|
+
# :day=>"monday", :coworker=>{:max=>true, :justine=>true, :alex=>true}
|
158
|
+
# }]
|
159
|
+
# },
|
160
|
+
# :max=>{
|
161
|
+
# :driving_days=>[{
|
162
|
+
# :day=>"thuesday", :coworker=>{:max=>true, :justine=>true, :jenny=>true}
|
163
|
+
# }]
|
164
|
+
# },
|
165
|
+
# :alex=>{
|
166
|
+
# :driving_days=>[{
|
167
|
+
# :day=>"thursday", :coworker=>{:max=>true, :jenny=>true}
|
168
|
+
# },
|
169
|
+
# {
|
170
|
+
# :day=>"friday", :coworker=>{:jenny=>true}
|
171
|
+
# },
|
172
|
+
# {
|
173
|
+
# :day=>"friday", :coworker=>{:jenny=>true}
|
174
|
+
# }]
|
175
|
+
# }
|
176
|
+
# }
|
177
|
+
|
178
|
+
```
|
179
|
+
### Clean array of hashes based on value
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
# Array of hashes
|
183
|
+
|
184
|
+
list = [
|
185
|
+
{ alex: {city: 'New York', age: 38} },
|
186
|
+
{ jenny: {city: 'New York', age: 39} }
|
187
|
+
]
|
188
|
+
|
189
|
+
NestedHashCleaner.clean_with_value(list, 'New York')
|
190
|
+
# [{:alex=>{:age=>38}}, {:jenny=>{:age=>39}}]
|
191
|
+
```
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
109
196
|
## Development
|
110
197
|
|
111
198
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/nested_hash_cleaner.rb
CHANGED
@@ -1,11 +1,33 @@
|
|
1
1
|
require_relative "nested_hash_cleaner/version"
|
2
2
|
|
3
3
|
module NestedHashCleaner
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def clean(x,key)
|
7
|
+
case x
|
8
|
+
when Hash then x = x.inject({}) {|m, (k, v)| m[k] = clean(v,key) unless k == key ; m }
|
9
|
+
when Array then x.map! {|e| clean(e,key)}
|
10
|
+
end
|
11
|
+
x
|
12
|
+
end
|
13
|
+
|
14
|
+
def clean_with_value(x, value)
|
15
|
+
case x
|
16
|
+
when Hash then x = x.inject({}) {|m, (k, v)| m[k] = clean_with_value(v,value) unless v == value ; m }
|
17
|
+
when Array then x.map! {|e| clean_with_value(e,value)}.compact!
|
18
|
+
else x = ((x.respond_to?(:empty?) && x.empty?) ? nil : x)
|
19
|
+
end
|
20
|
+
x
|
21
|
+
end
|
22
|
+
|
23
|
+
def clean_with_values(x, values)
|
24
|
+
values.each do |value|
|
25
|
+
x = clean_with_value(x, value)
|
26
|
+
end
|
27
|
+
x
|
28
|
+
end
|
29
|
+
|
30
|
+
def clean_empty_values(x)
|
31
|
+
clean_with_values(x, [nil, {}, [], ''])
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_hash_cleaner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maurice Kock
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -61,6 +61,7 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
63
|
- ".travis.yml"
|
64
|
+
- ".vscode/settings.json"
|
64
65
|
- Gemfile
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|