Class: Decidim::MetricRegistry
- Inherits:
-
Object
- Object
- Decidim::MetricRegistry
- Defined in:
- lib/decidim/metric_registry.rb
Overview
This class acts as a registry for metrics. Each metric needs a name and a manager class, that will be used for calculations
Also, each metrics must have a collection of attributes:
- highlighted: Determines if it showed in a highlighted chart
- scopes: List of scopes where it will be used
- weight: Priority of itself
In order to register a metric, you can follow this example:
Decidim.metrics_registry.register(:users) do
metric_registry.manager_class = "Decidim::Metrics::UsersMetricManage"
metric_registry.settings do |settings|
settings.attribute :highlighted, type: :boolean, default: true
settings.attribute :scopes, type: :array, default: %w(home)
settings.attribute :weight, type: :integer, default: 1
end
end
Metrics need to be registered in the ‘engine.rb` file of each module
Defined Under Namespace
Classes: MetricAlreadyRegistered
Instance Method Summary collapse
- #all ⇒ Object
- #filtered(highlight: nil, scope: nil, sort: nil, block: nil) ⇒ Object
- #for(metric_name, list = nil) ⇒ Object
- #highlighted(list = nil) ⇒ Object
- #not_highlighted(list = nil) ⇒ Object
-
#register(metric_name) {|metric_manifest| ... } ⇒ Object
Public: Registers a metric for calculations.
- #scoped(scope, list = nil) ⇒ Object
- #sorted(list = nil) ⇒ Object
- #stat_block(block, list = nil) ⇒ Object
Instance Method Details
#all ⇒ Object
57 58 59 |
# File 'lib/decidim/metric_registry.rb', line 57 def all metrics_manifests end |
#filtered(highlight: nil, scope: nil, sort: nil, block: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/decidim/metric_registry.rb', line 61 def filtered(highlight: nil, scope: nil, sort: nil, block: nil) result = all unless highlight.nil? result = if highlight highlighted(result) else not_highlighted(result) end end result = scoped(scope, result) if scope.present? result = sorted(result) if sort.present? result = stat_block(block, result) if block.present? result end |
#for(metric_name, list = nil) ⇒ Object
52 53 54 55 |
# File 'lib/decidim/metric_registry.rb', line 52 def for(metric_name, list = nil) list ||= all list.find { |manifest| manifest.metric_name == metric_name.to_s } end |
#highlighted(list = nil) ⇒ Object
76 77 78 79 |
# File 'lib/decidim/metric_registry.rb', line 76 def highlighted(list = nil) list ||= all list.find_all { |manifest| manifest.settings.attributes[:highlighted][:default] } end |
#not_highlighted(list = nil) ⇒ Object
81 82 83 84 |
# File 'lib/decidim/metric_registry.rb', line 81 def not_highlighted(list = nil) list ||= all list.find_all { |manifest| !manifest.settings.attributes[:highlighted][:default] } end |
#register(metric_name) {|metric_manifest| ... } ⇒ Object
Public: Registers a metric for calculations
metric_name - a symbol representing the name of the metric
Returns nothing. Raises an error if there’s already a metric registered with that metric name.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/decidim/metric_registry.rb', line 32 def register(metric_name) metric_name = metric_name.to_s metric_exists = self.for(metric_name).present? if metric_exists raise( MetricAlreadyRegistered, "There's a metric already registered with the name `:#{metric_name}`, must be unique" ) end metric_manifest = MetricManifest.new(metric_name: metric_name) yield(metric_manifest) metric_manifest.validate! metrics_manifests << metric_manifest end |
#scoped(scope, list = nil) ⇒ Object
86 87 88 89 |
# File 'lib/decidim/metric_registry.rb', line 86 def scoped(scope, list = nil) list ||= all list.find_all { |manifest| manifest.settings.attributes[:scopes][:default].include?(scope.to_s) } end |
#sorted(list = nil) ⇒ Object
96 97 98 99 |
# File 'lib/decidim/metric_registry.rb', line 96 def sorted(list = nil) list ||= all list.sort_by { |manifest| manifest.settings.attributes[:weight].default } end |
#stat_block(block, list = nil) ⇒ Object
91 92 93 94 |
# File 'lib/decidim/metric_registry.rb', line 91 def stat_block(block, list = nil) list ||= all list.find_all { |manifest| manifest.stat_block == block.to_s } end |