Class: Decidim::MetricOperation

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

Defined Under Namespace

Classes: MetricOperationAlreadyRegistered

Instance Method Summary collapse

Instance Method Details

#allObject



46
47
48
# File 'decidim-core/lib/decidim/metric_operation.rb', line 46

def all
  metrics_manifests
end

#for(metric_operation, metric_name = nil) ⇒ Object

Searches for MetricOperationManifest(s) depending on parameters With ‘metric_operation’ only:

- Returns all manifest related to that operation

With ‘metric_operation’ and ‘metric_name’:

- Returns a single manifest related to that two params


38
39
40
41
42
43
44
# File 'decidim-core/lib/decidim/metric_operation.rb', line 38

def for(metric_operation, metric_name = nil)
  if metric_name
    all.find { |manifest| manifest.metric_operation == metric_operation.to_s && manifest.metric_name == metric_name.to_s }
  else
    all.find_all { |manifest| manifest.metric_operation == metric_operation.to_s }
  end
end

#register(metric_operation, metric_name) {|metric_manifest| ... } ⇒ Object

Public: Registers a operation for metrics

metric_operation - a symbol representing the name of the operation involved metric_name - a symbol representing the name of the metric involved

Returns nothing. Raises an error if there is already a metric registered with that metric name.

Yields:

  • (metric_manifest)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'decidim-core/lib/decidim/metric_operation.rb', line 12

def register(metric_operation, metric_name)
  metric_operation = metric_operation.to_s
  metric_name = metric_name.to_s
  metric_exists = self.for(metric_operation, metric_name).present?

  if metric_exists
    raise(
      MetricOperationAlreadyRegistered,
      "There is a metric already registered with the name `:#{metric_name}`, must be unique"
    )
  end

  metric_manifest = MetricOperationManifest.new(metric_operation:, metric_name:)

  yield(metric_manifest)

  metric_manifest.validate!

  metrics_manifests << metric_manifest
end