Class: ArchSpec::Rules::AllowedConsumersRule

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

Overview

Backs ArchSpec::DSL::ComponentProxy#can_only_be_used_by. The inverse of an allowlist: flags references to the component from any component that is not an approved consumer. Use it to protect a shared kernel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, consumers) ⇒ AllowedConsumersRule

Returns a new instance of AllowedConsumersRule.



96
97
98
99
# File 'lib/archspec/rules/dependency_rules.rb', line 96

def initialize(source, consumers)
  @source = source.to_sym
  @consumers = Array(consumers).flatten.map(&:to_sym).to_set
end

Instance Attribute Details

#consumersObject (readonly)

Returns the value of attribute consumers.



94
95
96
# File 'lib/archspec/rules/dependency_rules.rb', line 94

def consumers
  @consumers
end

#sourceObject (readonly)

Returns the value of attribute source.



94
95
96
# File 'lib/archspec/rules/dependency_rules.rb', line 94

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/archspec/rules/dependency_rules.rb', line 114

def evaluate(graph)
  graph.dependency_edges.flat_map do |edge|
    next [] unless graph.target_components_for(edge).include?(source)

    offenders = graph.component_names_for_path(edge.from_path).reject do |component|
      component == source || consumers.include?(component)
    end

    offenders.map do |offender|
      Diagnostic.new(
        rule: id,
        message: message_for(offender),
        location: edge.location,
        evidence: "#{edge.from_constant || edge.from_path} #{edge.type} #{edge.to}"
      )
    end
  end
end

#idObject



110
111
112
# File 'lib/archspec/rules/dependency_rules.rb', line 110

def id
  'dependencies.consumers'
end

#merge!(other) ⇒ Object



105
106
107
108
# File 'lib/archspec/rules/dependency_rules.rb', line 105

def merge!(other)
  consumers.merge(other.consumers)
  self
end

#merge_keyObject



101
102
103
# File 'lib/archspec/rules/dependency_rules.rb', line 101

def merge_key
  [self.class, source]
end