blank_empty_nil_filters 0.1.2 → 1.0.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
  SHA256:
3
- metadata.gz: adbf79bbbb71e8dfed6243cf91759f9279b3970ad269e0f861c36172920b1e23
4
- data.tar.gz: 695e3e289ad0548df29683d72cc0b254ce8772b7450eb7dc4f09fd2f421dcf20
3
+ metadata.gz: 050ecf453b9f1337450920915eff6d0c33f228e12a41df2ab00d3d7ab14f9904
4
+ data.tar.gz: 691b76327aa76b7e61a1e521df5b4807a3ec117171480a9b375c142e7d051c7d
5
5
  SHA512:
6
- metadata.gz: bc6654261141905a9f63409dffb74caea814663102401992ee3eed3b63f0a528c5cd8fc07d2f4477786f800024a6b463087b27265b3c2b50318a8ccb1ded9242
7
- data.tar.gz: 4a14c426a1e5b5c65edf90558f8843d03bb6f293e8d55ab96ead2e301782fa3f37b4c27c2773ddb2b7a4bc8371ccaefdff8e906b6531abc49e42dd8609fe5979
6
+ metadata.gz: 835e07d0ba6be825c8ea574427fbd0cd995e133bb8473345af404d0495ea44386274d2161b204bf1f6ef842c7312e0adc81692574f227cb58897c3599d30fc9b
7
+ data.tar.gz: 27e8e1c1c8fa33bc12b9aafac71179b69ef35e7b019e908ebca4b4eb4700b0da8cf1f4689229ca753c3b547f551136afd3ace1f1828287fad5fdd13d4f7e39d7
data/Gemfile.lock CHANGED
@@ -1,35 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- blank_empty_nil_filters (0.1.2)
4
+ blank_empty_nil_filters (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activesupport (5.2.3)
10
- concurrent-ruby (~> 1.0, >= 1.0.2)
11
- i18n (>= 0.7, < 2)
12
- minitest (~> 5.1)
13
- tzinfo (~> 1.1)
14
- ast (2.4.0)
15
- builder (3.2.3)
16
- byebug (11.0.1)
17
- coderay (1.1.2)
18
- concurrent-ruby (1.1.5)
19
- diff-lcs (1.3)
20
- docile (1.3.1)
21
- ffi (1.10.0)
22
- formatador (0.2.5)
23
- fuubar (2.3.2)
9
+ ast (2.4.2)
10
+ builder (3.3.0)
11
+ byebug (11.1.3)
12
+ coderay (1.1.3)
13
+ diff-lcs (1.5.1)
14
+ docile (1.4.0)
15
+ ffi (1.17.0)
16
+ formatador (1.1.0)
17
+ fuubar (2.5.1)
24
18
  rspec-core (~> 3.0)
25
19
  ruby-progressbar (~> 1.4)
26
- guard (2.15.0)
20
+ guard (2.18.1)
27
21
  formatador (>= 0.2.4)
28
22
  listen (>= 2.7, < 4.0)
29
23
  lumberjack (>= 1.0.12, < 2.0)
30
24
  nenv (~> 0.1)
31
25
  notiffany (~> 0.0)
32
- pry (>= 0.9.12)
26
+ pry (>= 0.13.0)
33
27
  shellany (~> 0.0)
34
28
  thor (>= 0.18.1)
35
29
  guard-compat (1.2.1)
@@ -40,80 +34,82 @@ GEM
40
34
  guard-yard (2.2.1)
41
35
  guard (>= 1.1.0)
42
36
  yard (>= 0.7.0)
43
- i18n (1.6.0)
44
- concurrent-ruby (~> 1.0)
45
- jaro_winkler (1.5.2)
46
- json (2.2.0)
47
- listen (3.1.5)
48
- rb-fsevent (~> 0.9, >= 0.9.4)
49
- rb-inotify (~> 0.9, >= 0.9.7)
50
- ruby_dep (~> 1.2)
51
- lumberjack (1.0.13)
52
- method_source (0.9.2)
53
- minitest (5.11.3)
37
+ json (2.7.2)
38
+ language_server-protocol (3.17.0.3)
39
+ listen (3.9.0)
40
+ rb-fsevent (~> 0.10, >= 0.10.3)
41
+ rb-inotify (~> 0.9, >= 0.9.10)
42
+ lumberjack (1.2.10)
43
+ method_source (1.1.0)
54
44
  nenv (0.3.0)
55
- notiffany (0.1.1)
45
+ notiffany (0.1.3)
56
46
  nenv (~> 0.1)
57
47
  shellany (~> 0.0)
58
- parallel (1.17.0)
59
- parser (2.6.2.1)
60
- ast (~> 2.4.0)
61
- pry (0.12.2)
62
- coderay (~> 1.1.0)
63
- method_source (~> 0.9.0)
64
- pry-byebug (3.7.0)
48
+ parallel (1.25.1)
49
+ parser (3.3.3.0)
50
+ ast (~> 2.4.1)
51
+ racc
52
+ pry (0.14.2)
53
+ coderay (~> 1.1)
54
+ method_source (~> 1.0)
55
+ pry-byebug (3.10.1)
65
56
  byebug (~> 11.0)
66
- pry (~> 0.10)
67
- psych (3.1.0)
68
- rainbow (3.0.0)
69
- rake (12.3.2)
70
- rb-fsevent (0.10.3)
71
- rb-inotify (0.10.0)
57
+ pry (>= 0.13, < 0.15)
58
+ racc (1.8.0)
59
+ rainbow (3.1.1)
60
+ rake (13.2.1)
61
+ rb-fsevent (0.11.2)
62
+ rb-inotify (0.11.1)
72
63
  ffi (~> 1.0)
73
- redcarpet (3.4.0)
74
- rspec (3.8.0)
75
- rspec-core (~> 3.8.0)
76
- rspec-expectations (~> 3.8.0)
77
- rspec-mocks (~> 3.8.0)
78
- rspec-core (3.8.0)
79
- rspec-support (~> 3.8.0)
80
- rspec-expectations (3.8.2)
64
+ redcarpet (3.6.0)
65
+ regexp_parser (2.9.2)
66
+ rexml (3.3.1)
67
+ strscan
68
+ rspec (3.13.0)
69
+ rspec-core (~> 3.13.0)
70
+ rspec-expectations (~> 3.13.0)
71
+ rspec-mocks (~> 3.13.0)
72
+ rspec-core (3.13.0)
73
+ rspec-support (~> 3.13.0)
74
+ rspec-expectations (3.13.1)
81
75
  diff-lcs (>= 1.2.0, < 2.0)
82
- rspec-support (~> 3.8.0)
83
- rspec-mocks (3.8.0)
76
+ rspec-support (~> 3.13.0)
77
+ rspec-mocks (3.13.1)
84
78
  diff-lcs (>= 1.2.0, < 2.0)
85
- rspec-support (~> 3.8.0)
86
- rspec-support (3.8.0)
79
+ rspec-support (~> 3.13.0)
80
+ rspec-support (3.13.1)
87
81
  rspec_junit (4.0.4)
88
82
  builder (>= 3.2.2)
89
83
  rspec (>= 3.3.0)
90
- rspec_junit_formatter (0.4.1)
84
+ rspec_junit_formatter (0.6.0)
91
85
  rspec-core (>= 2, < 4, != 2.12.0)
92
- rubocop (0.67.2)
93
- jaro_winkler (~> 1.5.1)
86
+ rubocop (1.64.1)
87
+ json (~> 2.3)
88
+ language_server-protocol (>= 3.17.0)
94
89
  parallel (~> 1.10)
95
- parser (>= 2.5, != 2.5.1.1)
96
- psych (>= 3.1.0)
90
+ parser (>= 3.3.0.2)
97
91
  rainbow (>= 2.2.2, < 4.0)
92
+ regexp_parser (>= 1.8, < 3.0)
93
+ rexml (>= 3.2.5, < 4.0)
94
+ rubocop-ast (>= 1.31.1, < 2.0)
98
95
  ruby-progressbar (~> 1.7)
99
- unicode-display_width (>= 1.4.0, < 1.6)
100
- ruby-progressbar (1.10.0)
101
- ruby_dep (1.5.0)
96
+ unicode-display_width (>= 2.4.0, < 3.0)
97
+ rubocop-ast (1.31.3)
98
+ parser (>= 3.3.1.0)
99
+ ruby-progressbar (1.13.0)
102
100
  shellany (0.0.1)
103
- simplecov (0.16.1)
101
+ simplecov (0.22.0)
104
102
  docile (~> 1.1)
105
- json (>= 1.8, < 3)
106
- simplecov-html (~> 0.10.0)
107
- simplecov-html (0.10.2)
108
- spring (2.0.2)
109
- activesupport (>= 4.2)
103
+ simplecov-html (~> 0.11)
104
+ simplecov_json_formatter (~> 0.1)
105
+ simplecov-html (0.12.3)
106
+ simplecov_json_formatter (0.1.4)
107
+ spring (4.2.1)
108
+ strscan (3.1.0)
110
109
  terminal-notifier-guard (1.7.0)
111
- thor (0.20.3)
112
- thread_safe (0.3.6)
113
- tzinfo (1.2.5)
114
- thread_safe (~> 0.1)
115
- unicode-display_width (1.5.0)
116
- yard (0.9.19)
110
+ thor (1.3.1)
111
+ unicode-display_width (2.5.0)
112
+ yard (0.9.36)
117
113
 
118
114
  PLATFORMS
119
115
  ruby
data/README.md CHANGED
@@ -6,10 +6,10 @@ Build status: [![CircleCI](https://circleci.com/gh/aks/blank_empty_nil_filters/t
6
6
 
7
7
  This module creates new methods to be prepended to the Array, Hash,
8
8
  String, and Object classes to implement recursive filters for _blank_,
9
- _empty_, and nil values.
9
+ _empty_, and _nil_ values.
10
10
 
11
11
  The methods can select or reject values which are blank _(whitespace)_,
12
- empty _(zero length)_, or nil, and do so recursively.
12
+ empty _(zero length)_, or _nil_, and do so recursively.
13
13
 
14
14
  The default behavior is to filter recursively, without limit.
15
15
  However, optional arguments may be applied to any filter method to
@@ -20,7 +20,7 @@ level hashes can be completely removed if they are empty.
20
20
 
21
21
  Note: `ActiveSupport` provides _some_ of these methods, but in general
22
22
  is a *much* larger body of code. This module are only those methods
23
- related to "blank", "empty" or nil. Also, `ActiveSupport` uses
23
+ related to "blank", "empty" or `nil`. Also, `ActiveSupport` uses
24
24
  `Regexp` `match` to determine blankness, while this code uses a
25
25
  non-destructive `strip`, which is both faster and less sensitive to
26
26
  non-UTF8 string error conditions.
@@ -87,9 +87,9 @@ aoh.reject_blank_values # aoh.no_blank_values
87
87
  aoh.reject_nil_values # aoh.no_nil_values
88
88
  ```
89
89
 
90
- Any of the above methods can be provided optional arguments of _start_ and _depth_
91
- to indicate levels at which filter _(selecting or rejecting)_ should start and
92
- stop.
90
+ Any of the above methods can be provided optional arguments of _start_ and
91
+ _depth_ to indicate levels at which filter _(selecting or rejecting)_ should
92
+ start and stop.
93
93
 
94
94
  ```ruby
95
95
  aoh.no_empty_values(1) # no empty items at level 1 or higher; leave level 0 alone
@@ -107,16 +107,27 @@ aoh.reject_values(:condition_method) # reject items matching :condition_method
107
107
  aoh.select_values(:condition_method) # select items matching :condition_method
108
108
  ```
109
109
 
110
- There are two `Object` instance methods of `no_empty_value` and `no_blank_value` that can be used as `String` or
111
- other class instance filters to return the instance or a nil. This enables the use of `||` conditions to provide
110
+ There are two `Object` instance methods of `no_empty_value` and `no_blank_value`
111
+ that can be used as `String` or other class instance filters to return the
112
+ instance or a nil. This enables the use of `||` conditions to provide
112
113
  alternative values:
113
114
 
115
+ These methods all return the non-empty value, or a nil.
114
116
  ```ruby
115
117
  obj.no_empty_value || 'default'
118
+ obj.non_empty || 'default'
119
+ obj.no_empty || 'default'
120
+ ````
121
+
122
+ These methods all return the non-blank value, or a nil.
123
+ ```ruby
116
124
  obj.no_blank_value || 'default'
125
+ obj.non_blank || 'default'
126
+ obj.no_blank || 'default'
117
127
  ```
118
128
 
119
- These two methods are like `ActiveSupport`'s `presence` method but with finer-grain control.
129
+ The `non_empty` are like `ActiveSupport`'s `presence` method but with
130
+ finer-grain control.
120
131
 
121
132
  ### Condition Methods
122
133
 
@@ -132,8 +143,8 @@ obj.non_blank? # true if object is not nil and has non-zero size, and is not
132
143
  obj.non_nil? # !object.nil?
133
144
  ```
134
145
 
135
- For _hash_ or _array_ objects, `is_empty?` and `is_blank?` work recursively on the object
136
- elements, and the result is true only if true at every level.
146
+ For _hash_ or _array_ objects, `is_empty?` and `is_blank?` work recursively on
147
+ the object elements, and the result is true only if true at every level.
137
148
 
138
149
  ### Hash key and value filters
139
150
  There are also convenience methods to select the keys from filtered hash items:
@@ -152,8 +163,9 @@ hash.non_blank_value_keys => hash.no_blank_values.keys
152
163
 
153
164
  ### Hash Filters
154
165
 
155
- The most common usage of `no_empty_values` is with hashes of parameters, which are formed from
156
- many variables, and then the empty or blank ones can be easily filtered out of the hash:
166
+ The most common usage of `no_empty_values` is with hashes of parameters, which
167
+ are formed from many variables, and then the empty or blank ones can be easily
168
+ filtered out of the hash:
157
169
 
158
170
  ```ruby
159
171
  { 'Input' => {
@@ -168,6 +180,9 @@ many variables, and then the empty or blank ones can be easily filtered out of t
168
180
  }.no_empty_values
169
181
  ```
170
182
 
183
+ Ruby's `Hash` `compact` instance method can remove key-value pairs with nil
184
+ values, but does not filter for blank or empty values, or recurse.
185
+
171
186
  With `params` containing:
172
187
 
173
188
  ```ruby
@@ -197,9 +212,9 @@ params.no_blank_values => { type: :pdf }
197
212
 
198
213
  #### Finding keys with empty values
199
214
 
200
- Sometimes, it's not the key-value pairs that are non-empty that are desired; instead,
201
- the programmer often wants the keys with empty, blank, or nil values, _(e.g., so they can
202
- be reported on)_.
215
+ Sometimes, it's not the key-value pairs that are non-empty that are desired;
216
+ instead, the programmer often wants the keys with empty, blank, or nil values,
217
+ _(e.g., so they can be reported on)_.
203
218
 
204
219
  ```ruby
205
220
  params = { file: ' ', type: :pdf, pattern: nil, dest: ''}
@@ -218,7 +233,9 @@ To filter out empty (zero-length) values from an array:
218
233
  [:1 ['' :b] nil :2 ['' '']].no_empty_values => [:1 [:b] :2]
219
234
  ```
220
235
 
221
- Note that the filter is recursive, and if a sub-array is entirely empty, it is filtered out also _(because it also has zero length, after its items have been filtered)_.
236
+ Note that the filter is recursive, and if a sub-array is entirely empty, it is
237
+ filtered out also _(because it also has zero length, after its items have been
238
+ filtered)_.
222
239
 
223
240
  The `nil` is considered an "empty" value.
224
241
 
@@ -230,7 +247,9 @@ To filter out _blank_ values from an array:
230
247
 
231
248
  ### String Filters
232
249
 
233
- The `no_empty_value` and `no_blank_value` methods on the `Object` class can be applied to any object that supports the `is_blank?` and `is_empty?` condition methods, including the `String` class.
250
+ The `no_empty_value` and `no_blank_value` methods on the `Object` class can be
251
+ applied to any object that supports the `is_blank?` and `is_empty?` condition
252
+ methods, including the `String` class.
234
253
 
235
254
  ```ruby
236
255
  ' no fun '.no_empty_value || 'other' ==> ' no fun '
@@ -245,10 +264,12 @@ The `no_empty_value` and `no_blank_value` methods on the `Object` class can be a
245
264
 
246
265
  ## Testing
247
266
 
248
- There is an `rspec`-style test in the `spec` directory, the perusal of which will also provide some examples of usage.
267
+ There is an `rspec`-style test in the `spec` directory, the perusal of which
268
+ will also provide some examples of usage.
249
269
 
250
- The rspec-style tests are normally run under the `fuubar` formatter gem which shows an incremental summary on a single line. The
251
- tests can be run with `rake` or with `rspec`:
270
+ The rspec-style tests are normally run under the `fuubar` formatter gem which
271
+ shows an incremental summary on a single line. The tests can be run with `rake`
272
+ or with `rspec`:
252
273
 
253
274
  bundle exec rake spec
254
275
 
@@ -256,7 +277,8 @@ or
256
277
 
257
278
  bundle exec rspec
258
279
 
259
- However, if you wish to see all of the usual "documentation" style output, use the `-f doc` option on the `rspec` invocation:
280
+ However, if you wish to see all of the usual "documentation" style output, use
281
+ the `-f doc` option on the `rspec` invocation:
260
282
 
261
283
  bundle exec rspec -f doc
262
284
 
@@ -264,7 +286,11 @@ However, if you wish to see all of the usual "documentation" style output, use t
264
286
 
265
287
  After checking out the repo, run `rake spec` to run the tests.
266
288
 
267
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
289
+ To install this gem onto your local machine, run `bundle exec rake install`. To
290
+ release a new version, update the version number in `version.rb`, and then
291
+ run `bundle exec rake release`, which will create a git tag for the version,
292
+ push git commits and tags, and push the `.gem` file
293
+ to [rubygems.org](https://rubygems.org).
268
294
 
269
295
 
270
296
  ## Contributing
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blank_empty_nil_filters"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,152 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $PROG = File.basename($0)
4
+ $LOAD_PATH << File.expand_path('lib')
5
+
6
+ require 'blank_empty_nil_filters'
7
+ require 'awesome_print'
8
+ require 'fileutils'
9
+ require 'optparse'
10
+ require 'pry-byebug'
11
+
12
+ $opts = ARGV.getopts('hinv')
13
+ $verobse = $opts['v']
14
+ $norun = $opts['n']
15
+ $interactive = $opts['i']
16
+ $help = $opts['h'] || ARGV.detect { |arg| %w[--help help].include?(arg) }
17
+
18
+ def error(msg)
19
+ warn(msg)
20
+ exit -1
21
+ end
22
+
23
+ data = [
24
+ :lev1, ' ', '', nil, :ok1,
25
+ [:lev2a, ' ', '', nil, :ok2],
26
+ ' ', '', :ok1b,
27
+ [:lev2b, ' ', '', nil, :ok2b,
28
+ [:lev3, ' ', :ok3, '', nil],
29
+ nil],
30
+ :ok1c
31
+ ]
32
+
33
+ def ignore_stderr
34
+ old_stderr = $stderr
35
+ $stderr = File.open('/dev/null', 'wt')
36
+ yield
37
+ $stderr.close
38
+ ensure
39
+ $stderr = old_stderr
40
+ end
41
+
42
+ def capture_stdout(filepath)
43
+ old_stdout = $stdout
44
+ File.open(filepath, 'wt') do |file|
45
+ $stdout = file
46
+ yield
47
+ end
48
+ ensure
49
+ $stdout = old_stdout
50
+ end
51
+
52
+ def diff_files(file1, file2)
53
+ diff_cmd = "diff -y #{file1} #{file2}"
54
+ puts "$ " + diff_cmd
55
+ puts `#{diff_cmd}`
56
+ puts ''
57
+ end
58
+
59
+ def is_okay?(msg)
60
+ msg = msg.dup + "? [yN] "
61
+ loop do
62
+ $stderr.print msg
63
+ ans = $stdin.gets&.chomp
64
+ case ans
65
+ when NilClass then error "Quitting!"
66
+ when /yes|ye|y/i then return true
67
+ when /no|n/i, '' then return false
68
+ end
69
+ rescue Interrupt
70
+ error "Interrupted!"
71
+ end
72
+ end
73
+
74
+ ref_dir = 'test/ref'
75
+ out_dir = 'test/out'
76
+ diff_dir = 'test/diff'
77
+
78
+ ['test', ref_dir, out_dir, diff_dir].each do |dir|
79
+ unless Dir.exist?(dir)
80
+ $stderr.print "Creating #{dir}: "
81
+ ok = Dir.mkdir(dir)
82
+ warn(ok ? 'ok' : 'uh-oh!')
83
+ end
84
+ end
85
+
86
+ orig_output_filepath = 'test/out/test-original-output.txt'
87
+ capture_stdout(orig_output_filepath) do
88
+ puts 'original:'
89
+ ignore_stderr { puts data.ai(plain: true) }
90
+ end
91
+
92
+ diffs = 0
93
+ %i[no_empty_values no_blank_values no_nil_values].each do |filter|
94
+ [
95
+ [nil, 0], [nil, 1], [nil, 2], [nil, 3],
96
+ [0, nil], [0, 1], [0, 2], [0, 3],
97
+ [1, nil], [1, 1], [1, 2], [1, 3],
98
+ [2, nil], [2, 1], [2, 2], [2, 3]
99
+ ].each do |start, depth|
100
+ test_name = "#{filter}(#{start || 'nil'},#{depth || 'nil'})"
101
+ $stderr.printf "Testing #{test_name} .. "
102
+ filename = "#{$PROG}_#{test_name.gsub(/[(,)]/, '_')}output.txt"
103
+ out_file_path = File.join(out_dir, filename)
104
+ ref_file_path = File.join(ref_dir, filename)
105
+ diff_file_path = File.join(diff_dir, filename)
106
+ capture_stdout(out_file_path) do
107
+ puts "#{test_name}:"
108
+ new_data = data.send(filter.to_sym, start, depth)
109
+ ignore_stderr { puts new_data.ai(plain: true) }
110
+ end
111
+ FileUtils.touch(ref_file_path) unless File.exist?(ref_file_path)
112
+ if FileUtils.compare_file(ref_file_path, out_file_path)
113
+ warn "ok"
114
+ FileUtils.rm out_file_path
115
+ else
116
+ diffs += 1
117
+ warn "different! see #{diff_file_path}"
118
+ capture_stdout(diff_file_path) do
119
+ if File.size?(ref_file_path)
120
+ puts 'Diffs between reference output and test filtered data:'
121
+ diff_files(ref_file_path, out_file_path)
122
+ end
123
+ puts 'Diffs between original data and test filtered data:'
124
+ diff_files(orig_output_filepath, out_file_path)
125
+ if File.size?(ref_file_path)
126
+ puts 'Diffs between original data and reference filtered data:'
127
+ diff_files(orig_output_filepath, ref_file_path)
128
+ end
129
+ end
130
+ if $interactive
131
+ puts IO.read(diff_file_path)
132
+ if is_okay?('Are the changes okay? ')
133
+ FileUtils.rm ref_file_path if File.exist?(ref_file_path)
134
+ FileUtils.mv out_file_path, ref_file_path
135
+ FileUtils.rm diff_file_path
136
+ warn "Ref copy updated"
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ if diffs > 0
143
+ puts "There were #{diffs} differences!"
144
+ puts "The output files are stored in test/out"
145
+ puts "The reference output files are stored in test/ref"
146
+ puts "and the difference listings are in test/diff"
147
+ puts "Please review the difference listings; if the changes are acceptable,"
148
+ puts "then please move the corresponding output file from test/out into"
149
+ puts "test/ref. If the changes are not acceptable, then please fix the bug!"
150
+ end
151
+ warn 'done'
152
+ exit
@@ -1,5 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlankEmptyNilFilters
4
- VERSION = '0.1.2'
4
+ RELEASES = [
5
+ ['1.0.0', '2025-01-21'],
6
+ ['0.2.0', '2020-08-28'],
7
+ ['0.1.1', '2019-05-23']
8
+ ].freeze
9
+
10
+ VERSION = RELEASES[0][0]
5
11
  end
@@ -6,7 +6,7 @@ module BlankEmptyNilFilters
6
6
  # These filter methods are used on both the Array and Hash extensions
7
7
  module FilterMethods
8
8
  def maybe_recurse(val, scanner, condition, start, depth, level)
9
- if level >= start && (depth.nil? || level < depth)
9
+ if depth.nil? || level <= depth
10
10
  if val.respond_to?(scanner)
11
11
  val.send(scanner, condition, start, depth, level+1) # recurse
12
12
  else
@@ -17,8 +17,12 @@ module BlankEmptyNilFilters
17
17
  end
18
18
  end
19
19
 
20
- def maybe_apply(val, condition, start, depth, level)
21
- val.send(condition) if level >= start && (depth.nil? || level < depth)
20
+ def maybe_apply(val, condition, default, start, depth, level)
21
+ if level >= (start || 0) && (depth.nil? || level <= depth)
22
+ val.send(condition)
23
+ else
24
+ default
25
+ end
22
26
  end
23
27
  end
24
28
 
@@ -33,6 +37,7 @@ module BlankEmptyNilFilters
33
37
  end
34
38
  alias reject_blank_values no_blank_values
35
39
 
40
+ # more capable than just ARRAY.compact
36
41
  def no_nil_values(start = 0, depth = nil, level = 0)
37
42
  reject_values(:nil?, start, depth, level)
38
43
  end
@@ -88,8 +93,9 @@ module BlankEmptyNilFilters
88
93
  include FilterMethods
89
94
 
90
95
  def filter_values(scanner, selector, condition, start, depth, level)
96
+ default = selector == :select
91
97
  map { |val| maybe_recurse(val, scanner, condition, start, depth, level) }
92
- .send(selector) { |val| maybe_apply(val, condition, start, depth, level) }
98
+ .send(selector) { |val| maybe_apply(val, condition, default, start, depth, level) }
93
99
  end
94
100
  end
95
101
 
@@ -114,6 +120,7 @@ module BlankEmptyNilFilters
114
120
  end
115
121
  alias select_blank_values only_blank_values
116
122
 
123
+ # more capable than just HASH.compact
117
124
  def no_nil_values(start = 0, depth = nil, level = 0)
118
125
  reject_values(:nil?, start, depth, level)
119
126
  end
@@ -169,8 +176,9 @@ module BlankEmptyNilFilters
169
176
  include FilterMethods
170
177
 
171
178
  def filter_hash_values(scanner, selector, condition, start, depth, level)
179
+ default = selector == :select
172
180
  dup.transform_values { |val| maybe_recurse(val, scanner, condition, start, depth, level) }
173
- .send(selector) { |_key, val| maybe_apply(val, condition, start, depth, level) }
181
+ .send(selector) { |_key, val| maybe_apply(val, condition, default, start, depth, level) }
174
182
  end
175
183
  end
176
184
 
@@ -199,13 +207,15 @@ module BlankEmptyNilFilters
199
207
  end
200
208
 
201
209
  module ObjectExtensions
202
- def no_blank_value
210
+ def non_blank
203
211
  is_blank? ? nil : self
204
212
  end
213
+ alias no_blank_value non_blank
205
214
 
206
- def no_empty_value
215
+ def non_empty
207
216
  is_empty? ? nil : self
208
217
  end
218
+ alias no_empty_value non_empty
209
219
 
210
220
  def is_empty?
211
221
  if nil?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blank_empty_nil_filters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Stebbens
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-28 00:00:00.000000000 Z
11
+ date: 2025-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -249,6 +249,9 @@ files:
249
249
  - LICENSE.md
250
250
  - README.md
251
251
  - Rakefile
252
+ - bin/console
253
+ - bin/setup
254
+ - bin/test-filter-args.rb
252
255
  - blank_empty_nil_filters.gemspec
253
256
  - lib/blank_empty_nil_filters.rb
254
257
  - lib/blank_empty_nil_filters/version.rb
@@ -256,7 +259,7 @@ homepage: https://github.com/aks/blank_empty_nil_filters
256
259
  licenses:
257
260
  - MIT
258
261
  metadata: {}
259
- post_install_message:
262
+ post_install_message:
260
263
  rdoc_options: []
261
264
  require_paths:
262
265
  - lib
@@ -271,8 +274,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
271
274
  - !ruby/object:Gem::Version
272
275
  version: '0'
273
276
  requirements: []
274
- rubygems_version: 3.1.2
275
- signing_key:
277
+ rubygems_version: 3.4.19
278
+ signing_key:
276
279
  specification_version: 4
277
280
  summary: '["Extensions for filtering empty, blank, and nil values from Hashes and
278
281
  Arrays", "Extentions for convenient filtering of blank, empty, and nil values from\nHash