Class: ArchSpec::Rules::CannotDefineMethodRule

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

Overview

Backs ArchSpec::DSL::ComponentProxy#cannot_define. Flags method definitions in the component matching the named methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, methods) ⇒ CannotDefineMethodRule

Returns a new instance of CannotDefineMethodRule.



176
177
178
179
# File 'lib/archspec/rules/protocol_rules.rb', line 176

def initialize(source, methods)
  @source = source.to_sym
  @method_names = Array(methods).flatten.map(&:to_sym)
end

Instance Attribute Details

#method_namesObject (readonly)

Returns the value of attribute method_names.



174
175
176
# File 'lib/archspec/rules/protocol_rules.rb', line 174

def method_names
  @method_names
end

#sourceObject (readonly)

Returns the value of attribute source.



174
175
176
# File 'lib/archspec/rules/protocol_rules.rb', line 174

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/archspec/rules/protocol_rules.rb', line 194

def evaluate(graph)
  graph.method_definitions_for_component(source).filter_map do |method_definition|
    next unless method_names.include?(method_definition.name)

    Diagnostic.new(
      rule: id,
      message: "#{source} must not define ##{method_definition.name}",
      location: method_definition.location,
      evidence: "#{method_definition.owner} defines #{method_definition.scope} method #{method_definition.name}"
    )
  end
end

#idObject



190
191
192
# File 'lib/archspec/rules/protocol_rules.rb', line 190

def id
  'methods.define_forbid'
end

#merge!(other) ⇒ Object



185
186
187
188
# File 'lib/archspec/rules/protocol_rules.rb', line 185

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

#merge_keyObject



181
182
183
# File 'lib/archspec/rules/protocol_rules.rb', line 181

def merge_key
  [self.class, source]
end