Class: ArchSpec::Rules::CannotReferenceConstantsRule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, constants) ⇒ CannotReferenceConstantsRule

Returns a new instance of CannotReferenceConstantsRule.



82
83
84
85
# File 'lib/archspec/rules/dependency_rules.rb', line 82

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.



80
81
82
# File 'lib/archspec/rules/dependency_rules.rb', line 80

def constants
  @constants
end

#sourceObject (readonly)

Returns the value of attribute source.



80
81
82
# File 'lib/archspec/rules/dependency_rules.rb', line 80

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/archspec/rules/dependency_rules.rb', line 100

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



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

def id
  "constants.forbid"
end

#merge!(other) ⇒ Object



91
92
93
94
# File 'lib/archspec/rules/dependency_rules.rb', line 91

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

#merge_keyObject



87
88
89
# File 'lib/archspec/rules/dependency_rules.rb', line 87

def merge_key
  [self.class, source]
end