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.



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

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.



79
80
81
# File 'lib/archspec/rules/protocol_rules.rb', line 79

def method_names
  @method_names
end

#sourceObject (readonly)

Returns the value of attribute source.



79
80
81
# File 'lib/archspec/rules/protocol_rules.rb', line 79

def source
  @source
end

Instance Method Details

#evaluate(graph) ⇒ Object



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

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



95
96
97
# File 'lib/archspec/rules/protocol_rules.rb', line 95

def id
  "protocol.must_implement_one_of"
end

#merge!(other) ⇒ Object



90
91
92
93
# File 'lib/archspec/rules/protocol_rules.rb', line 90

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

#merge_keyObject



86
87
88
# File 'lib/archspec/rules/protocol_rules.rb', line 86

def merge_key
  [self.class, source]
end