Class: ArchSpec::Rules::CannotCallRule

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

Overview

Backs ArchSpec::DSL::ComponentProxy#cannot_call. Flags calls to the named methods, optionally only bare implicit-+self+ calls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, methods, receiver: :any) ⇒ CannotCallRule

Returns a new instance of CannotCallRule.



10
11
12
13
14
15
16
17
18
# File 'lib/archspec/rules/protocol_rules.rb', line 10

def initialize(source, methods, receiver: :any)
  unless %i[any none].include?(receiver)
    raise Error, "cannot_call receiver: must be :any or :none, got #{receiver.inspect}"
  end

  @source = source.to_sym
  @method_names = Array(methods).flatten.map(&:to_sym)
  @receiver = receiver
end

Instance Attribute Details

#method_namesObject (readonly)

Returns the value of attribute method_names.



8
9
10
# File 'lib/archspec/rules/protocol_rules.rb', line 8

def method_names
  @method_names
end

#receiverObject (readonly)

Returns the value of attribute receiver.



8
9
10
# File 'lib/archspec/rules/protocol_rules.rb', line 8

def receiver
  @receiver
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/archspec/rules/protocol_rules.rb', line 8

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/archspec/rules/protocol_rules.rb', line 33

def evaluate(graph)
  graph.edges.filter_map do |edge|
    next unless edge.type == :calls_named_method
    next unless method_names.include?(edge.to.to_sym)
    next if receiver == :none && edge.receiver != :none
    next unless graph.component_names_for_path(edge.from_path).include?(source)
    next if own_method_call?(graph, edge)

    Diagnostic.new(
      rule: id,
      message: "#{source} must not call ##{edge.to}",
      location: edge.location,
      evidence: "#{edge.from_constant || edge.from_path} calls #{edge.to}"
    )
  end
end

#idObject



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

def id
  'methods.forbid'
end

#merge!(other) ⇒ Object



24
25
26
27
# File 'lib/archspec/rules/protocol_rules.rb', line 24

def merge!(other)
  @method_names |= other.method_names
  self
end

#merge_keyObject



20
21
22
# File 'lib/archspec/rules/protocol_rules.rb', line 20

def merge_key
  [self.class, source, receiver]
end