Class: ArchUnit::Metrics::FluentApi::MetricSelection

Inherits:
Object
  • Object
show all
Defined in:
lib/archunit/metrics/fluentapi/metric_selection.rb

Overview

Lazy selection of one metric over a metrics scope.

Direct Known Subclasses

CustomMetricBuilder

Constant Summary collapse

THRESHOLD_METHODS =
{
  should_be_below: :below,
  should_be_above: :above,
  should_be: :equal,
  should_be_below_or_equal: :below_or_equal,
  should_be_above_or_equal: :above_or_equal
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, metric:) ⇒ MetricSelection

Returns a new instance of MetricSelection.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 33

def initialize(scope:, metric:)
  raise ArgumentError, 'scope must be a MetricsBuilder' unless scope.is_a?(MetricsBuilder)
  raise ArgumentError, 'metric must be a Metric' unless metric.is_a?(Calculation::Metric)

  @scope = scope
  @metric = metric
  freeze
end

Instance Attribute Details

#metric ⇒ Object (readonly)

Returns the value of attribute metric.



31
32
33
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 31

def metric
  @metric
end

#scope ⇒ Object (readonly)

Returns the value of attribute scope.



31
32
33
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 31

def scope
  @scope
end

Instance Method Details

#measure(options = nil) ⇒ Array<MetricMeasurement>

Evaluates this metric for every selected subject.

Parameters:

  • options (CheckOptions, nil) (defaults to: nil) —

    dependency-analysis options for coupling metrics

Returns:



45
46
47
48
49
50
51
52
53
54
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 45

def measure(options = nil)
  options = Common::FluentApi::CheckOptions.resolve(options)
  scope.__send__(:subjects_for, metric.subject_type, options:).map do |subject|
    MetricMeasurement.new(
      subject:,
      metric_name: metric.name,
      value: metric.calculate(subject)
    )
  end.freeze
end

#should_be(threshold) ⇒ MetricThresholdCondition



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 22

class MetricSelection
  THRESHOLD_METHODS = {
    should_be_below: :below,
    should_be_above: :above,
    should_be: :equal,
    should_be_below_or_equal: :below_or_equal,
    should_be_above_or_equal: :above_or_equal
  }.freeze

  attr_reader :scope, :metric

  def initialize(scope:, metric:)
    raise ArgumentError, 'scope must be a MetricsBuilder' unless scope.is_a?(MetricsBuilder)
    raise ArgumentError, 'metric must be a Metric' unless metric.is_a?(Calculation::Metric)

    @scope = scope
    @metric = metric
    freeze
  end

  # Evaluates this metric for every selected subject.
  # @param options [CheckOptions, nil] dependency-analysis options for coupling metrics
  # @return [Array<MetricMeasurement>]
  def measure(options = nil)
    options = Common::FluentApi::CheckOptions.resolve(options)
    scope.__send__(:subjects_for, metric.subject_type, options:).map do |subject|
      MetricMeasurement.new(
        subject:,
        metric_name: metric.name,
        value: metric.calculate(subject)
      )
    end.freeze
  end

  THRESHOLD_METHODS.each do |method_name, comparison|
    define_method(method_name) do |threshold|
      MetricThresholdCondition.new(selection: self, comparison:, threshold:)
    end
  end

  # Creates a rule from a custom predicate over value and subject.
  # @return [MetricPredicateCondition]
  def should_satisfy(predicate)
    MetricPredicateCondition.new(selection: self, predicate:)
  end
end

#should_be_above(threshold) ⇒ MetricThresholdCondition



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 22

class MetricSelection
  THRESHOLD_METHODS = {
    should_be_below: :below,
    should_be_above: :above,
    should_be: :equal,
    should_be_below_or_equal: :below_or_equal,
    should_be_above_or_equal: :above_or_equal
  }.freeze

  attr_reader :scope, :metric

  def initialize(scope:, metric:)
    raise ArgumentError, 'scope must be a MetricsBuilder' unless scope.is_a?(MetricsBuilder)
    raise ArgumentError, 'metric must be a Metric' unless metric.is_a?(Calculation::Metric)

    @scope = scope
    @metric = metric
    freeze
  end

  # Evaluates this metric for every selected subject.
  # @param options [CheckOptions, nil] dependency-analysis options for coupling metrics
  # @return [Array<MetricMeasurement>]
  def measure(options = nil)
    options = Common::FluentApi::CheckOptions.resolve(options)
    scope.__send__(:subjects_for, metric.subject_type, options:).map do |subject|
      MetricMeasurement.new(
        subject:,
        metric_name: metric.name,
        value: metric.calculate(subject)
      )
    end.freeze
  end

  THRESHOLD_METHODS.each do |method_name, comparison|
    define_method(method_name) do |threshold|
      MetricThresholdCondition.new(selection: self, comparison:, threshold:)
    end
  end

  # Creates a rule from a custom predicate over value and subject.
  # @return [MetricPredicateCondition]
  def should_satisfy(predicate)
    MetricPredicateCondition.new(selection: self, predicate:)
  end
end

#should_be_above_or_equal(threshold) ⇒ MetricThresholdCondition



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 22

class MetricSelection
  THRESHOLD_METHODS = {
    should_be_below: :below,
    should_be_above: :above,
    should_be: :equal,
    should_be_below_or_equal: :below_or_equal,
    should_be_above_or_equal: :above_or_equal
  }.freeze

  attr_reader :scope, :metric

  def initialize(scope:, metric:)
    raise ArgumentError, 'scope must be a MetricsBuilder' unless scope.is_a?(MetricsBuilder)
    raise ArgumentError, 'metric must be a Metric' unless metric.is_a?(Calculation::Metric)

    @scope = scope
    @metric = metric
    freeze
  end

  # Evaluates this metric for every selected subject.
  # @param options [CheckOptions, nil] dependency-analysis options for coupling metrics
  # @return [Array<MetricMeasurement>]
  def measure(options = nil)
    options = Common::FluentApi::CheckOptions.resolve(options)
    scope.__send__(:subjects_for, metric.subject_type, options:).map do |subject|
      MetricMeasurement.new(
        subject:,
        metric_name: metric.name,
        value: metric.calculate(subject)
      )
    end.freeze
  end

  THRESHOLD_METHODS.each do |method_name, comparison|
    define_method(method_name) do |threshold|
      MetricThresholdCondition.new(selection: self, comparison:, threshold:)
    end
  end

  # Creates a rule from a custom predicate over value and subject.
  # @return [MetricPredicateCondition]
  def should_satisfy(predicate)
    MetricPredicateCondition.new(selection: self, predicate:)
  end
end

#should_be_below(threshold) ⇒ MetricThresholdCondition



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 22

class MetricSelection
  THRESHOLD_METHODS = {
    should_be_below: :below,
    should_be_above: :above,
    should_be: :equal,
    should_be_below_or_equal: :below_or_equal,
    should_be_above_or_equal: :above_or_equal
  }.freeze

  attr_reader :scope, :metric

  def initialize(scope:, metric:)
    raise ArgumentError, 'scope must be a MetricsBuilder' unless scope.is_a?(MetricsBuilder)
    raise ArgumentError, 'metric must be a Metric' unless metric.is_a?(Calculation::Metric)

    @scope = scope
    @metric = metric
    freeze
  end

  # Evaluates this metric for every selected subject.
  # @param options [CheckOptions, nil] dependency-analysis options for coupling metrics
  # @return [Array<MetricMeasurement>]
  def measure(options = nil)
    options = Common::FluentApi::CheckOptions.resolve(options)
    scope.__send__(:subjects_for, metric.subject_type, options:).map do |subject|
      MetricMeasurement.new(
        subject:,
        metric_name: metric.name,
        value: metric.calculate(subject)
      )
    end.freeze
  end

  THRESHOLD_METHODS.each do |method_name, comparison|
    define_method(method_name) do |threshold|
      MetricThresholdCondition.new(selection: self, comparison:, threshold:)
    end
  end

  # Creates a rule from a custom predicate over value and subject.
  # @return [MetricPredicateCondition]
  def should_satisfy(predicate)
    MetricPredicateCondition.new(selection: self, predicate:)
  end
end

#should_be_below_or_equal(threshold) ⇒ MetricThresholdCondition



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 22

class MetricSelection
  THRESHOLD_METHODS = {
    should_be_below: :below,
    should_be_above: :above,
    should_be: :equal,
    should_be_below_or_equal: :below_or_equal,
    should_be_above_or_equal: :above_or_equal
  }.freeze

  attr_reader :scope, :metric

  def initialize(scope:, metric:)
    raise ArgumentError, 'scope must be a MetricsBuilder' unless scope.is_a?(MetricsBuilder)
    raise ArgumentError, 'metric must be a Metric' unless metric.is_a?(Calculation::Metric)

    @scope = scope
    @metric = metric
    freeze
  end

  # Evaluates this metric for every selected subject.
  # @param options [CheckOptions, nil] dependency-analysis options for coupling metrics
  # @return [Array<MetricMeasurement>]
  def measure(options = nil)
    options = Common::FluentApi::CheckOptions.resolve(options)
    scope.__send__(:subjects_for, metric.subject_type, options:).map do |subject|
      MetricMeasurement.new(
        subject:,
        metric_name: metric.name,
        value: metric.calculate(subject)
      )
    end.freeze
  end

  THRESHOLD_METHODS.each do |method_name, comparison|
    define_method(method_name) do |threshold|
      MetricThresholdCondition.new(selection: self, comparison:, threshold:)
    end
  end

  # Creates a rule from a custom predicate over value and subject.
  # @return [MetricPredicateCondition]
  def should_satisfy(predicate)
    MetricPredicateCondition.new(selection: self, predicate:)
  end
end

#should_satisfy(predicate) ⇒ MetricPredicateCondition

Creates a rule from a custom predicate over value and subject.



64
65
66
# File 'lib/archunit/metrics/fluentapi/metric_selection.rb', line 64

def should_satisfy(predicate)
  MetricPredicateCondition.new(selection: self, predicate:)
end