Class: ArchSpec::Rules::MustImplementOneOfRule

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

Overview

Backs ArchSpec::DSL::ComponentProxy#must_implement_one_of. Flags classes that implement none of the named methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, method_names) ⇒ MustImplementOneOfRule

Returns a new instance of MustImplementOneOfRule.



108
109
110
111
# File 'lib/archspec/rules/protocol_rules.rb', line 108

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

Instance Attribute Details

#method_namesObject (readonly)

Returns the value of attribute method_names.



106
107
108
# File 'lib/archspec/rules/protocol_rules.rb', line 106

def method_names
  @method_names
end

#sourceObject (readonly)

Returns the value of attribute source.



106
107
108
# File 'lib/archspec/rules/protocol_rules.rb', line 106

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/archspec/rules/protocol_rules.rb', line 126

def evaluate(graph)
  constants_for(graph).filter_map do |constant|
    methods, unresolved = graph.effective_instance_methods(constant.name)
    next if method_names.any? { |method_name| methods.include?(method_name) }

    Diagnostic.new(
      rule: id,
      message: "#{constant.name} must implement one of #{method_names.map { |name| "##{name}" }.join(', ')}",
      location: constant.location,
      evidence: ProtocolEvidence.for(constant, methods, unresolved),
      confidence: unresolved.empty? ? :high : :medium
    )
  end
end

#idObject



122
123
124
# File 'lib/archspec/rules/protocol_rules.rb', line 122

def id
  'protocol.must_implement_one_of'
end

#merge!(other) ⇒ Object



117
118
119
120
# File 'lib/archspec/rules/protocol_rules.rb', line 117

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

#merge_keyObject



113
114
115
# File 'lib/archspec/rules/protocol_rules.rb', line 113

def merge_key
  [self.class, source]
end