Class: Psychic::FileFinder
- Inherits:
-
Object
- Object
- Psychic::FileFinder
- Defined in:
- lib/psychic/util.rb
Instance Attribute Summary collapse
-
#ignored_patterns ⇒ Object
readonly
Returns the value of attribute ignored_patterns.
-
#search_path ⇒ Object
readonly
Returns the value of attribute search_path.
Instance Method Summary collapse
-
#find_file(name) ⇒ Object
Finds a file by loosely matching the file name to a scenario name.
-
#initialize(search_path, ignored_patterns) ⇒ FileFinder
constructor
A new instance of FileFinder.
- #potential_files(name) ⇒ Object
Constructor Details
#initialize(search_path, ignored_patterns) ⇒ FileFinder
Returns a new instance of FileFinder.
75 76 77 78 |
# File 'lib/psychic/util.rb', line 75 def initialize(search_path, ignored_patterns) @search_path = search_path @ignored_patterns = ignored_patterns || read_gitignore(search_path) end |
Instance Attribute Details
#ignored_patterns ⇒ Object (readonly)
Returns the value of attribute ignored_patterns.
73 74 75 |
# File 'lib/psychic/util.rb', line 73 def ignored_patterns @ignored_patterns end |
#search_path ⇒ Object (readonly)
Returns the value of attribute search_path.
73 74 75 |
# File 'lib/psychic/util.rb', line 73 def search_path @search_path end |
Instance Method Details
#find_file(name) ⇒ Object
Finds a file by loosely matching the file name to a scenario name
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/psychic/util.rb', line 81 def find_file(name) return name if File.exist? File.(name, search_path) # Filter out ignored filesFind the first file, not including generated files files = potential_files(name).select do |f| !ignored? f end # Select the shortest path, likely the best match file = files.min_by(&:length) fail Errno::ENOENT, "No file was found for #{name} within #{search_path}" if file.nil? Psychic::Util.relativize(file, search_path) end |
#potential_files(name) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/psychic/util.rb', line 96 def potential_files(name) slugified_name = Psychic::Util.slugify(name) glob_string = "#{search_path}/**/*#{slugified_name}*.*" potential_files = Dir.glob(glob_string, File::FNM_CASEFOLD) potential_files.concat Dir.glob(glob_string.gsub('_', '-'), File::FNM_CASEFOLD) potential_files.concat Dir.glob(glob_string.gsub('_', ''), File::FNM_CASEFOLD) end |