Class: Sherlock::Collection::Files
- Defined in:
- lib/collection/files.rb
Instance Method Summary collapse
-
#collect_lines_matching(pattern, &block) ⇒ Object
(also: #collect, #lines)
Returns all the lines matching the given pattern.
-
#initialize(glob_or_regex, opts = {}) ⇒ Files
constructor
A new instance of Files.
-
#select_files_containing(pattern) ⇒ Object
(also: #containing)
Returns a FileCollection with all files containing the given content / matching the given pattern.
-
#select_files_not_containing(pattern) ⇒ Object
(also: #not_containing)
Returns a FileCollection with all files not containing the given content / matching the given pattern.
Methods inherited from Base
#&, #+, #-, #[], #first, #select_items_matching, #|
Constructor Details
#initialize(glob_or_regex, opts = {}) ⇒ Files
Returns a new instance of Files.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/collection/files.rb', line 7 def initialize(glob_or_regex, opts = {}) if glob_or_regex.is_a?(Hash) opts = glob_or_regex elsif glob_or_regex.is_a?(Array) opts[:arr] = glob_or_regex elsif glob_or_regex.is_a?(String) opts[:glob] = glob_or_regex elsif glob_or_regex.is_a?(Regexp) if opts[:only] raise "Cannot use regexp and :only-option at the same time." else opts[:only] = glob_or_regex end end opts = {:glob => '*'}.merge(opts) super(opts[:arr] || Dir[opts[:glob]], opts) end |
Instance Method Details
#collect_lines_matching(pattern, &block) ⇒ Object Also known as: collect, lines
Returns all the lines matching the given pattern.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/collection/files.rb', line 26 def collect_lines_matching(pattern, &block) pattern = [pattern].flatten lines = Lines.new self.each { |f| io = File.open(f) io.each { |line| if matching?(line, pattern) lines << MatchedLine.new(line, :file => f, :line_number => io.lineno, :pattern => pattern) end } } lines end |
#select_files_containing(pattern) ⇒ Object Also known as: containing
Returns a FileCollection with all files containing the given content / matching the given pattern.
45 46 47 48 49 |
# File 'lib/collection/files.rb', line 45 def select_files_containing(pattern) pattern = [pattern].flatten arr = select { |f| matching?(File.read(f), pattern) } new(arr) end |
#select_files_not_containing(pattern) ⇒ Object Also known as: not_containing
Returns a FileCollection with all files not containing the given content / matching the given pattern.
54 55 56 57 58 |
# File 'lib/collection/files.rb', line 54 def select_files_not_containing(pattern) pattern = [pattern].flatten arr = select { |f| !matching?(File.read(f), pattern) } new(arr) end |