declutter 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +133 -0
  3. data/lib/declutter.rb +132 -0
  4. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 607c7167c298669ff221cbd00d7d8d984f1a1c936e2fbf2e5ae74cd4903cdfef
4
+ data.tar.gz: 654e400b781e186c44477c88f547c06935d4f733c41685524c6896d5b7e953b3
5
+ SHA512:
6
+ metadata.gz: b32c7fda4d2420cf4bc7a0cc99b8016d70638ef3773e4297e0419b9773a4974ac00552a729726cadb120ebc09f0ca7f3b1917c02084bb0555d33bf661b18cfe1
7
+ data.tar.gz: 96acb8ef11156a427bc1447099acfa2273b302e0f020321ee6c35dd1b4ff8f5b2693b7d0b869f70741494594deb0dffed41f439fde32743bff14e615daf97253
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # declutter
2
+ Declutters your Ruby hashes and arrays by removing empty arrays and hashes.
3
+ Also has options for removing nils, redundancies and falses.
4
+
5
+ In this simple example, we create a hash structure that contains an empty array:
6
+
7
+ ```ruby
8
+ #!/usr/bin/ruby -w
9
+ require 'declutter'
10
+
11
+ myhash = { 'title' => 'Titus Andronicus', 'episodes' => [] }
12
+ puts myhash # => {"title"=>"Titus Andronicus", "episodes"=>[]}
13
+ Declutter.process myhash
14
+ puts myhash # => {"title"=>"Titus Andronicus"}
15
+ ```
16
+
17
+ By running the hash through `Declutter.process`, we remove the empty array. If
18
+ an array contains only an empty array, then the whole array structure is
19
+ removed:
20
+
21
+ ```ruby
22
+ myhash = { 'title' => 'Titus Andronicus', 'episodes' => ['scenes'=>[]] };
23
+ puts myhash # => {"title"=>"Titus Andronicus", "episodes"=>[{"scenes"=>[]}]}
24
+ Declutter.process myhash
25
+ puts myhash # => {"title"=>"Titus Andronicus"}
26
+ ```
27
+
28
+ The same concept goes for empty hashes:
29
+
30
+ ```ruby
31
+ myhash = {'title' => 'Titus Andronicus', 'notes' => {'ideas'=>{}} }
32
+ puts myhash # => {"title"=>"Titus Andronicus", "notes"=>{}}
33
+ Declutter.process myhash
34
+ puts myhash # => {"title"=>"Titus Andronicus"}
35
+ ```
36
+
37
+ ## Object oriented approach
38
+
39
+ To refine what elements are deleted, instantiate `Declutter` to set what type of
40
+ elements are deleted.
41
+
42
+ ### nil
43
+
44
+ To delete nils, set `declutter.delete_nils` to true:
45
+
46
+ ```ruby
47
+ myhash = { 'episodes'=>['pilot', nil, 'transition'] }
48
+ puts myhash # => {"episodes"=>["pilot", nil, "transition"]}
49
+
50
+ declutter = Declutter.new()
51
+ declutter.delete_nils = true
52
+ declutter.process myhash
53
+
54
+ puts myhash # => {"episodes"=>["pilot", "transition"]}
55
+ ```
56
+
57
+ If deleting the nil values results in an empty array, then the array is deleted.
58
+
59
+ ```ruby
60
+ myhash = { 'episodes'=>[nil] }
61
+ puts myhash # => { 'episodes'=>[nil] }
62
+
63
+ declutter = Declutter.new()
64
+ declutter.delete_nils = true
65
+ declutter.process myhash
66
+
67
+ puts myhash # => {}
68
+ ```
69
+
70
+ ### redundancies
71
+
72
+ To delete redundant elements in arrays, set `declutter.delete_redundancies` to
73
+ true.
74
+
75
+ ```ruby
76
+ myhash = { 'episodes'=>['pilot', 'pilot'] }
77
+ puts myhash # => { 'episodes'=>['pilot', 'pilot'] }
78
+
79
+ declutter = Declutter.new()
80
+ declutter.delete_redundancies = true
81
+ declutter.process myhash
82
+
83
+ puts myhash # => { 'episodes'=>['pilot'] }
84
+ ```
85
+
86
+ ### false
87
+
88
+ To delete false values, set `declutter.delete_falses` to true:
89
+
90
+ ```ruby
91
+ myhash = { 'episodes'=>['pilot', false] }
92
+ puts myhash # => {"episodes"=>["pilot", false]}
93
+
94
+ declutter = Declutter.new()
95
+ declutter.delete_falses = true
96
+ declutter.process myhash
97
+
98
+ puts myhash # => {"episodes"=>["pilot"]}
99
+ ```
100
+
101
+ ### keep empty hashes and/or arrays
102
+
103
+ To keep empty hashes, set `declutter.delete_empty_hashes` to false:
104
+
105
+ ```ruby
106
+ myhash = { 'episodes'=>[], 'notes'=>{} }
107
+ puts myhash # => {"episodes"=>[], "notes"=>{}}
108
+
109
+ declutter = Declutter.new()
110
+ declutter.delete_empty_hashes = false
111
+ declutter.process myhash
112
+
113
+ puts myhash # => {"notes"=>{}}
114
+ ```
115
+
116
+ To keep empty arrays, set `declutter.delete_empty_arrays` to false:
117
+
118
+ ```ruby
119
+ myhash = { 'episodes'=>[], 'notes'=>{} }
120
+ puts myhash # => {"episodes"=>[], "notes"=>{}}
121
+
122
+ declutter = Declutter.new()
123
+ declutter.delete_empty_arrays = false
124
+ declutter.process myhash
125
+
126
+ puts myhash # => {"episodes"=>[]}
127
+ ```
128
+
129
+ ## History
130
+
131
+ | date | version | notes |
132
+ |--------------|---------|-----------------|
133
+ | May 23, 2023 | 1.0 | Initial upload. |
data/lib/declutter.rb ADDED
@@ -0,0 +1,132 @@
1
+ #===============================================================================
2
+ # Declutter
3
+ #
4
+ class Declutter
5
+ attr_accessor :delete_empty_hashes
6
+ attr_accessor :delete_empty_arrays
7
+ attr_accessor :delete_redundancies
8
+ attr_accessor :delete_nils
9
+ attr_accessor :delete_falses
10
+
11
+ #---------------------------------------------------------------------------
12
+ # initialize
13
+ #
14
+ def initialize
15
+ @delete_empty_hashes = true
16
+ @delete_empty_arrays = true
17
+ @delete_redundancies = false
18
+ @delete_nils = false
19
+ @delete_falses = false
20
+ end
21
+ #
22
+ # initialize
23
+ #---------------------------------------------------------------------------
24
+
25
+
26
+ #---------------------------------------------------------------------------
27
+ # process
28
+ # Process hash or array. Anything else is left as is.
29
+ #
30
+ def process(obj)
31
+ if obj.is_a?(Hash)
32
+ process_hash obj
33
+ elsif obj.is_a?(Array)
34
+ process_array obj
35
+ end
36
+ end
37
+ #
38
+ # process
39
+ #---------------------------------------------------------------------------
40
+
41
+
42
+ #---------------------------------------------------------------------------
43
+ # self.process
44
+ #
45
+ def self.process(obj)
46
+ self.new.process obj
47
+ end
48
+ #
49
+ # self.process
50
+ #---------------------------------------------------------------------------
51
+
52
+
53
+ #---------------------------------------------------------------------------
54
+ # process_hash
55
+ #
56
+ def process_hash(hsh)
57
+ hsh.keys.each do |k|
58
+ v = hsh[k]
59
+ process v
60
+
61
+ # delete or process element
62
+ if not keep_element?(v)
63
+ hsh.delete k
64
+ end
65
+ end
66
+ end
67
+ #
68
+ # process_hash
69
+ #---------------------------------------------------------------------------
70
+
71
+
72
+ #---------------------------------------------------------------------------
73
+ # process_array
74
+ #
75
+ def process_array(arr)
76
+ hold = arr.clone
77
+ arr.clear
78
+
79
+ # add back elements that are acceptable
80
+ hold.each do |child|
81
+ process child
82
+
83
+ if keep_element?(child)
84
+ arr.push child
85
+ end
86
+ end
87
+
88
+ # uniq array if necessary
89
+ if @delete_redundancies
90
+ arr.uniq!
91
+ end
92
+ end
93
+ #
94
+ # process_array
95
+ #---------------------------------------------------------------------------
96
+
97
+
98
+ #---------------------------------------------------------------------------
99
+ # keep_element
100
+ #
101
+ def keep_element?(v)
102
+ # hash
103
+ if v.is_a?(Hash)
104
+ if @delete_empty_hashes and v.empty?
105
+ return false
106
+ end
107
+
108
+ # array
109
+ elsif v.is_a?(Array)
110
+ if @delete_empty_arrays and v.empty?
111
+ return false
112
+ end
113
+
114
+ # nil
115
+ elsif v.nil?
116
+ return !@delete_nils
117
+
118
+ # false
119
+ elsif not v
120
+ return !@delete_falses
121
+ end
122
+
123
+ # if we get this far then we're keeping the element
124
+ return true
125
+ end
126
+ #
127
+ # keep_element
128
+ #---------------------------------------------------------------------------
129
+ end
130
+ #
131
+ # Declutter
132
+ #===============================================================================
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: declutter
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Mike O'Sullivan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Declutter your Ruby objects by removing empty hashes and arrays
14
+ email: mike@idocs.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/declutter.rb
21
+ homepage: https://github.com/mikosullivan/declutter
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.1.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Declutter
44
+ test_files: []