module RuboCop::Cop::ConfigurableEnforcedStyle

Handles `EnforcedStyle` configuration parameters.

Public Instance Methods

alternative_style() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 74
def alternative_style
  if supported_styles.size != 2
    raise 'alternative_style can only be used when there are exactly '                 '2 SupportedStyles'
  end
  (supported_styles - [style]).first
end
ambiguous_style_detected(*possibilities) click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 19
def ambiguous_style_detected(*possibilities)
  style_detected(possibilities)
end
conflicting_styles_detected()
correct_style_detected() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 11
def correct_style_detected
  style_detected(style)
end
detected_style() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 48
def detected_style
  Formatter::DisabledConfigFormatter.detected_styles[cop_name] ||= nil
end
detected_style=(style) click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 52
def detected_style=(style)
  Formatter::DisabledConfigFormatter.detected_styles[cop_name] = style

  return no_acceptable_style! if style.nil?
  return no_acceptable_style! if style.empty?

  config_to_allow_offenses[parameter_name] = style.first
end
no_acceptable_style!() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 43
def no_acceptable_style!
  self.config_to_allow_offenses = { 'Enabled' => false }
  Formatter::DisabledConfigFormatter.detected_styles[cop_name] = []
end
no_acceptable_style?() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 39
def no_acceptable_style?
  config_to_allow_offenses['Enabled'] == false
end
opposite_style_detected() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 7
def opposite_style_detected
  style_detected(alternative_style)
end
parameter_name() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 86
def parameter_name
  'EnforcedStyle'
end
style() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 64
def style
  @enforced_style ||= begin
    s = cop_config[parameter_name].to_sym
    unless supported_styles.include?(s)
      raise "Unknown style #{s} selected!"
    end
    s
  end
end
style_detected(detected) click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 23
def style_detected(detected)
  return if no_acceptable_style?

  # `detected` can be a single style, or an Array of possible styles
  # (if there is more than one which matches the observed code)
  detected_as_strings = Array(detected).map(&:to_s)

  if !detected_style # we haven't observed any specific style yet
    self.detected_style = detected_as_strings
  elsif detected_style.is_a?(Array)
    self.detected_style &= detected_as_strings
  elsif !detected.include?(detected_style)
    no_acceptable_style!
  end
end
supported_styles() click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 82
def supported_styles
  @supported_styles ||= cop_config['SupportedStyles'].map(&:to_sym)
end
unexpected_style_detected(unexpected) click to toggle source
# File lib/rubocop/cop/mixin/configurable_enforced_style.rb, line 15
def unexpected_style_detected(unexpected)
  style_detected(unexpected)
end
unrecognized_style_detected()