Module: TLDR::PathUtil
- Defined in:
- lib/tldr/path_util.rb
Class Method Summary collapse
- .chdir_maybe(path) ⇒ Object
-
.expand_globs(search_paths) ⇒ Object
Because search paths to TLDR can include line numbers (e.g. a.rb:4), we can’t just pass everything to Dir.glob.
- .expand_paths(path_strings, globs: true) ⇒ Object
- .locations_include_test?(locations, test) ⇒ Boolean
Class Method Details
.chdir_maybe(path) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/tldr/path_util.rb', line 43 def self.chdir_maybe path if path.nil? yield else Dir.chdir(path) { yield } end end |
.expand_globs(search_paths) ⇒ Object
Because search paths to TLDR can include line numbers (e.g. a.rb:4), we can’t just pass everything to Dir.glob. Instead, we have to check whether a user-provided search path looks like a glob, and if so, expand it
Globby characters specified here: ruby-doc.org/3.2.2/Dir.html#method-c-glob
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tldr/path_util.rb', line 26 def self. search_paths search_paths.flat_map { |search_path| if search_path.match?(/[*?\[\]{}]/) raise Error, "Can't combine globs and line numbers in: #{search_path}" if search_path.match?(/:(\d+)$/) Dir[search_path] else search_path end } end |
.expand_paths(path_strings, globs: true) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/tldr/path_util.rb', line 3 def self. path_strings, globs: true path_strings = (path_strings) if globs path_strings.flat_map { |path_string| File.directory?(path_string) ? Dir["#{path_string}/**/*.rb"] : path_string }.flat_map { |path_string| absolute_path = File.(path_string.gsub(/:.*$/, ""), Dir.pwd) line_numbers = path_string.scan(/:(\d+)/).flatten.map(&:to_i) if line_numbers.any? line_numbers.map { |line_number| Location.new(absolute_path, line_number) } else [Location.new(absolute_path, nil)] end }.uniq end |
.locations_include_test?(locations, test) ⇒ Boolean
37 38 39 40 41 |
# File 'lib/tldr/path_util.rb', line 37 def self.locations_include_test? locations, test locations.any? { |location| location.file == test.file && (location.line.nil? || test.covers_line?(location.line)) } end |