Class: ArchSpec::Rules::CannotReferenceConstantsRule
- Inherits:
-
Object
- Object
- ArchSpec::Rules::CannotReferenceConstantsRule
- Defined in:
- lib/archspec/rules/dependency_rules.rb
Instance Attribute Summary collapse
-
#constants ⇒ Object
readonly
Returns the value of attribute constants.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #evaluate(graph) ⇒ Object
- #id ⇒ Object
-
#initialize(source, constants) ⇒ CannotReferenceConstantsRule
constructor
A new instance of CannotReferenceConstantsRule.
- #merge!(other) ⇒ Object
- #merge_key ⇒ Object
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
#constants ⇒ Object (readonly)
Returns the value of attribute constants.
80 81 82 |
# File 'lib/archspec/rules/dependency_rules.rb', line 80 def constants @constants end |
#source ⇒ Object (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 |
#id ⇒ Object
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_key ⇒ Object
87 88 89 |
# File 'lib/archspec/rules/dependency_rules.rb', line 87 def merge_key [self.class, source] end |