Class: ArchSpec::Rules::CannotReferenceConstantsRule

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

Overview

Backs ArchSpec::DSL::ComponentProxy#cannot_reference_constants. Flags references to the named constants or anything nested under them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, constants) ⇒ CannotReferenceConstantsRule

Returns a new instance of CannotReferenceConstantsRule.



147
148
149
150
# File 'lib/archspec/rules/dependency_rules.rb', line 147

def initialize(source, constants)
  @source = source.to_sym
  @constants = Array(constants).flatten.map { |constant| constant.to_s.sub(/\A::/, '') }
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



145
146
147
# File 'lib/archspec/rules/dependency_rules.rb', line 145

def constants
  @constants
end

#sourceObject (readonly)

Returns the value of attribute source.



145
146
147
# File 'lib/archspec/rules/dependency_rules.rb', line 145

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/archspec/rules/dependency_rules.rb', line 165

def evaluate(graph)
  graph.dependency_edges.filter_map do |edge|
    next unless graph.component_names_for_path(edge.from_path).include?(source)

    referenced = graph.resolve_constant_reference(edge.to, edge.from_constant)
    next unless constants.any? { |constant| referenced == constant || referenced.start_with?("#{constant}::") }

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

#idObject



161
162
163
# File 'lib/archspec/rules/dependency_rules.rb', line 161

def id
  'constants.forbid'
end

#merge!(other) ⇒ Object



156
157
158
159
# File 'lib/archspec/rules/dependency_rules.rb', line 156

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

#merge_keyObject



152
153
154
# File 'lib/archspec/rules/dependency_rules.rb', line 152

def merge_key
  [self.class, source]
end