module Bovem::ShellMethods::Read

Methods to find or check entries.

Public Instance Methods

check(path, *tests) click to toggle source

Tests a path against a list of test.

Valid tests are every method available in www.ruby-doc.org/core-2.3.0/FileTest.html (plus `read`, `write`, `execute`, `exec`, `dir`).

Trailing question mark can be omitted. Unrecognized tests will make the check fail.

@param path [String] The path to test. @param tests [Array] The list of tests to perform.

# File lib/bovem/shell.rb, line 51
def check(path, *tests)
  path = path.ensure_string

  tests.ensure_array(no_duplicates: true, compact: true, flatten: true).all? do |test|
    # Adjust test name
    test = test.ensure_string.strip

    test =
      case test
      when "read" then "readable"
      when "write" then "writable"
      when "execute", "exec" then "executable"
      when "dir" then "directory"
      else test
      end

    # Execute test
    test += "?" if test !~ /\?$/
    FileTest.respond_to?(test) ? FileTest.send(test, path) : nil
  end
end
find(directories, patterns: [], extension_only: false, case_sensitive: false, &block) click to toggle source

Find a list of files in directories matching given regexps or patterns.

You can also pass a block to perform matching. The block will receive a single argument and the path will be considered if return value is not falsey.

Inside the block, you can call `Find.prune` to stop searching in the current directory.

@param directories [String] A list of directories where to search files. @param patterns [Array] A list of regexps or patterns to match files. If empty, every file is returned. Ignored if a block is provided. @param extension_only [Boolean] If to only search in extensions. Ignored if a block is provided. @param case_sensitive [Boolean] If the search is case sensitive. Only meaningful for string patterns. @param block [Proc] An optional block to perform matching instead of pattern matching.

# File lib/bovem/shell.rb, line 84
def find(directories, patterns: [], extension_only: false, case_sensitive: false, &block)
  rv = []

  directories = directories.ensure_array(no_duplicates: true, compact: true, flatten: true) { |d| File.expand_path(d.ensure_string) }
  patterns = normalize_patterns(patterns, extension_only, case_sensitive)

  directories.each do |directory|
    next unless check(directory, [:directory, :readable, :executable])
    Find.find(directory) do |entry|
      found = patterns.blank? ? true : match_pattern(entry, patterns, extension_only, &block)

      rv << entry if found
    end
  end

  rv
end