Class: RubocopChallenger::Rubocop::TodoReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop_challenger/rubocop/todo_reader.rb

Overview

To read ‘.rubocop_todo.yml` and parse each rules

Instance Method Summary collapse

Constructor Details

#initialize(rubocop_todo_file_path) ⇒ TodoReader

Returns a new instance of TodoReader.



7
8
9
# File 'lib/rubocop_challenger/rubocop/todo_reader.rb', line 7

def initialize(rubocop_todo_file_path)
  @rubocop_todo_file_path = rubocop_todo_file_path
end

Instance Method Details

#all_rulesArray<Rule>

Returns Array of rubocop rule instances which ordered by offense count.

Returns:

  • (Array<Rule>)

    Array of rubocop rule instances which ordered by offense count



21
22
23
24
25
26
27
28
29
# File 'lib/rubocop_challenger/rubocop/todo_reader.rb', line 21

def all_rules
  @all_rules ||=
    file_contents
    .split(/\n{2,}/)
    .drop(1) # remove header contents
    .map { |content| Rule.new(content) }
    .reject { |rule| ignored?(rule) }
    .sort
end

#any_ruleRule

Returns:



47
48
49
# File 'lib/rubocop_challenger/rubocop/todo_reader.rb', line 47

def any_rule
  autocorrectable_rules.sample
end

#autocorrectable_rulesArray<Rule>

Returns:



32
33
34
# File 'lib/rubocop_challenger/rubocop/todo_reader.rb', line 32

def autocorrectable_rules
  all_rules.select(&:autocorrectable?)
end

#least_occurrence_ruleRule

Returns:



37
38
39
# File 'lib/rubocop_challenger/rubocop/todo_reader.rb', line 37

def least_occurrence_rule
  autocorrectable_rules.first
end

#most_occurrence_ruleRule

Returns:



42
43
44
# File 'lib/rubocop_challenger/rubocop/todo_reader.rb', line 42

def most_occurrence_rule
  autocorrectable_rules.last
end

#versionType

Returns the version of RuboCop used to create the “.rubocop_todo.yml”

Returns:

  • (Type)

    the RuboCop version



14
15
16
17
# File 'lib/rubocop_challenger/rubocop/todo_reader.rb', line 14

def version
  file_contents =~ /using RuboCop version (\d{1,}\.\d{1,}\.\d{1,})/
  Regexp.last_match(1)
end