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 =

The priority of the stat used for render organization level stats.

1
MEDIUM_PRIORITY =

The priority of the stat used for render participatory space level stats.

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



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

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



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

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



117
118
119
120
121
122
# File 'decidim-core/lib/decidim/stats_registry.rb', line 117

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
39
40
41
42
43
# 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[:admin] = true unless options.has_key?(:admin)
  options[:priority] ||= LOW_PRIORITY

  @stats.push(name:,
              primary: options[:primary],
              priority: options[:priority],
              tag: options[:tag],
              icon_name: options[:icon_name],
              tooltip_key: options[:tooltip_key],
              sub_title: options[:sub_title],
              admin: options[:admin],
              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 arbitrary 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)


53
54
55
56
57
58
# File 'decidim-core/lib/decidim/stats_registry.rb', line 53

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 arbitrary 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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'decidim-core/lib/decidim/stats_registry.rb', line 67

def with_context(context, start_at = nil, end_at = nil)
  Enumerator.new do |yielder|
    @stats.each do |stat|
      yielder << {
        name: stat[:name],
        data: resolve(stat[:name], context, start_at, end_at),
        icon_name: stat[:icon_name],
        tooltip_key: stat[:tooltip_key],
        sub_title: stat[:sub_title],
        admin: stat[:admin]
      }
    end
  end
end