declutter 1.0 → 1.1
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 +57 -3
- data/lib/declutter.rb +36 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9dbc1622dadba96ab55c42f8e7a477dac2ed481557703e074b096b6c4fffaa5
|
4
|
+
data.tar.gz: d147bd94493a740d1854a94bed6adb1d09891217ff60637b8c4d75dbc331e5a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c08b8c3c36da7bd3b3af62eded77f0843b4af7f8b77ce01c12d5af248e6584cc3daf5942568c617b7a4c2bba75e736420dc1490b3bfb71e770fa65885118677
|
7
|
+
data.tar.gz: 47d13c23af1e61d2e4d2c2b7809c175fc61e194dd86ddf3d57ee7a748548fb65efcd50527374d11ed38af99c031034840ac00a44ca15ed5ca174dc01a0446780
|
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,60 @@ 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. |
|
data/lib/declutter.rb
CHANGED
@@ -2,21 +2,38 @@
|
|
2
2
|
# Declutter
|
3
3
|
#
|
4
4
|
class Declutter
|
5
|
+
# If empty hashes should be deleted. True by default.
|
5
6
|
attr_accessor :delete_empty_hashes
|
7
|
+
|
8
|
+
# If empty arrays should be deleted. True by default.
|
6
9
|
attr_accessor :delete_empty_arrays
|
10
|
+
|
11
|
+
# If redundant array elements should be deleted. False by default.
|
7
12
|
attr_accessor :delete_redundancies
|
13
|
+
|
14
|
+
# If nils should be deleted. False by default.
|
8
15
|
attr_accessor :delete_nils
|
16
|
+
|
17
|
+
# If falses should be deleted. False by default.
|
9
18
|
attr_accessor :delete_falses
|
10
19
|
|
20
|
+
# If objects that respond to #declutter should be processed Defaults
|
21
|
+
# to true.
|
22
|
+
attr_accessor :process_others
|
23
|
+
|
11
24
|
#---------------------------------------------------------------------------
|
12
25
|
# initialize
|
13
26
|
#
|
27
|
+
|
28
|
+
# Creates a new Declutter object. Takes no params.
|
29
|
+
|
14
30
|
def initialize
|
15
31
|
@delete_empty_hashes = true
|
16
32
|
@delete_empty_arrays = true
|
17
33
|
@delete_redundancies = false
|
18
34
|
@delete_nils = false
|
19
35
|
@delete_falses = false
|
36
|
+
@process_others = true
|
20
37
|
end
|
21
38
|
#
|
22
39
|
# initialize
|
@@ -25,11 +42,16 @@ class Declutter
|
|
25
42
|
|
26
43
|
#---------------------------------------------------------------------------
|
27
44
|
# process
|
28
|
-
# Process hash or array. Anything else is left as is.
|
29
45
|
#
|
46
|
+
|
47
|
+
# Declutter a hash or array.
|
48
|
+
|
30
49
|
def process(obj)
|
50
|
+
# declutter hash
|
31
51
|
if obj.is_a?(Hash)
|
32
52
|
process_hash obj
|
53
|
+
|
54
|
+
# declutter array
|
33
55
|
elsif obj.is_a?(Array)
|
34
56
|
process_array obj
|
35
57
|
end
|
@@ -42,6 +64,9 @@ class Declutter
|
|
42
64
|
#---------------------------------------------------------------------------
|
43
65
|
# self.process
|
44
66
|
#
|
67
|
+
|
68
|
+
# Shortcut for decluttering with default settings.
|
69
|
+
|
45
70
|
def self.process(obj)
|
46
71
|
self.new.process obj
|
47
72
|
end
|
@@ -50,6 +75,10 @@ class Declutter
|
|
50
75
|
#---------------------------------------------------------------------------
|
51
76
|
|
52
77
|
|
78
|
+
# private
|
79
|
+
private
|
80
|
+
|
81
|
+
|
53
82
|
#---------------------------------------------------------------------------
|
54
83
|
# process_hash
|
55
84
|
#
|
@@ -118,6 +147,12 @@ class Declutter
|
|
118
147
|
# false
|
119
148
|
elsif not v
|
120
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
|
121
156
|
end
|
122
157
|
|
123
158
|
# if we get this far then we're keeping the element
|
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.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike O'Sullivan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-29 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
|