Class: DebtCeiling::Debt

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/debt_ceiling/debt.rb

Constant Summary collapse

DoNotWhitelistAndBlacklistSimulateneously =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_attributes) ⇒ Debt

Returns a new instance of Debt.



13
14
15
16
# File 'lib/debt_ceiling/debt.rb', line 13

def initialize(file_attributes)
  @file_attributes  = file_attributes
  default_measure_debt if valid_debt?
end

Instance Attribute Details

#debt_amountObject

Returns the value of attribute debt_amount.



10
11
12
# File 'lib/debt_ceiling/debt.rb', line 10

def debt_amount
  @debt_amount
end

#file_attributesObject (readonly)

Returns the value of attribute file_attributes.



7
8
9
# File 'lib/debt_ceiling/debt.rb', line 7

def file_attributes
  @file_attributes
end

Class Method Details

.blacklist_includes?(debt) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/debt_ceiling/debt.rb', line 71

def self.blacklist_includes?(debt)
  DebtCeiling.blacklist.find { |filename| filename.match debt.path }
end

.whitelist_includes?(debt) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/debt_ceiling/debt.rb', line 67

def self.whitelist_includes?(debt)
  DebtCeiling.whitelist.find { |filename| filename.match debt.path }
end

Instance Method Details

#+(other) ⇒ Object



79
80
81
# File 'lib/debt_ceiling/debt.rb', line 79

def +(other)
  to_i + other.to_i
end

#debt_from_callout(callout) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/debt_ceiling/debt.rb', line 49

def debt_from_callout(callout)
  source_code.each_line.reduce(0) do |sum, line|
    match_data = line.match(Regexp.new(callout + '.*'))
    string = match_data.to_s.split(callout).last
    amount = string.match(/\d+/).to_s if string
    sum + amount.to_i
  end
end

#debt_from_source_code_rulesObject



32
33
34
35
36
37
# File 'lib/debt_ceiling/debt.rb', line 32

def debt_from_source_code_rules
  manual_callout_debt +
  text_match_debt('TODO', DebtCeiling.cost_per_todo) +
  DebtCeiling.deprecated_reference_pairs
    .reduce(0) {|accum, (string, value)| accum + text_match_debt(string, value.to_i) }
end

#default_measure_debtObject



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

def default_measure_debt
  cost = public_send(:measure_debt) if self.respond_to?(:measure_debt)

  unless cost
    cost = public_send(:augment_debt) if respond_to?(:augment_debt)
    cost = cost.to_i
    letter_grade = rating.to_s.downcase
    cost_per_line = DebtCeiling.configuration.grade_points[letter_grade.to_sym]
    cost += file_attributes.linecount * cost_per_line
    cost += debt_from_source_code_rules
  end
  self.debt_amount = cost
end

#manual_callout_debtObject



43
44
45
46
47
# File 'lib/debt_ceiling/debt.rb', line 43

def manual_callout_debt
  DebtCeiling.manual_callouts.reduce(0) do |sum, callout|
    sum + debt_from_callout(callout)
  end
end

#nameObject



75
76
77
# File 'lib/debt_ceiling/debt.rb', line 75

def name
  file_attributes.analysed_module.name || path.to_s.split('/').last
end

#text_match_debt(string, cost) ⇒ Object



39
40
41
# File 'lib/debt_ceiling/debt.rb', line 39

def text_match_debt(string, cost)
  source_code.scan(string).count * cost.to_i
end

#valid_debt?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/debt_ceiling/debt.rb', line 58

def valid_debt?
  black_empty = DebtCeiling.blacklist.empty?
  white_empty = DebtCeiling.whitelist.empty?
  fail DoNotWhitelistAndBlacklistSimulateneously unless black_empty || white_empty
  (black_empty && white_empty) ||
  (black_empty && self.class.whitelist_includes?(self)) ||
  (white_empty && !self.class.blacklist_includes?(self))
end