class RuboCop::Cop::Style::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? ...

Private Instance Methods

expected_name(method_name, prefix) click to toggle source
# File lib/rubocop/cop/style/predicate_name.rb, line 39
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/style/predicate_name.rb, line 49
def message(method_name, new_name)
  "Rename `#{method_name}` to `#{new_name}`."
end
on_method_def(node, method_name, _args, _body) click to toggle source
# File lib/rubocop/cop/style/predicate_name.rb, line 25
def on_method_def(node, method_name, _args, _body)
  predicate_prefixes.each do |prefix|
    method_name = method_name.to_s
    next unless method_name.start_with?(prefix)
    next if method_name == expected_name(method_name, prefix)
    next if predicate_whitelist.include?(method_name)
    add_offense(
      node,
      :name,
      message(method_name, expected_name(method_name, prefix))
    )
  end
end
predicate_prefixes() click to toggle source
# File lib/rubocop/cop/style/predicate_name.rb, line 57
def predicate_prefixes
  cop_config['NamePrefix']
end
predicate_whitelist() click to toggle source
# File lib/rubocop/cop/style/predicate_name.rb, line 61
def predicate_whitelist
  cop_config['NameWhitelist']
end
prefix_blacklist() click to toggle source
# File lib/rubocop/cop/style/predicate_name.rb, line 53
def prefix_blacklist
  cop_config['NamePrefixBlacklist']
end