Class: DebtCeiling::Accounting

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

Constant Summary collapse

DebtCeilingExceeded =
Class.new(StandardError)
TargetDeadlineMissed =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.calculate(path) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/debt_ceiling/accounting.rb', line 8

def calculate(path)
  analysed_modules = construct_rubycritic_modules(path)
  debts            = construct_debts(analysed_modules)
  max_debt         = debts.max_by(&:to_i)
  total_debt       = debts.map(&:to_i).reduce(:+)
  puts "Current total tech debt: #{total_debt}"
  puts "Largest source of debt is: #{max_debt.name} at #{max_debt.to_i}"
  total_debt
end

.construct_debts(modules) ⇒ Object



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

def construct_debts(modules)
  modules.map do |mod|
    path            = mod.path
    file_attributes = OpenStruct.new
    file_attributes.linecount = `wc -l #{path}`.match(/\d+/)[0].to_i
    file_attributes.path = path
    file_attributes.analysed_module = mod
    file_attributes.source_code = File.read(path)
    Debt.new(file_attributes)
  end
end

.construct_rubycritic_modules(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/debt_ceiling/accounting.rb', line 30

def construct_rubycritic_modules(path)
  if ENV['FULL_ANALYSIS']
    Rubycritic::Orchestrator.new.critique([path])
  else
    # temporarily use Rubycritic internals until they provide an API
    require 'rubycritic/modules_initializer'
    require 'rubycritic/analysers/complexity'
    require 'rubycritic/analysers/smells/flay'

    modules = Rubycritic::ModulesInitializer.init([path])
    [Rubycritic::Analyser::Complexity, Rubycritic::Analyser::FlaySmells].each do |analyser|
      analyser.new(modules).run
    end
    modules
  end
end