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.



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

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

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/archspec/rules/protocol_rules.rb', line 225

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

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

#idObject



221
222
223
# File 'lib/archspec/rules/protocol_rules.rb', line 221

def id
  'objects.instantiate_and_invoke_forbid'
end

#merge_keyObject



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

def merge_key
  [self.class, source]
end