Module: DDMemoize

Defined in:
lib/ddmemoize.rb,
lib/ddmemoize/version.rb

Defined Under Namespace

Modules: Mixin Classes: Value

Constant Summary collapse

NONE =
Object.new
VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.activate(mod) ⇒ Object



20
21
22
# File 'lib/ddmemoize.rb', line 20

def self.activate(mod)
  mod.extend(Mixin)
end

.enable_metricsObject



25
26
27
# File 'lib/ddmemoize.rb', line 25

def enable_metrics
  @metrics_enabled = true
end

.metrics_counterObject



33
34
35
# File 'lib/ddmemoize.rb', line 33

def metrics_counter
  @_metrics_counter ||= DDMetrics::Counter.new
end

.metrics_enabled?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ddmemoize.rb', line 29

def metrics_enabled?
  @metrics_enabled
end


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ddmemoize.rb', line 38

def self.print_metrics
  headers = %w[memoization hit miss %]

  rows_raw = DDMemoize.metrics_counter.map do |label, count|
    {
      name: label.fetch(:method),
      type: label.fetch(:type),
      count: count,
    }
  end

  rows = rows_raw.group_by { |r| r[:name] }.map do |name, rows_for_name|
    rows_by_type = rows_for_name.group_by { |r| r[:type] }

    num_hit = rows_by_type.fetch(:hit, []).fetch(0, {}).fetch(:count, 0)
    num_miss = rows_by_type.fetch(:miss, []).fetch(0, {}).fetch(:count, 0)
    pct = num_hit.to_f / (num_hit + num_miss).to_f

    [name, num_hit.to_s, num_miss.to_s, "#{format('%3.1f', pct * 100)}%"]
  end

  all_rows = [headers] + rows
  puts DDMetrics::Table.new(all_rows).to_s
end