Class: Itools::StringSearcher
- Inherits:
-
Object
- Object
- Itools::StringSearcher
- Defined in:
- lib/itools/string_searcher.rb
Overview
Main class
Instance Attribute Summary collapse
-
#result ⇒ Object
Returns the value of attribute result.
-
#search_in ⇒ Object
Returns the value of attribute search_in.
-
#search_strs ⇒ Object
Returns the value of attribute search_strs.
Class Method Summary collapse
-
.search_result(temp_search_in, temp_search_strs) ⇒ Object
第一个参数为要搜索的文件或者文件夹名称 第二个参数为要搜索的字符串.
Instance Method Summary collapse
-
#check_exist ⇒ Object
第一步:检查是否存在.
-
#initialize(temp_SearchStrs, temp_SearchIn) ⇒ StringSearcher
constructor
A new instance of StringSearcher.
-
#search ⇒ Object
第二步开始搜索.
-
#search_in_file(temp_file) ⇒ Object
从文件查找.
-
#search_in_folder(unuse) ⇒ Object
从文件夹查找.
Constructor Details
#initialize(temp_SearchStrs, temp_SearchIn) ⇒ StringSearcher
Returns a new instance of StringSearcher.
31 32 33 34 35 |
# File 'lib/itools/string_searcher.rb', line 31 def initialize(temp_SearchStrs,temp_SearchIn) @search_strs = temp_SearchStrs @search_in = temp_SearchIn @result = [] end |
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
30 31 32 |
# File 'lib/itools/string_searcher.rb', line 30 def result @result end |
#search_in ⇒ Object
Returns the value of attribute search_in.
30 31 32 |
# File 'lib/itools/string_searcher.rb', line 30 def search_in @search_in end |
#search_strs ⇒ Object
Returns the value of attribute search_strs.
30 31 32 |
# File 'lib/itools/string_searcher.rb', line 30 def search_strs @search_strs end |
Class Method Details
.search_result(temp_search_in, temp_search_strs) ⇒ Object
第一个参数为要搜索的文件或者文件夹名称 第二个参数为要搜索的字符串
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/itools/string_searcher.rb', line 87 def self.search_result(temp_search_in,temp_search_strs) if temp_search_in.nil? puts "\033[31m传入的参数有误,第一个参数为要搜索的文件或者文件夹名称,第二个参数为要搜索的字符串(如要查找多个str使用英文,分割),两个参数中间用空格区分\033[0m" return end if temp_search_strs.nil? puts "\033[31m传入的参数有误,第一个参数为要搜索的文件或者文件夹名称,第二个参数为要搜索的字符串(如要查找多个str使用英文,分割),两个参数中间用空格区分\033[0m" return end # 传入的可能是字符串数组 searcher = StringSearcher.new(temp_search_strs.split(","),temp_search_in) searcher.search if searcher.result.size == 0 puts "\033[32m没有找到相关字段\033[0m" return end # 输出搜索的内容 Spreadsheet.client_encoding = 'utf-8' book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet sheet1.row(0)[0] = "文件名" sheet1.row(0)[1] = "包含字符串" sheet1.row(0)[2] = "文件所在目录" sheet1.row(0)[3] = "查找内容所在行" sheet1.row(0)[4] = "查找结果Str" searcher.result.each_with_index do |item,i| sheet1.row(i+1)[0] = File.basename(item.file_name) sheet1.row(i+1)[1] = item.key_str sheet1.row(i+1)[2] = File.dirname(item.file_name) sheet1.row(i+1)[3] = item.in_line sheet1.row(i+1)[4] = item.result_str if i < 10 puts "#{item.key_str} is in file:#{File.basename(item.file_name)} and Inline:#{item.in_line}" end end puts "\033[32m查找成功,内容已经保存到#{File.dirname(searcher.search_in)},请点击查看\033[0m" book.write "#{File.dirname(searcher.search_in)}/search_result.xls" end |
Instance Method Details
#check_exist ⇒ Object
第一步:检查是否存在
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/itools/string_searcher.rb', line 74 def check_exist if File.file?(@search_in) puts "\033[32m从文件中查找\033[0m" return true elsif File::directory?(@search_in) puts "\033[32m从文件夹中查找\033[0m" return true else return false end end |
#search ⇒ Object
第二步开始搜索
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/itools/string_searcher.rb', line 37 def search if check_exist handle_method = '' if File.file?(@search_in) #如果是文件 handle_method = "search_in_file" else handle_method = "search_in_folder" end self.send(handle_method,@search_in) else puts "\033[31m文件不存在,请检查输入是否正确\033[0m" return end end |
#search_in_file(temp_file) ⇒ Object
从文件查找
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/itools/string_searcher.rb', line 52 def search_in_file(temp_file) line_index = 1 File.read(temp_file).each_line do |line| haveIndex = StringHandle.containsStr(line,@search_strs) if haveIndex != -1 search_result = SearchResult.new(temp_file,line_index,line,@search_strs[haveIndex]) @result << search_result end line_index = line_index + 1 end end |
#search_in_folder(unuse) ⇒ Object
从文件夹查找
65 66 67 68 69 70 71 72 |
# File 'lib/itools/string_searcher.rb', line 65 def search_in_folder(unuse) puts @search_in_folder Find.find(@search_in) do |filename| if File.file?(filename) #如果是文件,则从文件中查找,忽略文件夹 search_in_file(filename) end end end |