Module: RuboCop::Cop::ConfigurableNaming

Includes:
ConfigurableEnforcedStyle
Included in:
Style::MethodName, Style::VariableName
Defined in:
lib/rubocop/cop/mixin/configurable_naming.rb

Overview

This module provides functionality for checking if names match the configured EnforcedStyle.

Constant Summary collapse

SNAKE_CASE =
/^@{0,2}[\da-z_]+[!?=]?$/
CAMEL_CASE =
/^@{0,2}[a-z][\da-zA-Z]+[!?=]?$/

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #both_styles_detected, #correct_style_detected, #opposite_style_detected, #parameter_name, #style, #unrecognized_style_detected

Instance Method Details

#check_name(node, name, name_range) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/mixin/configurable_naming.rb', line 13

def check_name(node, name, name_range)
  return if operator?(name)

  if valid_name?(name)
    correct_style_detected
  else
    add_offense(node, name_range, message(style)) do
      opposite_style_detected
    end
  end
end

#valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/rubocop/cop/mixin/configurable_naming.rb', line 25

def valid_name?(name)
  pattern = (style == :snake_case ? SNAKE_CASE : CAMEL_CASE)
  name.match(pattern)
end