Module: FS::Find

Included in:
FS
Defined in:
lib/fs/find.rb

Instance Method Summary collapse

Instance Method Details

#find(dir, options = {}, &block) ⇒ Object

Find::find


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fs/find.rb', line 5

def find(dir, options={}, &block)
  defaults = {
    :current => false,
    :absolute => false,
    :condition => nil
  }
  options = defaults.merge(options)

  result = []

  full_dir = File.expand_path(dir)
  ::Find.find(dir) do |full_path|
    next if !options[:current] && full_path == full_dir
    path = options[:absolute] ? full_path : FS.chop(full_path, full_dir)
    next if options[:condition] && !options[:condition][path]

    if block
      block[path]
    else
      result << path
    end
  end

  block ? nil : result
end

#find_dirs(dir, options = {}, &block) ⇒ Object


31
32
33
34
# File 'lib/fs/find.rb', line 31

def find_dirs(dir, options={}, &block)
  condition = ->(path){ FileTest.directory?(path) }
  find(dir, options.merge(:condition => condition), &block)
end

#find_files(dir, options = {}, &block) ⇒ Object


36
37
38
39
# File 'lib/fs/find.rb', line 36

def find_files(dir, options={}, &block)
  condition = ->(path){ !FileTest.directory?(path) }
  find(dir, options.merge(:condition => condition), &block)
end