Class: Gitt::Sanitizers::Statistic

Inherits:
Object
  • Object
show all
Defined in:
lib/gitt/sanitizers/statistic.rb

Overview

Converts raw text into a statistics hash.

Constant Summary collapse

EMPTY =
{files_changed: 0, insertions: 0, deletions: 0}.freeze
PATTERN =
/
  (?<total>\d+)                     # Total capture group.
  \s                                # Space delimiter.
  (?<kind>file|insertion|deletion)  # Kind capture group.
/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(empty: EMPTY, pattern: PATTERN) ⇒ Statistic

Returns a new instance of Statistic.



24
25
26
27
# File 'lib/gitt/sanitizers/statistic.rb', line 24

def initialize empty: EMPTY, pattern: PATTERN
  @empty = empty
  @pattern = pattern
end

Class Method Details

.update_stats(attributes, kind, total) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/gitt/sanitizers/statistic.rb', line 15

def self.update_stats attributes, kind, total
  case kind
    when "file" then attributes[:files_changed] = total
    when "insertion" then attributes[:insertions] = total
    when "deletion" then attributes[:deletions] = total
    else fail StandardError, "Invalid kind: #{kind.inspect}."
  end
end

Instance Method Details

#call(text) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/gitt/sanitizers/statistic.rb', line 29

def call text
  return empty unless text

  text.scan(pattern).each.with_object(empty.dup) do |(number, kind), aggregate|
    self.class.update_stats aggregate, kind, number.to_i
  end
end