Class: Turbulence::Calculators::Churn

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/turbulence/calculators/churn.rb

Constant Summary collapse

RUBY_FILE_EXTENSION =
".rb"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Churn

Returns a new instance of Churn.



9
10
11
# File 'lib/turbulence/calculators/churn.rb', line 9

def initialize(config = nil)
  @config = config || Turbulence.config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/turbulence/calculators/churn.rb', line 8

def config
  @config
end

Instance Method Details

#calculate_mean_of_churn(churn, sample_size) ⇒ Object



38
39
40
41
# File 'lib/turbulence/calculators/churn.rb', line 38

def calculate_mean_of_churn(churn, sample_size)
  return churn if sample_size < 1
  churn /= sample_size
end

#changes_by_ruby_fileObject



26
27
28
29
30
# File 'lib/turbulence/calculators/churn.rb', line 26

def changes_by_ruby_file
  ruby_files_changed_in_scm.group_by(&:first).map do |filename, stats|
    churn_for_file(filename,stats)
  end
end

#churn_for_file(filename, stats) ⇒ Object



32
33
34
35
36
# File 'lib/turbulence/calculators/churn.rb', line 32

def churn_for_file(filename,stats)
  churn = stats[0..-2].map(&:last).inject(0){|running_total, changes| running_total + changes}
  churn = calculate_mean_of_churn(churn, stats.size - 1) if compute_mean
  [filename, churn]
end

#counted_line_changes_by_file_by_commitObject



49
50
51
52
53
54
# File 'lib/turbulence/calculators/churn.rb', line 49

def counted_line_changes_by_file_by_commit
  scm_log_file_lines.map do |line|
    adds, deletes, filename = line.split(/\t/)
    [filename, adds.to_i + deletes.to_i]
  end
end

#for_these_files(files) ⇒ Object



20
21
22
23
24
# File 'lib/turbulence/calculators/churn.rb', line 20

def for_these_files(files)
  changes_by_ruby_file.each do |filename, count|
    yield filename, count if files.include?(filename)
  end
end

#ruby_files_changed_in_scmObject



43
44
45
46
47
# File 'lib/turbulence/calculators/churn.rb', line 43

def ruby_files_changed_in_scm
  counted_line_changes_by_file_by_commit.select do |filename, _|
    filename.end_with?(RUBY_FILE_EXTENSION) && File.exist?(filename)
  end
end

#scm_log_commandObject



60
61
62
# File 'lib/turbulence/calculators/churn.rb', line 60

def scm_log_command
  scm.log_command(commit_range)
end

#scm_log_file_linesObject



56
57
58
# File 'lib/turbulence/calculators/churn.rb', line 56

def scm_log_file_lines
  scm_log_command.each_line.reject{|line| line == "\n"}.map(&:chomp)
end