Class: Countless::Statistics::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/countless/statistics.rb

Overview

The source code statistics calculator which holds the data of a single runtime.

Heavily stolen from: bit.ly/3tk7ZgJ

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, lines: 0, code_lines: 0, comment_lines: 0, classes: 0, methods: 0) ⇒ Countless::Statistics::Calculator

Setup a new source code statistics calculator instance.

rubocop:disable Metrics/ParameterLists because of the

various metrics we support

Parameters:

  • name (String, nil) (defaults to: nil)

    the name of the calculated path

  • lines (Integer) (defaults to: 0)

    the initial lines count

  • code_lines (Integer) (defaults to: 0)

    the initial code lines count

  • comment_lines (Integer) (defaults to: 0)

    the initial comment lines count

  • classes (Integer) (defaults to: 0)

    the initial classes count

  • methods (Integer) (defaults to: 0)

    the initial methods count



224
225
226
227
228
229
230
231
232
# File 'lib/countless/statistics.rb', line 224

def initialize(name: nil, lines: 0, code_lines: 0, comment_lines: 0,
               classes: 0, methods: 0)
  @name = name
  @lines = lines
  @code_lines = code_lines
  @comment_lines = comment_lines
  @classes = classes
  @methods = methods
end

Instance Attribute Details

#classesObject (readonly)

Expose each metric as simple readers



209
210
211
# File 'lib/countless/statistics.rb', line 209

def classes
  @classes
end

#code_linesObject (readonly)

Expose each metric as simple readers



209
210
211
# File 'lib/countless/statistics.rb', line 209

def code_lines
  @code_lines
end

#comment_linesObject (readonly)

Expose each metric as simple readers



209
210
211
# File 'lib/countless/statistics.rb', line 209

def comment_lines
  @comment_lines
end

#linesObject (readonly)

Expose each metric as simple readers



209
210
211
# File 'lib/countless/statistics.rb', line 209

def lines
  @lines
end

#methodsObject (readonly)

Expose each metric as simple readers



209
210
211
# File 'lib/countless/statistics.rb', line 209

def methods
  @methods
end

#nameObject (readonly)

Expose each metric as simple readers



209
210
211
# File 'lib/countless/statistics.rb', line 209

def name
  @name
end

Instance Method Details

#add(calculator) ⇒ Object

Add the metrics from another calculator instance to the current one.

Parameters:



239
240
241
242
243
244
245
# File 'lib/countless/statistics.rb', line 239

def add(calculator)
  @lines += calculator.lines
  @code_lines += calculator.code_lines
  @comment_lines += calculator.comment_lines
  @classes += calculator.classes
  @methods += calculator.methods
end

#add_by_file_path(path, **stats) ⇒ Object

Parse and add statistics of a single file by path.

Parameters:

  • path (String)

    the path of the file

  • stats (Hash{Symbol => Integer})

    addtional CLOC statistics



251
252
253
254
255
256
# File 'lib/countless/statistics.rb', line 251

def add_by_file_path(path, **stats)
  @lines += stats.fetch(:total, 0)
  @code_lines += stats.fetch(:code, 0)
  @comment_lines += stats.fetch(:comment, 0)
  add_details_by_file_path(path)
end

#add_details_by_file_path(path) ⇒ Object

Analyse a given input file and extract the corresponding detailed metrics. (class and method counts) Afterwards apply the new metrics to the current calculator instance metrics.

rubocop:disable Metrics/AbcSize because of the pattern search by file

extension and pattern matching on each line afterwards

rubocop:disable Metrics/CyclomaticComplexity dito rubocop:disable Metrics/PerceivedComplexity dito

Parameters:

  • path (String)

    the path of the file



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/countless/statistics.rb', line 268

def add_details_by_file_path(path)
  all_patterns = Countless.configuration.detailed_stats_patterns

  ext = path.split('.').last
  patterns = all_patterns.find do |_, conf|
    conf[:extensions].include? ext
  end&.last

  # When no detailed patterns are configured for this file,
  # we skip further processing
  return unless patterns

  # Walk through the given file, line by line
  File.read(path).lines.each do |line|
    @classes += 1 if patterns[:class]&.match? line
    @methods += 1 if patterns[:method]&.match? line
  end
end

#loc_over_mInteger

Return the lines of code per methods.

Returns:

  • (Integer)

    the lines of code per methods



302
303
304
305
306
# File 'lib/countless/statistics.rb', line 302

def loc_over_m
  code_lines / methods
rescue StandardError
  0
end

#m_over_cInteger

Return the methods per classes.

Returns:

  • (Integer)

    the methods per classes



293
294
295
296
297
# File 'lib/countless/statistics.rb', line 293

def m_over_c
  methods / classes
rescue StandardError
  0
end

#to_hHash{Symbol => Mixed}

Convert the current calculator instance to a simple hash.

Returns:

  • (Hash{Symbol => Mixed})

    the calculator values as simple hash



311
312
313
314
315
316
# File 'lib/countless/statistics.rb', line 311

def to_h
  %i[
    name lines code_lines comment_lines
    classes methods m_over_c loc_over_m
  ].each_with_object({}) { |key, memo| memo[key] = send(key) }
end