class MultiGrep::Grep

Attributes

invert_match[RW]
matches[RW]
regex[RW]
silent[RW]

Public Class Methods

new() click to toggle source
# File lib/multi_grep.rb, line 7
def initialize
  self.regex = Regexp.new ''
  self.matches = Hash.new { Set.new }
  self.silent = false
end

Public Instance Methods

debug() click to toggle source
# File lib/multi_grep.rb, line 42
def debug
  puts "Named captures:"
  ap matches
end
match_file(filename) click to toggle source
# File lib/multi_grep.rb, line 22
def match_file(filename)
  open(filename).each_line do |line|
    if match_data = regex.match(line.strip)
      all_match = true
      match_data.names.each do |name|
        if name[0..1] == "__"
          all_match = all_match && !matches[name[2..-1]].include?(match_data[name])
        elsif name[0] == "_"
          all_match = all_match && matches[name[1..-1]].include?(match_data[name])
        else
          matches[name] = matches[name] << match_data[name]
        end
      end
      output "#{filename}: #{line}" if all_match && !silent
    end
  end
  output ""
  self.silent = false
end
output(*arg) click to toggle source
# File lib/multi_grep.rb, line 47
def output(*arg)
  puts *arg unless self.silent
end
regex=(r) click to toggle source
# File lib/multi_grep.rb, line 18
def regex=(r)
  @regex = Regexp.new r
end
silence() click to toggle source
# File lib/multi_grep.rb, line 13
def silence
  self.silent = !silent
end