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.



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

def each_in_path(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)


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

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



55
56
57
# File 'lib/shattered_support/core_ext/file/search.rb', line 55

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")


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

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