Class: StudFinder::Complexity

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

Defined Under Namespace

Classes: Error, Result

Constant Summary collapse

COMPLEXITY_COP =
'Metrics/CyclomaticComplexity'
COMPLEXITY_PATTERN =
%r{\[(\d+)/0\]}
INVALID_ENCODING_PATTERN =
/invalid byte sequence/i
PARSE_ERROR_COPS =
%w[Lint/Syntax].freeze
BATCH_SIZE =
500
RUBOCOP_CONFIG =
<<~YAML
  AllCops:
    DisabledByDefault: true
    NewCops: disable
  Metrics/CyclomaticComplexity:
    Enabled: true
    Max: 0
YAML

Instance Method Summary collapse

Constructor Details

#initialize(repo_path:, files:, stderr: $stderr) ⇒ Complexity

Returns a new instance of Complexity.



27
28
29
30
31
# File 'lib/stud_finder/complexity.rb', line 27

def initialize(repo_path:, files:, stderr: $stderr)
  @repo_path = File.expand_path(repo_path)
  @files = files
  @stderr = stderr
end

Instance Method Details

#callObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stud_finder/complexity.rb', line 33

def call
  counts = zero_counts
  skipped = []

  @files.each_slice(BATCH_SIZE) do |batch|
    result = run_batch(batch)
    counts.merge!(result.counts) { |_file, old, new| [old, new].max }
    skipped.concat(result.skipped_files)
  end

  skipped.uniq.each { |file| counts.delete(file) }
  Result.new(counts: counts, skipped_files: skipped.uniq)
rescue Errno::ENOENT
  raise Error, 'Error: rubocop not found. Install it: gem install rubocop'
rescue JSON::ParserError => e
  raise Error, "Error: failed to parse RuboCop JSON output: #{e.message}"
end