Class: Countless::Statistics::Calculator
- Inherits:
-
Object
- Object
- Countless::Statistics::Calculator
- 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
-
#classes ⇒ Object
readonly
Expose each metric as simple readers.
-
#code_lines ⇒ Object
readonly
Expose each metric as simple readers.
-
#comment_lines ⇒ Object
readonly
Expose each metric as simple readers.
-
#lines ⇒ Object
readonly
Expose each metric as simple readers.
-
#methods ⇒ Object
readonly
Expose each metric as simple readers.
-
#name ⇒ Object
readonly
Expose each metric as simple readers.
Instance Method Summary collapse
-
#add(calculator) ⇒ Object
Add the metrics from another calculator instance to the current one.
-
#add_by_file_path(path, **stats) ⇒ Object
Parse and add statistics of a single file by path.
-
#add_details_by_file_path(path) ⇒ Object
Analyse a given input file and extract the corresponding detailed metrics.
-
#initialize(name: nil, lines: 0, code_lines: 0, comment_lines: 0, classes: 0, methods: 0) ⇒ Countless::Statistics::Calculator
constructor
Setup a new source code statistics calculator instance.
-
#loc_over_m ⇒ Integer
Return the lines of code per methods.
-
#m_over_c ⇒ Integer
Return the methods per classes.
-
#to_h ⇒ Hash{Symbol => Mixed}
Convert the current calculator instance to a simple hash.
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
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
#classes ⇒ Object (readonly)
Expose each metric as simple readers
209 210 211 |
# File 'lib/countless/statistics.rb', line 209 def classes @classes end |
#code_lines ⇒ Object (readonly)
Expose each metric as simple readers
209 210 211 |
# File 'lib/countless/statistics.rb', line 209 def code_lines @code_lines end |
#comment_lines ⇒ Object (readonly)
Expose each metric as simple readers
209 210 211 |
# File 'lib/countless/statistics.rb', line 209 def comment_lines @comment_lines end |
#lines ⇒ Object (readonly)
Expose each metric as simple readers
209 210 211 |
# File 'lib/countless/statistics.rb', line 209 def lines @lines end |
#methods ⇒ Object (readonly)
Expose each metric as simple readers
209 210 211 |
# File 'lib/countless/statistics.rb', line 209 def methods @methods end |
#name ⇒ Object (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.
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.
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
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_m ⇒ Integer
Return 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_c ⇒ Integer
Return 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_h ⇒ Hash{Symbol => Mixed}
Convert the current calculator instance to a 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 |