Class: Sherlock::Collection::Files

Inherits:
Base show all
Defined in:
lib/sherlock/collection/files.rb

Instance Method Summary collapse

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
24
25
26
27
# File 'lib/sherlock/collection/files.rb', line 7

def initialize(glob_or_regex, opts = {})
  case glob_or_regex
  when Hash
    opts = glob_or_regex
  when Array
    opts[:arr] = glob_or_regex
  when String
    opts[:glob] = glob_or_regex
  when Symbol
    opts[:glob] = "**/*.#{glob_or_regex}"
  when 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)
  arr = opts[:arr] || Dir[opts[:glob]]
  super(arr, opts)
end

Instance Method Details

#blank_linesObject



50
51
52
# File 'lib/sherlock/collection/files.rb', line 50

def blank_lines
  lines(/^\s+$/)
end

#collect_lines_matching(pattern = //, &block) ⇒ Object Also known as: lines

Returns a Lines collection with all lines containing the given content / matching the given pattern.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sherlock/collection/files.rb', line 31

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

#not_blank_linesObject



46
47
48
# File 'lib/sherlock/collection/files.rb', line 46

def not_blank_lines
  lines(/\S+/)
end

#select_files_containing(pattern) ⇒ Object Also known as: containing

Returns a Files collection with all files containing the given content / matching the given pattern.



56
57
58
# File 'lib/sherlock/collection/files.rb', line 56

def select_files_containing(pattern)
  select_files(pattern, :select)
end

#select_files_not_containing(pattern) ⇒ Object Also known as: not_containing

Returns a Files collection with all files not containing the given content / matching the given pattern.



63
64
65
# File 'lib/sherlock/collection/files.rb', line 63

def select_files_not_containing(pattern)
  select_files(pattern, :reject)
end