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



202
203
204
205
206
207
208
209
210
# File 'lib/countless/statistics.rb', line 202

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



187
188
189
# File 'lib/countless/statistics.rb', line 187

def classes
  @classes
end

#code_linesObject (readonly)

Expose each metric as simple readers



187
188
189
# File 'lib/countless/statistics.rb', line 187

def code_lines
  @code_lines
end

#comment_linesObject (readonly)

Expose each metric as simple readers



187
188
189
# File 'lib/countless/statistics.rb', line 187

def comment_lines
  @comment_lines
end

#linesObject (readonly)

Expose each metric as simple readers



187
188
189
# File 'lib/countless/statistics.rb', line 187

def lines
  @lines
end

#methodsObject (readonly)

Expose each metric as simple readers



187
188
189
# File 'lib/countless/statistics.rb', line 187

def methods
  @methods
end

#nameObject (readonly)

Expose each metric as simple readers



187
188
189
# File 'lib/countless/statistics.rb', line 187

def name
  @name
end

Instance Method Details

#add(calculator) ⇒ Object

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

Parameters:



217
218
219
220
221
222
223
# File 'lib/countless/statistics.rb', line 217

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})

    additional CLOC statistics



229
230
231
232
233
234
# File 'lib/countless/statistics.rb', line 229

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.

Parameters:

  • path (String)

    the path of the file



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/countless/statistics.rb', line 241

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



272
273
274
275
276
# File 'lib/countless/statistics.rb', line 272

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



263
264
265
266
267
# File 'lib/countless/statistics.rb', line 263

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



281
282
283
284
285
286
# File 'lib/countless/statistics.rb', line 281

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