Class: FindGrep::FindGrep
- Inherits:
-
Object
- Object
- FindGrep::FindGrep
- Defined in:
- lib/findgrep/findgrep.rb
Defined Under Namespace
Classes: Option
Constant Summary collapse
- DEFAULT_OPTION =
Option.new([], [], ".", -1, false, false, false, false, [], [], [], [], Platform.get_shell_kcode, false, nil, false, false, false)
Instance Attribute Summary collapse
-
#documents ⇒ Object
readonly
Returns the value of attribute documents.
Instance Method Summary collapse
- #and_expression(key, list) ⇒ Object
- #first_condition(match_datas, sub_matchs, or_matchs) ⇒ Object
-
#initialize(patterns, option) ⇒ FindGrep
constructor
A new instance of FindGrep.
- #open_database ⇒ Object
- #searchAndPrint(stdout) ⇒ Object
- #searchFromDB(stdout, dir) ⇒ Object
- #strs2regs(strs, ignore = false) ⇒ Object
Constructor Details
#initialize(patterns, option) ⇒ FindGrep
Returns a new instance of FindGrep.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/findgrep/findgrep.rb', line 57 def initialize(patterns, option) @patterns = patterns @option = option @patternRegexps = strs2regs(patterns, @option.ignoreCase) @subRegexps = strs2regs(option.keywordsNot, @option.ignoreCase) @orRegexps = strs2regs(option.keywordsOr, @option.ignoreCase) @filePatterns = (!@option.dbFile) ? strs2regs(option.filePatterns) : [] @ignoreFiles = strs2regs(option.ignoreFiles) @ignoreDirs = strs2regs(option.ignoreDirs) @result = Result.new(option.directory) open_database if (@option.dbFile) end |
Instance Attribute Details
#documents ⇒ Object (readonly)
Returns the value of attribute documents.
55 56 57 |
# File 'lib/findgrep/findgrep.rb', line 55 def documents @documents end |
Instance Method Details
#and_expression(key, list) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/findgrep/findgrep.rb', line 181 def and_expression(key, list) sub = nil list.each do |word| e = key =~ word if sub.nil? sub = e else sub &= e end end sub end |
#first_condition(match_datas, sub_matchs, or_matchs) ⇒ Object
400 401 402 403 404 405 406 |
# File 'lib/findgrep/findgrep.rb', line 400 def first_condition(match_datas, sub_matchs, or_matchs) unless match_datas.empty? match_datas[0] else or_matchs[0] end end |
#open_database ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/findgrep/findgrep.rb', line 70 def open_database() # データベース開く dbfile = Pathname(File.(@option.dbFile)) if dbfile.exist? Groonga::Database.open(dbfile.to_s) puts "open : #{dbfile} open." else raise "error : #{dbfile.to_s} not found!!" end # ドキュメントを取 @documents = Groonga::Context.default["documents"] end |
#searchAndPrint(stdout) ⇒ Object
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 128 129 |
# File 'lib/findgrep/findgrep.rb', line 97 def searchAndPrint(stdout) unless (@option.dbFile) searchFromDir(stdout, @option.directory, 0) else searchFromDB(stdout, @option.directory) end @result.time_stop if (!@option.isSilent && !@option.dispHtml) if (@option.debugMode) stdout.puts stdout.puts "--- search --------" print_fpaths stdout, @result.search_files stdout.puts "--- match --------" print_fpaths stdout, @result.match_files stdout.puts "--- ignore-file --------" print_fpaths stdout, @result.ignore_files stdout.puts "--- ignore-dir --------" print_fpaths stdout, @result.prune_dirs stdout.puts "--- unreadable --------" print_fpaths stdout, @result.unreadable_files end unless (@option.colorHighlight) stdout.puts else stdout.puts HighLine::REVERSE + "------------------------------------------------------------" + HighLine::CLEAR end @result.print(stdout) end end |
#searchFromDB(stdout, dir) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/findgrep/findgrep.rb', line 131 def searchFromDB(stdout, dir) # 全てのパターンを検索 table = @documents.select do |record| expression = nil # キーワード @patterns.each do |word| sub_expression = record.content =~ word if expression.nil? expression = sub_expression else expression &= sub_expression end end # パス @option.filePatterns.each do |word| sub_expression = record.path =~ word if expression.nil? expression = sub_expression else expression &= sub_expression end end # 拡張子(OR) se = suffix_expression(record) expression &= se if (se) # 検索式 expression end # タイムスタンプでソート records = table.sort([{:key => "_score", :order => "descending"}, {:key => "timestamp", :order => "descending"}]) # データベースにヒット stdout.puts "Found : #{records.size} records." unless (@option.dispHtml) # 検索にヒットしたファイルを実際に検索 records.each do |record| if (@option.groongaOnly) searchGroongaOnly(stdout, record) else searchFile(stdout, record.path, record.path) if FileTest.exist?(record.path) end end end |
#strs2regs(strs, ignore = false) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/findgrep/findgrep.rb', line 85 def strs2regs(strs, ignore = false) regs = [] strs.each do |v| option = 0 option |= Regexp::IGNORECASE if (ignore) regs << Regexp.new(v, option) end regs end |