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.



139
140
141
142
# File 'lib/archspec/rules/dependency_rules.rb', line 139

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.



137
138
139
# File 'lib/archspec/rules/dependency_rules.rb', line 137

def constants
  @constants
end

#sourceObject (readonly)

Returns the value of attribute source.



137
138
139
# File 'lib/archspec/rules/dependency_rules.rb', line 137

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/archspec/rules/dependency_rules.rb', line 157

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

    referenced = graph.resolve_edge_constant(edge)
    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: "#{graph.edge_source_name(edge)} #{edge.verb} #{edge.to}"
    )
  end
end

#idObject



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

def id
  'constants.forbid'
end

#merge!(other) ⇒ Object



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

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

#merge_keyObject



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

def merge_key
  [self.class, source]
end