Class: ArchSpec::Rules::CannotInstantiateAndInvokeRule

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

Overview

Backs ArchSpec::DSL::ComponentProxy#cannot_instantiate_and_invoke. Flags the one-shot Thing.new(...).call pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ CannotInstantiateAndInvokeRule

Returns a new instance of CannotInstantiateAndInvokeRule.



210
211
212
# File 'lib/archspec/rules/protocol_rules.rb', line 210

def initialize(source)
  @source = source.to_sym
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



208
209
210
# File 'lib/archspec/rules/protocol_rules.rb', line 208

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/archspec/rules/protocol_rules.rb', line 222

def evaluate(graph)
  graph.edges.filter_map do |edge|
    next unless edge.type == :instantiates_and_invokes
    next unless graph.component_names_for_path(edge.from_path).include?(source)

    Diagnostic.new(
      rule: id,
      message: "#{source} must not instantiate and immediately invoke #{edge.to}",
      location: edge.location,
      evidence: "#{edge.from_constant || edge.from_path} uses #{edge.to}"
    )
  end
end

#idObject



218
219
220
# File 'lib/archspec/rules/protocol_rules.rb', line 218

def id
  'objects.instantiate_and_invoke_forbid'
end

#merge_keyObject



214
215
216
# File 'lib/archspec/rules/protocol_rules.rb', line 214

def merge_key
  [self.class, source]
end