Module: Bovem::ShellMethods::Read

Included in:
Bovem::Shell
Defined in:
lib/bovem/shell.rb

Overview

Methods to find or check entries.

Instance Method Summary (collapse)

Instance Method Details

- (Object) check(path, tests)

Tests a path against a list of test.

Valid tests are every method available in http://www.ruby-doc.org/core-1.9.3/FileTest.html (plus read, write, execute, exec, dir). Trailing question mark can be omitted.

Parameters:

  • path (String)

    The path to test.

  • tests (Array)

    The list of tests to perform.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bovem/shell.rb', line 58

def check(path, tests)
  path = path.ensure_string

  tests.ensure_array.all? {|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

- (Object) find(directories, patterns = [], by_extension = false, case_sensitive = false, &block)

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 matched if the blocks not evaluates to nil or false.

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

Parameters:

  • directories (String)

    A list of directories where to search files.

  • patterns (Array) (defaults to: [])

    A list of regexps or patterns to match files. If empty, every file is returned. Ignored if a block is provided.

  • by_extension (Boolean) (defaults to: false)

    If to only search in extensions. Ignored if a block is provided.

  • case_sensitive (Boolean) (defaults to: false)

    If the search is case sensitive. Only meaningful for string patterns.

  • block (Proc)

    An optional block to perform matching instead of pattern matching.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bovem/shell.rb', line 90

def find(directories, patterns = [], by_extension = false, case_sensitive = false, &block)
  rv = []

  directories = directories.ensure_array.compact {|d| File.expand_path(d.ensure_string) }
  patterns = normalize_patterns(patterns, by_extension, case_sensitive)

  directories.each do |directory|
    if self.check(directory, [:directory, :readable, :executable]) then
      Find.find(directory) do |entry|
        found = patterns.blank? ? true : match_pattern(entry, patterns, by_extension, &block)

        rv << entry if found
      end
    end
  end

  rv
end