Class: ArchSpec::Rules::NamingRule

Inherits:
Object
  • Object
show all
Defined in:
lib/archspec/rules/naming_rules.rb

Overview

Backs the method_names.matching(...) DSL. One rule pairs a selector (which methods it judges) with a constraint (what must hold of them). It judges a component's defined, public methods in the requested scope, or every such method in the project when source is nil. Every finding is name-based and exact, so confidence is always :high.

Constant Summary collapse

VALID_SCOPES =
%i[instance class].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, selector:, constraint:, scope: :instance, except: []) ⇒ NamingRule

Returns a new instance of NamingRule.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/archspec/rules/naming_rules.rb', line 17

def initialize(source:, selector:, constraint:, scope: :instance, except: [])
  unless VALID_SCOPES.include?(scope)
    raise Error, "method_names scope: must be :instance or :class, got #{scope.inspect}"
  end

  @source = source&.to_sym
  @selector = selector
  @constraint = constraint
  @scope = scope
  @except = Array(except).flatten.map(&:to_sym).to_set
end

Instance Attribute Details

#exceptObject (readonly)

Returns the value of attribute except.



15
16
17
# File 'lib/archspec/rules/naming_rules.rb', line 15

def except
  @except
end

#scopeObject (readonly)

Returns the value of attribute scope.



15
16
17
# File 'lib/archspec/rules/naming_rules.rb', line 15

def scope
  @scope
end

#selectorObject (readonly)

Returns the value of attribute selector.



15
16
17
# File 'lib/archspec/rules/naming_rules.rb', line 15

def selector
  @selector
end

#sourceObject (readonly)

Returns the value of attribute source.



15
16
17
# File 'lib/archspec/rules/naming_rules.rb', line 15

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/archspec/rules/naming_rules.rb', line 33

def evaluate(graph)
  selected = candidate_methods(graph).filter_map do |definition|
    next if except.include?(definition.name)

    match = selector.match(definition)
    [definition, match] if match
  end

  @constraint.diagnostics(selected, self, graph)
end

#idObject



29
30
31
# File 'lib/archspec/rules/naming_rules.rb', line 29

def id
  @constraint.id
end