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
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
#classes ⇒ Object (readonly)
Expose each metric as simple readers
187 188 189 |
# File 'lib/countless/statistics.rb', line 187 def classes @classes end |
#code_lines ⇒ Object (readonly)
Expose each metric as simple readers
187 188 189 |
# File 'lib/countless/statistics.rb', line 187 def code_lines @code_lines end |
#comment_lines ⇒ Object (readonly)
Expose each metric as simple readers
187 188 189 |
# File 'lib/countless/statistics.rb', line 187 def comment_lines @comment_lines end |
#lines ⇒ Object (readonly)
Expose each metric as simple readers
187 188 189 |
# File 'lib/countless/statistics.rb', line 187 def lines @lines end |
#methods ⇒ Object (readonly)
Expose each metric as simple readers
187 188 189 |
# File 'lib/countless/statistics.rb', line 187 def methods @methods end |
#name ⇒ Object (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.
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.
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.
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_m ⇒ Integer
Return 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_c ⇒ Integer
Return 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_h ⇒ Hash{Symbol => Mixed}
Convert the current calculator instance to a 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 |