Module: RuboCop::PathUtil
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Included In:
Config ,
Cop::Metrics::Utils::CodeLengthCalculator ,
Cop::PercentLiteralCorrector ,
Cop::Util ,
Formatter::AutoGenConfigFormatter ,
Formatter::ClangStyleFormatter ,
Formatter::DisabledConfigFormatter ,
Formatter::FuubarStyleFormatter ,
Formatter::HTMLFormatter::ERBContext ,
Formatter::JSONFormatter ,
Formatter::MarkdownFormatter ,
Formatter::PacmanFormatter ,
Formatter::ProgressFormatter ,
Formatter::QuietFormatter ,
Formatter::SimpleTextFormatter ,
Formatter::TapFormatter
| |
Defined in: | lib/rubocop/path_util.rb |
Overview
Common methods and behaviors for dealing with paths.
Constant Summary
-
HIDDEN_FILE_PATTERN =
# File 'lib/rubocop/path_util.rb', line 98"#{File::SEPARATOR}."
-
SMART_PATH_CACHE =
private
# File 'lib/rubocop/path_util.rb', line 31{}
Class Attribute Summary
Class Method Summary
-
.absolute?(path) ⇒ Boolean
mod_func
Returns true for an absolute Unix or Windows path.
-
.glob?(path) ⇒ Boolean
mod_func
Returns true for a glob.
- .hidden_dir?(path) ⇒ Boolean mod_func
- .hidden_file?(path) ⇒ Boolean mod_func
- .hidden_file_in_not_hidden_dir?(pattern, path) ⇒ Boolean mod_func
-
.match_path?(pattern, path) ⇒ Boolean
mod_func
Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
.maybe_hidden_file?(path) ⇒ Boolean
mod_func
Loose check to reduce memory allocations.
- .relative_path(path, base_dir = Dir.pwd) mod_func
- .smart_path(path) mod_func
Class Attribute Details
.relative_paths_cache (rw)
[ GitHub ]# File 'lib/rubocop/path_util.rb', line 7
attr_accessor :relative_paths_cache
Class Method Details
.absolute?(path) ⇒ Boolean
(mod_func)
Returns true for an absolute Unix or Windows path.
# File 'lib/rubocop/path_util.rb', line 76
def absolute?(path) %r{\A([A-Z]:)?/}i.match?(path) end
.glob?(path) ⇒ Boolean
(mod_func)
Returns true for a glob
# File 'lib/rubocop/path_util.rb', line 81
def glob?(path) path.match?(/[*{\[?]/) end
.match_path?(pattern, path) ⇒ Boolean
(mod_func)
Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# File 'lib/rubocop/path_util.rb', line 48
def match_path?(pattern, path) case pattern when String matches = if pattern == path true elsif glob?(pattern) # File name matching doesn't really work with relative patterns that start with "..". We # get around that problem by converting the pattern to an absolute path. pattern = File. (pattern) if pattern.start_with?('..') File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) end matches || hidden_file_in_not_hidden_dir?(pattern, path) when Regexp begin pattern.match?(path) rescue ArgumentError => e return false if e. .start_with?('invalid byte sequence') raise e end end end
.relative_path(path, base_dir = Dir.pwd) (mod_func)
[ GitHub ]# File 'lib/rubocop/path_util.rb', line 13
def relative_path(path, base_dir = Dir.pwd) PathUtil.relative_paths_cache[base_dir][path] ||= # Optimization for the common case where path begins with the base # dir. Just cut off the first part. if path.start_with?(base_dir) base_dir_length = base_dir.length result_length = path.length - base_dir_length - 1 path[base_dir_length + 1, result_length] else path_name = Pathname.new(File. (path)) begin path_name.relative_path_from(Pathname.new(base_dir)).to_s rescue ArgumentError path end end end
.smart_path(path) (mod_func)
[ GitHub ]# File 'lib/rubocop/path_util.rb', line 34
def smart_path(path) SMART_PATH_CACHE[path] ||= begin # Ideally, we calculate this relative to the project root. base_dir = Dir.pwd if path.start_with? base_dir relative_path(path, base_dir) else path end end end