Class: Zomgit::Concerns::Findable::Finder
- Inherits:
-
Object
- Object
- Zomgit::Concerns::Findable::Finder
- Defined in:
- lib/zomgit/concerns/findable.rb
Class Method Summary collapse
-
.fuzzy_find(files, query, options = {}) ⇒ Object
IMPROVE: Permit the query to be more complex (e.g.: a regex).
Class Method Details
.fuzzy_find(files, query, options = {}) ⇒ Object
IMPROVE: Permit the query to be more complex (e.g.: a regex)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/zomgit/concerns/findable.rb', line 6 def self.fuzzy_find(files, query, = {}) return Array.new if query.empty? eager_pattern = "\\b#{Regexp.escape(query)}" if query.include?("/") greedy_pattern = query.split("/").map { |p| p.split("").map { |c| Regexp.escape(c) }.join(")[^\/]*?(").prepend("[^\/]*?(") + ")[^\/]*?" }.join("\/") greedy_pattern << "\/" if query[-1] == "/" else greedy_pattern = query.split("").map { |c| Regexp.escape(c) }.join(").*?(").prepend(".*?(") + ").*?" end eager_results = [] greedy_results = [] exact_match_found = false files.each do |f| if f =~ /#{eager_pattern}/ eager_results << f exact_match_found = true next end if exact_match_found next unless !![:greedy] end greedy_results << f if f =~ /#{greedy_pattern}/ end if eager_results.empty? || !![:greedy] eager_results + greedy_results else eager_results end end |