Module: ChurnVsComplexity::Complexity::GoCalculator

Defined in:
lib/churn_vs_complexity/complexity/go_calculator.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.command_runner=(value) ⇒ Object

Sets the attribute command_runner

Parameters:

  • value —

    the value to set the attribute command_runner to.



10
11
12
# File 'lib/churn_vs_complexity/complexity/go_calculator.rb', line 10

def command_runner=(value)
  @command_runner = value
end

Class Method Details

.calculate(files:) ⇒ Object



14
15
16
17
# File 'lib/churn_vs_complexity/complexity/go_calculator.rb', line 14

def calculate(files:)
  json_output = run_gocognit(files)
  parse_gocognit_output(json_output, files:)
end

.check_dependencies! ⇒ Object



26
27
28
29
30
# File 'lib/churn_vs_complexity/complexity/go_calculator.rb', line 26

def check_dependencies!
  command_runner.call('gocognit --help 2>&1')
rescue Errno::ENOENT
  raise Error, 'Needs gocognit installed (go install github.com/uudashr/gocognit/cmd/gocognit@latest)'
end

.folder_based? ⇒ Boolean

Returns:

  • (Boolean)


12
# File 'lib/churn_vs_complexity/complexity/go_calculator.rb', line 12

def folder_based? = false

.parse_gocognit_output(json_output, files:) ⇒ Object



19
20
21
22
23
24
# File 'lib/churn_vs_complexity/complexity/go_calculator.rb', line 19

def parse_gocognit_output(json_output, files:)
  stats = JSON.parse(json_output) || []
  scores = stats.group_by { |s| s.dig('Pos', 'Filename') }
                .transform_values { |funcs| funcs.sum { |f| f['Complexity'] } }
  files.to_h { |file| [file, scores[file] || 0] }
end