Class: Calltally::Scanner

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

Instance Method Summary collapse

Constructor Details

#initialize(base_dir:, config:) ⇒ Scanner



9
10
11
12
13
# File 'lib/calltally/scanner.rb', line 9

def initialize(base_dir:, config:)
  @base_dir = File.expand_path(base_dir)
  @config   = config
  load_plugins(@config["plugins"])
end

Instance Method Details

#scanObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/calltally/scanner.rb', line 15

def scan
  files = collect_paths
  warn_verbose "Scan targets: #{files.size} files"
  files.first(10).each { |file| warn_verbose " - #{file}" }

  pair_counts     = Hash.new(0)
  method_counts   = Hash.new(0)
  receiver_counts = Hash.new(0)

  visitor = PrismVisitor.new(@config, pair_counts, method_counts, receiver_counts)

  files.each do |path|
    begin
      src    = read_source(path)
      result = ::Prism.parse(src)
      if (root = result.value)
        visitor.visit(root)
      else
        warn_verbose "Prism.parse returned nil: #{path}"
      end
    rescue => e
      warn "Error: #{path}: #{e.class}: #{e.message}"
    end
  end

  top = Integer(@config["top"])
  mode_sym = @config["mode"].to_s.downcase.to_sym

  case mode_sym
  when :pairs
    rows = pair_counts.sort_by { |(_, _), c| -c }.first(top).map { |(r, m), c| [r, m, c] }
  when :methods
    rows = method_counts.sort_by { |_, c| -c }.first(top).map { |m, c| [m, c] }
  when :receivers
    rows = receiver_counts.sort_by { |_, c| -c }.first(top).map { |r, c| [r, c] }
  else
    raise ArgumentError, "Unknown mode: #{@config['mode']}"
  end

  [mode_sym, rows]
end