Class: Decidim::StatsRegistry

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/stats_registry.rb

Overview

This class stores different stats computations and resolves them given a context.

Constant Summary collapse

HIGH_PRIORITY =
1
MEDIUM_PRIORITY =
2
LOW_PRIORITY =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stats = []) ⇒ StatsRegistry

Public: Initializes the object with an optional stats array

stats - An array of Hashes to represent a stat object


15
16
17
# File 'decidim-core/lib/decidim/stats_registry.rb', line 15

def initialize(stats = [])
  @stats = stats
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.


10
11
12
# File 'decidim-core/lib/decidim/stats_registry.rb', line 10

def stats
  @stats
end

Instance Method Details

#except(names) ⇒ Object

Public: Creates a new registry with all stats except the provided ones

names - An Array of stats names to exclude

Returns a new StatsRegistry with the selected stats


93
94
95
96
97
98
# File 'decidim-core/lib/decidim/stats_registry.rb', line 93

def except(names)
  filtered_stats = @stats.reject do |stat|
    names.include? stat[:name]
  end
  StatsRegistry.new(filtered_stats)
end

#filter(conditions) ⇒ Object

Public: Creates a new registry with the filtered stats

conditions - A hash of conditions

* primary: Whether the stat is primary or not.
* priority: The priority of the stat used for render issues.

Returns a new StatsRegistry with the filtered stats


77
78
79
80
81
82
83
84
85
86
# File 'decidim-core/lib/decidim/stats_registry.rb', line 77

def filter(conditions)
  filtered_stats = @stats.select do |stat|
    selected = true
    conditions.each do |condition, value|
      selected = false if stat[condition] != value
    end
    selected
  end
  StatsRegistry.new(filtered_stats)
end

#only(names) ⇒ Object

Public: Creates a new registry with only the stats included into the provided ones

names - An Array of stats names to include

Returns a new StatsRegistry with the selected stats


105
106
107
108
109
110
# File 'decidim-core/lib/decidim/stats_registry.rb', line 105

def only(names)
  filtered_stats = @stats.select do |stat|
    names.include? stat[:name]
  end
  StatsRegistry.new(filtered_stats)
end

#register(name, options = {}, &block) ⇒ Object

Public: Register a stat

name - The name of the stat options - A hash of options

* primary: Whether the stat is primary or not.
* priority: The priority of the stat used for render issues.

block - A block that receive the components to filter out the stat.

Raises:

  • (StandardError)

26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'decidim-core/lib/decidim/stats_registry.rb', line 26

def register(name, options = {}, &block)
  stat = @stats.detect { |s| s[:name] == name }
  raise StandardError, "Stats '#{name}' is already registered." if stat.present?

  options[:primary] ||= false
  options[:priority] ||= LOW_PRIORITY

  @stats.push(name: name,
              primary: options[:primary],
              priority: options[:priority],
              tag: options[:tag],
              block: block)
end

#resolve(name, context, start_at = nil, end_at = nil) ⇒ Object

Public: Returns a number returned by executing the corresponding block.

name - The name of the stat context - An arbritrary context object to compute the result. start_at - A date to filter resources created after it end_at - A date to filter resources created before it.

Returns the result of executing the stats block using the passing context or an error.

Raises:

  • (StandardError)

48
49
50
51
52
53
# File 'decidim-core/lib/decidim/stats_registry.rb', line 48

def resolve(name, context, start_at = nil, end_at = nil)
  stat = @stats.detect { |s| s[:name] == name }
  return stat[:block].call(context, start_at, end_at) if stat.present?

  raise StandardError, "Stats '#{name}' is not registered."
end

#with_context(context, start_at = nil, end_at = nil) ⇒ Object

Public: Resolves every stat with the given context and return an enumerator

context - An arbritrary context object to compute the result. start_at - A date to filter resources created after it end_at - A date to filter resources created before it

Returns an Enumerator where each value is a tuple of name and resolved value


62
63
64
65
66
67
68
# File 'decidim-core/lib/decidim/stats_registry.rb', line 62

def with_context(context, start_at = nil, end_at = nil)
  Enumerator.new do |yielder|
    @stats.each do |stat|
      yielder << [stat[:name], resolve(stat[:name], context, start_at, end_at)]
    end
  end
end