Class: ByeByeBug::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/byebyebug.rb

Constant Summary collapse

COMMENT_REGEX =
/^#.*/
BYEBUG_REGEX =
/.*(byebug|binding.pry)\s+.*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glob = "./**/*.rb") ⇒ Checker



11
12
13
14
# File 'lib/byebyebug.rb', line 11

def initialize(glob = "./**/*.rb")
  @glob = glob
  @bad_files = {} # 'path' => [list of numbers in file]
end

Instance Attribute Details

#bad_filesObject (readonly)

Returns the value of attribute bad_files.



9
10
11
# File 'lib/byebyebug.rb', line 9

def bad_files
  @bad_files
end

#globObject (readonly)

Returns the value of attribute glob.



9
10
11
# File 'lib/byebyebug.rb', line 9

def glob
  @glob
end

Instance Method Details

#checkObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/byebyebug.rb', line 16

def check      
  Dir[glob].each do |path|
    File.open(path, 'r').each_line.with_index do |line, line_number|
      next if line =~ COMMENT_REGEX # ignore if its a comment          

      # match
      if line =~ BYEBUG_REGEX          
        @bad_files[path] ||= []
        @bad_files[path] << line_number + 1 # starts at 0
      end
    end
  end
end

#errorObject



41
42
43
# File 'lib/byebyebug.rb', line 41

def error
  !@bad_files.empty?
end

#reportObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/byebyebug.rb', line 30

def report
  if error
    puts 'Bad files:'
    @bad_files.each do |path, line_numbers|
      puts "#{path} on number(s): #{line_numbers.join(',')}"
    end
  else
    puts 'No bad files'
  end
end