Class: Yatte::FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/yatte/file_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_dir = ".") ⇒ FileFinder

Returns a new instance of FileFinder.



5
6
7
8
# File 'lib/yatte/file_finder.rb', line 5

def initialize(root_dir = ".")
  @root_dir = File.expand_path(root_dir)
  @files = nil
end

Instance Method Details

#filter(query) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/yatte/file_finder.rb', line 17

def filter(query)
  scan unless @files
  return @files if query.empty?

  pattern = query.downcase
  @files.select { |f| fuzzy_match?(f.downcase, pattern) }
    .sort_by { |f| f.length }
end

#scanObject



10
11
12
13
14
15
# File 'lib/yatte/file_finder.rb', line 10

def scan
  @files = Dir.glob("**/*", File::FNM_DOTMATCH, base: @root_dir)
    .reject { |f| f.start_with?(".git/") }
    .select { |f| File.file?(File.join(@root_dir, f)) }
    .sort
end