Class: ArchSpec::Rules::PublicApiRule

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

Overview

Backs ArchSpec::DSL::ComponentProxy#public_api. Flags references from outside the component to constants that are not part of its public API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, files: [], constants: nil, namespaces: nil) ⇒ PublicApiRule

Returns a new instance of PublicApiRule.



10
11
12
13
14
15
# File 'lib/archspec/rules/privacy_rule.rb', line 10

def initialize(source, files: [], constants: nil, namespaces: nil)
  @source = source.to_sym
  @file_patterns = Array(files).flatten.compact.map(&:to_s)
  @constants = Array(constants).compact.map { |value| normalize_constant(value) }
  @namespaces = Array(namespaces).compact.map { |value| normalize_constant(value) }
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



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

def constants
  @constants
end

#file_patternsObject (readonly)

Returns the value of attribute file_patterns.



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

def file_patterns
  @file_patterns
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



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

def namespaces
  @namespaces
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/archspec/rules/privacy_rule.rb', line 32

def evaluate(graph)
  component = graph.components[source]
  return [] unless component

  public_names = public_constant_names(graph)

  graph.dependency_edges.filter_map do |edge|
    next if component.files.include?(edge.from_path)

    resolved = graph.resolve_constant_reference(edge.to, edge.from_constant)
    next unless graph.component_names_for_constant(resolved).include?(source)
    next if public?(resolved, public_names)

    Diagnostic.new(
      rule: id,
      message: "#{resolved} is private to #{source}",
      location: edge.location,
      evidence: "#{edge.from_constant || edge.from_path} #{edge.type} #{resolved}"
    )
  end
end

#idObject



28
29
30
# File 'lib/archspec/rules/privacy_rule.rb', line 28

def id
  'dependencies.privacy'
end

#merge!(other) ⇒ Object



21
22
23
24
25
26
# File 'lib/archspec/rules/privacy_rule.rb', line 21

def merge!(other)
  @file_patterns |= other.file_patterns
  @constants |= other.constants
  @namespaces |= other.namespaces
  self
end

#merge_keyObject



17
18
19
# File 'lib/archspec/rules/privacy_rule.rb', line 17

def merge_key
  [self.class, source]
end