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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0dff3001c7395305de08a99ee70c3134c0fa9080
4
- data.tar.gz: af24b882788ca9d2c77aed88735aeedaec5ca86f
3
+ metadata.gz: 304a9893796a5e6ff81f3009a0b8e2800039af05
4
+ data.tar.gz: 42000a5abc70e7ede27e7693cd3210b00737d155
5
5
  SHA512:
6
- metadata.gz: 33cbdd9159524eb0559d2fde19855b8489c5bd53941f6ca7d84dde9eaf293725f35376b72ef559f343bc5256bf8d84bbff54e636bbac6a63d405fdf84cd79403
7
- data.tar.gz: 9eb72223288406e92d158b0acb32b5610ced3e3cfc44655ce33d02ed784a73b666267c2c9074817f48a2f62d29c22cb667145812bf80ebb0db1b9846d6234d85
6
+ metadata.gz: 1bae52dd7a550dceba344764a6f216de2df418c13d99b25f03e43df5f71e176517e348145235cb6a4a7373200c6ccd36b1e6bfbea20a4c91b867d49e647acc5b
7
+ data.tar.gz: 5750950753aac0058e89cbf0a152126feb031a181615ea1ff130ef208a3069f7037ed70a2a44013dddded337848d1e80ffdebde2d6d5015b5d82b88264fa9d07
@@ -0,0 +1,3 @@
1
+ {
2
+ "workbench.colorTheme": "Visual Studio Dark"
3
+ }
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.
@@ -1,11 +1,33 @@
1
1
  require_relative "nested_hash_cleaner/version"
2
2
 
3
3
  module NestedHashCleaner
4
- def self.clean(x,key)
5
- case x
6
- when Hash then x = x.inject({}) {|m, (k, v)| m[k] = clean(v,key) unless k == key ; m }
7
- when Array then x.map! {|e| clean(e,key)}
8
- end
9
- x
10
- end
11
- end
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
@@ -1,3 +1,3 @@
1
1
  module NestedHashCleaner
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  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.1.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-27 00:00:00.000000000 Z
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