blank_empty_nil_filters 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adbf79bbbb71e8dfed6243cf91759f9279b3970ad269e0f861c36172920b1e23
4
- data.tar.gz: 695e3e289ad0548df29683d72cc0b254ce8772b7450eb7dc4f09fd2f421dcf20
3
+ metadata.gz: 35a7e5e27066b542dc53c963f9f3769ea1a9da4667c69e5edad010158fe5717d
4
+ data.tar.gz: dad47bacd3d2f9adfc63d42ca24dcea0ede3bb87e97ffb18bd25a3d15c39720b
5
5
  SHA512:
6
- metadata.gz: bc6654261141905a9f63409dffb74caea814663102401992ee3eed3b63f0a528c5cd8fc07d2f4477786f800024a6b463087b27265b3c2b50318a8ccb1ded9242
7
- data.tar.gz: 4a14c426a1e5b5c65edf90558f8843d03bb6f293e8d55ab96ead2e301782fa3f37b4c27c2773ddb2b7a4bc8371ccaefdff8e906b6531abc49e42dd8609fe5979
6
+ metadata.gz: 141dae5987cfd3487d8c98977c1048036793ff8d2e2c49735ea630481aa23b5c6ee273325850f4351be126f11340f7045a1480606c264ecf2d57d0680781ed16
7
+ data.tar.gz: 102f2f9132840745ee8ac2ca5e40a2bfd9b2e460acb32e090cb876ae63434aa4eba2e668f1cfb2500b34dcb69818435b242d267e09cb0d9572e48558f8824a04
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- blank_empty_nil_filters (0.1.2)
4
+ blank_empty_nil_filters (0.1.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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__)
@@ -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
@@ -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
 
@@ -88,8 +92,9 @@ module BlankEmptyNilFilters
88
92
  include FilterMethods
89
93
 
90
94
  def filter_values(scanner, selector, condition, start, depth, level)
95
+ default = selector == :select
91
96
  map { |val| maybe_recurse(val, scanner, condition, start, depth, level) }
92
- .send(selector) { |val| maybe_apply(val, condition, start, depth, level) }
97
+ .send(selector) { |val| maybe_apply(val, condition, default, start, depth, level) }
93
98
  end
94
99
  end
95
100
 
@@ -169,8 +174,9 @@ module BlankEmptyNilFilters
169
174
  include FilterMethods
170
175
 
171
176
  def filter_hash_values(scanner, selector, condition, start, depth, level)
177
+ default = selector == :select
172
178
  dup.transform_values { |val| maybe_recurse(val, scanner, condition, start, depth, level) }
173
- .send(selector) { |_key, val| maybe_apply(val, condition, start, depth, level) }
179
+ .send(selector) { |_key, val| maybe_apply(val, condition, default, start, depth, level) }
174
180
  end
175
181
  end
176
182
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlankEmptyNilFilters
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Stebbens
@@ -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