Sha256: 5b7817068abdc0e9278978c5c92e3cd9730733fca0d70f95f857a4314174031d

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'churn/calculator'

require 'attractor/value'

module Attractor
  # calculates churn and complexity
  class BaseCalculator
    attr_reader :type

    def initialize(file_prefix: '', file_extension: 'rb', minimum_churn_count: 3, start_ago: '5y')
      @file_prefix = file_prefix
      @file_extension = file_extension
      @minimum_churn_count = minimum_churn_count
      @start_date = Date.today - Attractor::DurationParser.new(start_ago).duration
    end

    def calculate
      churn = ::Churn::ChurnCalculator.new(
        file_extension: @file_extension,
        file_prefix: @file_prefix,
        minimum_churn_count: @minimum_churn_count,
        start_date: @start_date
      ).report(false)

      churn[:churn][:changes].map do |change|
        complexity, details = yield(change)
        Value.new(file_path: change[:file_path],
                  churn: change[:times_changed],
                  complexity: complexity,
                  details: details,
                  history: git_history_for_file(file_path: change[:file_path]))
      end
    end

    private

    def git_history_for_file(file_path:, limit: 10)
      history = `git log --oneline -n #{limit} -- #{file_path}`
      history.split("\n")
             .map do |log_entry|
        log_entry.partition(/\A(\S+)\s/)
                 .map(&:strip)
                 .reject(&:empty?)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
attractor-2.0.5 lib/attractor/calculators/base_calculator.rb
attractor-2.0.4 lib/attractor/calculators/base_calculator.rb
attractor-2.0.3 lib/attractor/calculators/base_calculator.rb
attractor-2.0.2 lib/attractor/calculators/base_calculator.rb
attractor-2.0.1 lib/attractor/calculators/base_calculator.rb
attractor-2.0.0 lib/attractor/calculators/base_calculator.rb
attractor-1.2.0 lib/attractor/calculators/base_calculator.rb
attractor-1.1.1 lib/attractor/calculators/base_calculator.rb