Class: Peaches::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/peaches/finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Finder

Init

Parameters

root

The root directory to cache



10
11
12
# File 'lib/peaches/finder.rb', line 10

def initialize(root)
  @root = root
end

Instance Attribute Details

#cacheObject

Get and/or fetch the file list cache



35
36
37
# File 'lib/peaches/finder.rb', line 35

def cache
  @cache
end

Instance Method Details

#build_cacheObject

Builds the cache excluding files that start with .



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/peaches/finder.rb', line 40

def build_cache
  all_files = []

  Find.find(@root) do |path|
    if FileTest.directory?(path)
      if File.basename(path)[0] == ?.
        Find.prune
      else
        next
      end
    else
      path.slice!("#{@root}/")
      all_files << path
    end
  end

  return all_files
end

#find(string) ⇒ Object

FuzzyFind files from the cache

Paarameters

string

The string you want to use to find matching files, if left black it will bring up all the files



20
21
22
23
24
25
26
27
# File 'lib/peaches/finder.rb', line 20

def find(string)
  string = string.split(//).join(".*?")
  pattern = "/#{string}/i"

  results = self.cache.grep /#{string}/i

  return results
end

#refresh_cacheObject

forces a refresh of the file list cache



30
31
32
# File 'lib/peaches/finder.rb', line 30

def refresh_cache
  @cache = build_cache
end