Module: RuboCop::PathUtil
- Included in:
- Config, Cop::Util, Formatter::DisabledLinesFormatter, Formatter::HTMLFormatter::ERBContext, Formatter::JSONFormatter, Formatter::SimpleTextFormatter
- Defined in:
- lib/rubocop/path_util.rb
Overview
Common methods and behaviors for dealing with paths.
Class Method Summary collapse
-
.absolute?(path) ⇒ Boolean
Returns true for an absolute Unix or Windows path.
- .match_path?(pattern, path) ⇒ Boolean
- .relative_path(path, base_dir = Dir.pwd) ⇒ Object
- .smart_path(path) ⇒ Object
Class Method Details
.absolute?(path) ⇒ Boolean
Returns true for an absolute Unix or Windows path.
43 44 45 |
# File 'lib/rubocop/path_util.rb', line 43 def absolute?(path) path =~ %r{\A([A-Z]:)?/} end |
.match_path?(pattern, path) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rubocop/path_util.rb', line 28 def match_path?(pattern, path) case pattern when String File.fnmatch?(pattern, path, File::FNM_PATHNAME) when Regexp begin path =~ pattern rescue ArgumentError => e return false if e..start_with?('invalid byte sequence') raise e end end end |
.relative_path(path, base_dir = Dir.pwd) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/rubocop/path_util.rb', line 8 def relative_path(path, base_dir = Dir.pwd) # Optimization for the common case where path begins with the base # dir. Just cut off the first part. return path[(base_dir.length + 1)..-1] if path.start_with?(base_dir) path_name = Pathname.new(File.(path)) path_name.relative_path_from(Pathname.new(base_dir)).to_s end |
.smart_path(path) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rubocop/path_util.rb', line 17 def smart_path(path) # 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 |