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.



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

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.



171
172
173
# File 'lib/archspec/rules/protocol_rules.rb', line 171

def method_names
  @method_names
end

#sourceObject (readonly)

Returns the value of attribute source.



171
172
173
# File 'lib/archspec/rules/protocol_rules.rb', line 171

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/archspec/rules/protocol_rules.rb', line 191

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



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

def id
  'methods.define_forbid'
end

#merge!(other) ⇒ Object



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

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

#merge_keyObject



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

def merge_key
  [self.class, source]
end