Module: RuboCop::ExcludeLimit

Included in:
Cop::Base, Cop::CodeLength, Cop::MethodComplexity, Cop::Style::NumberedParametersLimit
Defined in:
lib/rubocop/cop/exclude_limit.rb

Overview

Allows specified configuration options to have an exclude limit ie. a maximum value tracked that it can be used by ‘–auto-gen-config`.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.tmp_dirObject

Returns the value of attribute tmp_dir.



8
9
10
# File 'lib/rubocop/cop/exclude_limit.rb', line 8

def tmp_dir
  @tmp_dir
end

Class Method Details

.cop_dir_for(cop_name) ⇒ Object

Returns the tmp directory path for a given cop, or nil if tmp_dir is not set.



27
28
29
# File 'lib/rubocop/cop/exclude_limit.rb', line 27

def cop_dir_for(cop_name)
  tmp_dir&.join(cop_name.tr('/', '-'))
end

.read_limits(cop_name) ⇒ Object

Reads the aggregated exclude limit values for a cop from tmp files. Returns a hash like { ‘Max’ => 81 } or an empty hash if no values were written.



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

def read_limits(cop_name)
  cop_dir = cop_dir_for(cop_name)
  return {} unless cop_dir&.directory?

  limits = {}
  cop_dir.children.each do |filepath|
    next unless filepath.file?

    values = filepath.readlines.map(&:to_i)
    limits[filepath.basename.to_s] = values.max unless values.empty?
  end
  limits
end

Instance Method Details

#exclude_limit(parameter_name, method_name: transform(parameter_name)) ⇒ Object

Sets up a configuration option to have an exclude limit tracked. The parameter name given is transformed into a method name (eg. ‘Max` becomes `self.max=` and `MinDigits` becomes `self.min_digits=`).



35
36
37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/exclude_limit.rb', line 35

def exclude_limit(parameter_name, method_name: transform(parameter_name))
  define_method(:"#{method_name}=") do |value|
    cop_dir = RuboCop::ExcludeLimit.cop_dir_for(self.class.badge.to_s)
    return unless cop_dir

    cop_dir.mkpath
    filepath = cop_dir.join(parameter_name)
    filepath.write("#{value}\n", mode: 'a')
  end
end