class RuboCop::Cop::Naming::PredicateName
This cop makes sure that predicates are named properly.
@example
# bad def is_even?(value) ... # good def even?(value) # bad def has_value? ... # good def value? ...
Public Instance Methods
on_def(node)
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 41 def on_def(node) predicate_prefixes.each do |prefix| method_name = node.method_name.to_s next if allowed_method_name?(method_name, prefix) add_offense( node, location: :name, message: message(method_name, expected_name(method_name, prefix)) ) end end
Also aliased as: on_defs
on_send(node)
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 26 def on_send(node) dynamic_method_define(node) do |method_name| predicate_prefixes.each do |prefix| next if allowed_method_name?(method_name.to_s, prefix) add_offense( node, location: node.first_argument.loc.expression, message: message(method_name, expected_name(method_name.to_s, prefix)) ) end end end
Private Instance Methods
allowed_method_name?(method_name, prefix)
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 58 def allowed_method_name?(method_name, prefix) !method_name.start_with?(prefix) || method_name == expected_name(method_name, prefix) || predicate_whitelist.include?(method_name) end
expected_name(method_name, prefix)
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 64 def expected_name(method_name, prefix) new_name = if prefix_blacklist.include?(prefix) method_name.sub(prefix, '') else method_name.dup end new_name << '?' unless method_name.end_with?('?') new_name end
message(method_name, new_name)
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 74 def message(method_name, new_name) "Rename `#{method_name}` to `#{new_name}`." end
method_definition_macros(macro_name)
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 90 def method_definition_macros(macro_name) cop_config['MethodDefinitionMacros'].include?(macro_name.to_s) end
predicate_prefixes()
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 82 def predicate_prefixes cop_config['NamePrefix'] end
predicate_whitelist()
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 86 def predicate_whitelist cop_config['NameWhitelist'] end
prefix_blacklist()
click to toggle source
# File lib/rubocop/cop/naming/predicate_name.rb, line 78 def prefix_blacklist cop_config['NamePrefixBlacklist'] end