Class: ArchUnit::Metrics::Calculation::Metric

Inherits:
Data
  • Object
show all
Defined in:
lib/archunit/metrics/calculation/metric.rb

Overview

Immutable named calculation over one extracted subject type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, subject_type:, calculation:, description: nil) ⇒ Metric

Returns a new instance of Metric.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/archunit/metrics/calculation/metric.rb', line 10

def initialize(name:, subject_type:, calculation:, description: nil)
  name = immutable_name(name)
  raise ArgumentError, 'subject_type must be a Class' unless subject_type.is_a?(Class)
  unless calculation.respond_to?(:call)
    raise ArgumentError, 'calculation must respond to call'
  end

  calculation = calculation.dup.freeze
  description = immutable_description(description)
  super
end

Instance Attribute Details

#calculationObject (readonly)

Returns the value of attribute calculation

Returns:

  • (Object)

    the current value of calculation



9
10
11
# File 'lib/archunit/metrics/calculation/metric.rb', line 9

def calculation
  @calculation
end

#descriptionObject (readonly)

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



9
10
11
# File 'lib/archunit/metrics/calculation/metric.rb', line 9

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



9
10
11
# File 'lib/archunit/metrics/calculation/metric.rb', line 9

def name
  @name
end

#subject_typeObject (readonly)

Returns the value of attribute subject_type

Returns:

  • (Object)

    the current value of subject_type



9
10
11
# File 'lib/archunit/metrics/calculation/metric.rb', line 9

def subject_type
  @subject_type
end

Instance Method Details

#calculate(subject) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/archunit/metrics/calculation/metric.rb', line 22

def calculate(subject)
  unless subject.is_a?(subject_type)
    raise ArgumentError, "subject must be a #{subject_type.name.split('::').last}"
  end

  NumericValue.validate(
    calculation.call(subject),
    attribute: 'metric calculations',
    error_class: TypeError
  )
end