Module: ShatteredSupport::CoreExtensions::File::Search::ClassMethods

Defined in:
lib/shattered_support/core_ext/file/search.rb

Instance Method Summary collapse

Instance Method Details

#each_in_path(path) ⇒ Object

File.each_in_path will recursively look for all files, starting at the given path. It will yield with each result.

Raises:

  • (Errno::ENOENT)


14
15
16
17
18
19
20
21
22
23
# File 'lib/shattered_support/core_ext/file/search.rb', line 14

def each_in_path(path)
  raise Errno::ENOENT.new("Path not found: '#{path}'") unless ::File.directory? path
  
  ::Dir.each_in_path(path) do |directory|
    ::Dir.foreach( directory ) do |filename|
      resource = directory + "/#{filename}"
      yield(resource) if ::File.file? resource
    end
  end
end

#find(paths, file) ⇒ Object

Finds the first file matching ‘file’ in the given paths.

Raises:

  • (StandardError)


46
47
48
49
50
51
52
53
54
# File 'lib/shattered_support/core_ext/file/search.rb', line 46

def find(paths, file)
  paths = [paths] if paths.is_a? ::String
  paths.each do |path|
    each_in_path(path) do |resource|
      return resource if resource.ends_with?(file)
    end
  end
  raise StandardError.new("File not found #{file}")
end

#find_by_extension(paths, extension) ⇒ Object

See File#find_by_extensions



57
58
59
# File 'lib/shattered_support/core_ext/file/search.rb', line 57

def find_by_extension(paths, extension)
  find_by_extensions(paths, extension)
end

#find_by_extensions(paths, *extensions) ⇒ Object

This finds all files in paths (an array or string) matching the given extensions:

Usage:

-File.find_by_extensions(SHATTERED_ROOT,"ogg","mp3","wav")
-File.find_by_extension(["apps","media","plugins"], "rmaterial")


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shattered_support/core_ext/file/search.rb', line 31

def find_by_extensions(paths, *extensions)
  paths = [paths] if paths.is_a? ::String
  # get rid of "." in ".ogg"
  extensions.collect! { |ext| ext[0].chr == "." ? ext[1..-1] : ext }
  reg_exp = /\.(#{extensions.join("|")})$/
  files = []
  paths.each do |path|
    each_in_path(path) do |filename|
      files << filename if filename =~ reg_exp
    end
  end
  return files
end