Module: Polyrun::Data::FactoryCounts

Defined in:
lib/polyrun/data/factory_counts.rb

Overview

Lightweight per-example factory/build counters with zero dependencies. Call reset! in before(:suite) or setup, record inside factory helpers, summary in after(:suite).

Class Method Summary collapse

Class Method Details

.countsObject



24
25
26
27
# File 'lib/polyrun/data/factory_counts.rb', line 24

def counts
  @counts ||= Hash.new(0)
  @counts.dup
end

.example_countsObject



29
30
31
32
# File 'lib/polyrun/data/factory_counts.rb', line 29

def example_counts
  @example_counts ||= Hash.new(0)
  @example_counts.dup
end

.format_summary(title: "Polyrun factory counts") ⇒ Object



40
41
42
43
44
# File 'lib/polyrun/data/factory_counts.rb', line 40

def format_summary(title: "Polyrun factory counts")
  lines = [title]
  lines.concat(summary_lines)
  lines.join("\n") + "\n"
end

.record(factory_name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/polyrun/data/factory_counts.rb', line 16

def record(factory_name)
  @counts ||= Hash.new(0)
  @example_counts ||= Hash.new(0)
  name = factory_name.to_s
  @counts[name] += 1
  @example_counts[name] += 1
end

.reset!Object



7
8
9
10
# File 'lib/polyrun/data/factory_counts.rb', line 7

def reset!
  @counts = Hash.new(0)
  @example_counts = Hash.new(0)
end

.reset_example!Object



12
13
14
# File 'lib/polyrun/data/factory_counts.rb', line 12

def reset_example!
  @example_counts = Hash.new(0)
end

.summary_lines(top: 20) ⇒ Object



34
35
36
37
38
# File 'lib/polyrun/data/factory_counts.rb', line 34

def summary_lines(top: 20)
  @counts ||= Hash.new(0)
  sorted = @counts.sort_by { |_, n| -n }
  sorted[0, top].map { |name, n| "  #{name}: #{n}" }
end