Class: ArchSpec::Rules::MustImplementOneOfRule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, method_names) ⇒ MustImplementOneOfRule

Returns a new instance of MustImplementOneOfRule.



83
84
85
86
# File 'lib/archspec/rules/protocol_rules.rb', line 83

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.



81
82
83
# File 'lib/archspec/rules/protocol_rules.rb', line 81

def method_names
  @method_names
end

#sourceObject (readonly)

Returns the value of attribute source.



81
82
83
# File 'lib/archspec/rules/protocol_rules.rb', line 81

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/archspec/rules/protocol_rules.rb', line 101

def evaluate(graph)
  constants_for(graph).filter_map do |constant|
    next if method_names.any? { |method_name| constant.instance_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: "#{constant.name} methods: #{constant.instance_methods.to_a.sort.join(', ')}"
    )
  end
end

#idObject



97
98
99
# File 'lib/archspec/rules/protocol_rules.rb', line 97

def id
  'protocol.must_implement_one_of'
end

#merge!(other) ⇒ Object



92
93
94
95
# File 'lib/archspec/rules/protocol_rules.rb', line 92

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

#merge_keyObject



88
89
90
# File 'lib/archspec/rules/protocol_rules.rb', line 88

def merge_key
  [self.class, source]
end