Class: Spree::Reporting::Counters

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/reporting/counters.rb

Overview

Evaluates the registered counters for one store, filtered to those the caller may read. Carries no copy: a counter is identified by its key and the client owns every string it shows, so an interface in one language never renders a number labelled in another.

Instance Method Summary collapse

Constructor Details

#initialize(store:, channel: nil, registry: Spree.reporting, allowed: ->(_counter) { true }) ⇒ Counters

Returns a new instance of Counters.

Parameters:

  • store (Spree::Store)
  • channel (Spree::Channel, nil) (defaults to: nil) —

    narrows order-derived counts; nil means all channels

  • registry (Registry) (defaults to: Spree.reporting)
  • allowed (Proc) (defaults to: ->(_counter) { true }) —

    ->(counter) { true/false } — the same permission gate the schema applies to metrics and dimensions



13
14
15
16
17
18
# File 'lib/spree/reporting/counters.rb', line 13

def initialize(store:, channel: nil, registry: Spree.reporting, allowed: ->(_counter) { true })
  @store = store
  @channel = channel
  @registry = registry
  @allowed = allowed
end

Instance Method Details

#to_a ⇒ Array<CounterResult>

Returns in registration order.

Returns:



21
22
23
24
25
26
27
28
29
30
# File 'lib/spree/reporting/counters.rb', line 21

def to_a
  @registry.counters.values.select { |counter| @allowed.call(counter) }.map do |counter|
    CounterResult.new(
      key: counter.name,
      value: counter.count.call(@store, channel: @channel),
      link: counter.link&.deep_stringify_keys,
      nav: counter.nav
    )
  end
end