Class: FileFilter
- Inherits:
-
Object
- Object
- FileFilter
- Defined in:
- lib/file_filter.rb
Class Method Summary collapse
- .check(dir_pattern, size_threshold: 1000, cust_judge: nil) ⇒ Object
- .check_save_result(dir_pattern, save_file_name: 'filtered_file.txt', size_threshold: 1000, cust_judge: nil) ⇒ Object
- .delete(dir_pattern, size_threshold: 1000, cust_judge: nil) ⇒ Object
Instance Method Summary collapse
- #default_judge(f) ⇒ Object
- #filter_file(f) ⇒ Object
-
#initialize(dir_pattern, size_threshold: 1000, cust_judge: nil, process_block: nil) ⇒ FileFilter
constructor
4033 920.
- #start ⇒ Object
Constructor Details
#initialize(dir_pattern, size_threshold: 1000, cust_judge: nil, process_block: nil) ⇒ FileFilter
4033 920
5 6 7 8 9 10 11 12 |
# File 'lib/file_filter.rb', line 5 def initialize(dir_pattern, size_threshold: 1000, cust_judge: nil, process_block: nil) @dir_pattern = dir_pattern @size_threshold = size_threshold @cust_judge = cust_judge ? cust_judge : method(:default_judge) @total = 0 @process_block = process_block end |
Class Method Details
.check(dir_pattern, size_threshold: 1000, cust_judge: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/file_filter.rb', line 45 def self.check(dir_pattern, size_threshold: 1000, cust_judge: nil) FileFilter.new( dir_pattern, size_threshold: size_threshold, cust_judge: cust_judge, process_block: proc do |f| puts "filterd file: #{f}" end ).start end |
.check_save_result(dir_pattern, save_file_name: 'filtered_file.txt', size_threshold: 1000, cust_judge: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/file_filter.rb', line 57 def self.check_save_result(dir_pattern, save_file_name: 'filtered_file.txt', size_threshold: 1000, cust_judge: nil) result_file = File.open(save_file_name, 'wt') FileFilter.new( dir_pattern, size_threshold: size_threshold, cust_judge: cust_judge, process_block: proc do |f| puts "filterd file: #{f}" result_file << f << "\n" end ).start result_file.close end |
.delete(dir_pattern, size_threshold: 1000, cust_judge: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/file_filter.rb', line 32 def self.delete(dir_pattern, size_threshold: 1000, cust_judge: nil) FileFilter.new( dir_pattern, size_threshold: size_threshold, cust_judge: cust_judge, process_block: proc do |f| puts "deleted file: #{f}" File.delete(f) end ).start end |
Instance Method Details
#default_judge(f) ⇒ Object
14 15 16 |
# File 'lib/file_filter.rb', line 14 def default_judge(f) File.size(f) <= @size_threshold end |
#filter_file(f) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/file_filter.rb', line 18 def filter_file(f) if @cust_judge.call(f) @total += 1 @process_block.call(f) end end |
#start ⇒ Object
25 26 27 28 29 30 |
# File 'lib/file_filter.rb', line 25 def start Dir.glob(@dir_pattern) do |f| filter_file(f) end puts "total:#{@total}" end |