declutter 1.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 +4 -4
- data/README.md +58 -3
- data/lib/declutter.rb +159 -124
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 405d0434633c9cbad8fb8618c6120342e441910fad8134878f5c6bf320ac340c
|
|
4
|
+
data.tar.gz: 828c40921b050e099a612485de9179cd029a0d1d2311109d5fb6c8892412d94c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e386bd19f6a3a63f5742f84da21f4debf70f89bbdc67d41777bd0e0504ad87f8fce080d3fc6be40815c0b7faabdb20220b44cf3a9209060dc9b073b05eb65a6
|
|
7
|
+
data.tar.gz: 67bb244674d0bdc11ffa59eac0b847082888a9190b452326a4b35e0ecd59a35aff247ad6fda6286a41cfbcb9dc68daff17e8de17a1e87900d769e6f1c5716bc3
|
data/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Declutter
|
|
2
|
+
|
|
2
3
|
Declutters your Ruby hashes and arrays by removing empty arrays and hashes.
|
|
3
|
-
Also has options for removing nils, redundancies and falses
|
|
4
|
+
Also has options for removing nils, redundancies and falses, and objects that
|
|
5
|
+
respond to `#declutter`.
|
|
4
6
|
|
|
5
7
|
In this simple example, we create a hash structure that contains an empty array:
|
|
6
8
|
|
|
@@ -126,8 +128,61 @@ declutter.process myhash
|
|
|
126
128
|
puts myhash # => {"episodes"=>[]}
|
|
127
129
|
```
|
|
128
130
|
|
|
131
|
+
### objects of other classes
|
|
132
|
+
|
|
133
|
+
Objects of your own custom class can be decluttered if they have a `#declutter`
|
|
134
|
+
method. Consider these two classes.
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
class KeepMe
|
|
138
|
+
def declutter
|
|
139
|
+
return true
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
class DeleteMe
|
|
144
|
+
def declutter
|
|
145
|
+
return false
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Both classes have declutter methods. `KeepMe#declutter` returns true and
|
|
151
|
+
`DeleteMe#declutter` returns false. In this first example, we use the default
|
|
152
|
+
settings to declutter the hash.
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
hsh = {}
|
|
156
|
+
hsh['keep-it'] = KeepMe.new
|
|
157
|
+
hsh['delete-it'] = DeleteMe.new
|
|
158
|
+
|
|
159
|
+
Declutter.process hsh
|
|
160
|
+
|
|
161
|
+
puts hsh # => {"keep-it"=>#<KeepMe:0x0000559f8574b718>}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The `keep-it` element was kept because `KeepMe#declutter` returns true. However,
|
|
165
|
+
the `delete-it` element was deleted because `DeleteMe#declutter` returns false.
|
|
166
|
+
|
|
167
|
+
To bypass decluttering objects that have a `#declutter` method, use the object
|
|
168
|
+
oriented approach and set `process_others` to false:
|
|
169
|
+
|
|
170
|
+
```ruby
|
|
171
|
+
hsh = {}
|
|
172
|
+
hsh['keep-it'] = KeepMe.new
|
|
173
|
+
hsh['delete-it'] = DeleteMe.new
|
|
174
|
+
|
|
175
|
+
declutter = Declutter.new
|
|
176
|
+
declutter.process_others = false
|
|
177
|
+
declutter.process hsh
|
|
178
|
+
|
|
179
|
+
puts hsh # => {"keep-it"=>#<KeepMe:0x000055e5ffd46f80>, "delete-it"=>#<DeleteMe:0x000055e5ffd46f30>}
|
|
180
|
+
```
|
|
181
|
+
|
|
129
182
|
## History
|
|
130
183
|
|
|
131
184
|
| date | version | notes |
|
|
132
185
|
|--------------|---------|-----------------|
|
|
133
|
-
| May 23, 2023 | 1.0 | Initial upload. |
|
|
186
|
+
| May 23, 2023 | 1.0 | Initial upload. |
|
|
187
|
+
| May 29, 2023 | 1.1 | Added ability to call #declutter on any object that responds to that method. |
|
|
188
|
+
| June 9, 2023 | 1.2 | Changed tests to Minitest. No change to functionality. |
|
data/lib/declutter.rb
CHANGED
|
@@ -2,130 +2,165 @@
|
|
|
2
2
|
# Declutter
|
|
3
3
|
#
|
|
4
4
|
class Declutter
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
5
|
+
# If empty hashes should be deleted. True by default.
|
|
6
|
+
attr_accessor :delete_empty_hashes
|
|
7
|
+
|
|
8
|
+
# If empty arrays should be deleted. True by default.
|
|
9
|
+
attr_accessor :delete_empty_arrays
|
|
10
|
+
|
|
11
|
+
# If redundant array elements should be deleted. False by default.
|
|
12
|
+
attr_accessor :delete_redundancies
|
|
13
|
+
|
|
14
|
+
# If nils should be deleted. False by default.
|
|
15
|
+
attr_accessor :delete_nils
|
|
16
|
+
|
|
17
|
+
# If falses should be deleted. False by default.
|
|
18
|
+
attr_accessor :delete_falses
|
|
19
|
+
|
|
20
|
+
# If objects that respond to #declutter should be processed. Defaults
|
|
21
|
+
# to true.
|
|
22
|
+
attr_accessor :process_others
|
|
23
|
+
|
|
24
|
+
#---------------------------------------------------------------------------
|
|
25
|
+
# initialize
|
|
26
|
+
#
|
|
27
|
+
|
|
28
|
+
# Creates a new Declutter object. Takes no params.
|
|
29
|
+
|
|
30
|
+
def initialize
|
|
31
|
+
@delete_empty_hashes = true
|
|
32
|
+
@delete_empty_arrays = true
|
|
33
|
+
@delete_redundancies = false
|
|
34
|
+
@delete_nils = false
|
|
35
|
+
@delete_falses = false
|
|
36
|
+
@process_others = true
|
|
37
|
+
end
|
|
38
|
+
#
|
|
39
|
+
# initialize
|
|
40
|
+
#---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
#---------------------------------------------------------------------------
|
|
44
|
+
# process
|
|
45
|
+
#
|
|
46
|
+
|
|
47
|
+
# Declutter a hash or array.
|
|
48
|
+
|
|
49
|
+
def process(obj)
|
|
50
|
+
# declutter hash
|
|
51
|
+
if obj.is_a?(Hash)
|
|
52
|
+
process_hash obj
|
|
53
|
+
|
|
54
|
+
# declutter array
|
|
55
|
+
elsif obj.is_a?(Array)
|
|
56
|
+
process_array obj
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
#
|
|
60
|
+
# process
|
|
61
|
+
#---------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
#---------------------------------------------------------------------------
|
|
65
|
+
# self.process
|
|
66
|
+
#
|
|
67
|
+
|
|
68
|
+
# Shortcut for decluttering with default settings.
|
|
69
|
+
|
|
70
|
+
def self.process(obj)
|
|
71
|
+
self.new.process obj
|
|
72
|
+
end
|
|
73
|
+
#
|
|
74
|
+
# self.process
|
|
75
|
+
#---------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# private
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
#---------------------------------------------------------------------------
|
|
83
|
+
# process_hash
|
|
84
|
+
#
|
|
85
|
+
def process_hash(hsh)
|
|
86
|
+
hsh.keys.each do |k|
|
|
87
|
+
v = hsh[k]
|
|
88
|
+
process v
|
|
89
|
+
|
|
90
|
+
# delete or process element
|
|
91
|
+
if not keep_element?(v)
|
|
92
|
+
hsh.delete k
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
#
|
|
97
|
+
# process_hash
|
|
98
|
+
#---------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
#---------------------------------------------------------------------------
|
|
102
|
+
# process_array
|
|
103
|
+
#
|
|
104
|
+
def process_array(arr)
|
|
105
|
+
hold = arr.clone
|
|
106
|
+
arr.clear
|
|
107
|
+
|
|
108
|
+
# add back elements that are acceptable
|
|
109
|
+
hold.each do |child|
|
|
110
|
+
process child
|
|
111
|
+
|
|
112
|
+
if keep_element?(child)
|
|
113
|
+
arr.push child
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# uniq array if necessary
|
|
118
|
+
if @delete_redundancies
|
|
119
|
+
arr.uniq!
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
#
|
|
123
|
+
# process_array
|
|
124
|
+
#---------------------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
#---------------------------------------------------------------------------
|
|
128
|
+
# keep_element
|
|
129
|
+
#
|
|
130
|
+
def keep_element?(v)
|
|
131
|
+
# hash
|
|
132
|
+
if v.is_a?(Hash)
|
|
133
|
+
if @delete_empty_hashes and v.empty?
|
|
134
|
+
return false
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# array
|
|
138
|
+
elsif v.is_a?(Array)
|
|
139
|
+
if @delete_empty_arrays and v.empty?
|
|
140
|
+
return false
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# nil
|
|
144
|
+
elsif v.nil?
|
|
145
|
+
return !@delete_nils
|
|
146
|
+
|
|
147
|
+
# false
|
|
148
|
+
elsif not v
|
|
149
|
+
return !@delete_falses
|
|
150
|
+
|
|
151
|
+
# other class
|
|
152
|
+
elsif @process_others and v.respond_to?('declutter')
|
|
153
|
+
if not v.declutter
|
|
154
|
+
return false
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# if we get this far then we're keeping the element
|
|
159
|
+
return true
|
|
160
|
+
end
|
|
161
|
+
#
|
|
162
|
+
# keep_element
|
|
163
|
+
#---------------------------------------------------------------------------
|
|
129
164
|
end
|
|
130
165
|
#
|
|
131
166
|
# Declutter
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: declutter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: '1.
|
|
4
|
+
version: '1.2'
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mike O'Sullivan
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2023-
|
|
11
|
+
date: 2023-06-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Declutter your Ruby objects by removing empty hashes and arrays
|
|
14
14
|
email: mike@idocs.com
|
|
@@ -22,7 +22,7 @@ homepage: https://github.com/mikosullivan/declutter
|
|
|
22
22
|
licenses:
|
|
23
23
|
- MIT
|
|
24
24
|
metadata: {}
|
|
25
|
-
post_install_message:
|
|
25
|
+
post_install_message:
|
|
26
26
|
rdoc_options: []
|
|
27
27
|
require_paths:
|
|
28
28
|
- lib
|
|
@@ -37,8 +37,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
37
37
|
- !ruby/object:Gem::Version
|
|
38
38
|
version: '0'
|
|
39
39
|
requirements: []
|
|
40
|
-
rubygems_version: 3.
|
|
41
|
-
signing_key:
|
|
40
|
+
rubygems_version: 3.3.5
|
|
41
|
+
signing_key:
|
|
42
42
|
specification_version: 4
|
|
43
43
|
summary: Declutter
|
|
44
44
|
test_files: []
|