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.



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

def initialize(config = nil)
  @config = config || Turbulence.config
  @type = :churn
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

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#calculate_mean_of_churn(churn, sample_size) ⇒ Object



40
41
42
43
# File 'lib/turbulence/calculators/churn.rb', line 40

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

#changes_by_ruby_fileObject



28
29
30
31
32
# File 'lib/turbulence/calculators/churn.rb', line 28

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



34
35
36
37
38
# File 'lib/turbulence/calculators/churn.rb', line 34

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



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

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



22
23
24
25
26
# File 'lib/turbulence/calculators/churn.rb', line 22

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



45
46
47
48
49
# File 'lib/turbulence/calculators/churn.rb', line 45

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



62
63
64
# File 'lib/turbulence/calculators/churn.rb', line 62

def scm_log_command
  scm.log_command(commit_range)
end

#scm_log_file_linesObject



58
59
60
# File 'lib/turbulence/calculators/churn.rb', line 58

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