Class: BestPracticeProject::RubocopHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/best_practice_project/rubocop_handler.rb

Instance Method Summary collapse

Methods inherited from BaseHandler

#rails?

Constructor Details

#initialize(args) ⇒ RubocopHandler

Returns a new instance of RubocopHandler.



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/best_practice_project/rubocop_handler.rb', line 2

def initialize(args)
  super

  @actual_config_path = File.realpath("#{File.dirname(__FILE__)}/config/rubocop.yml")

  if rails?
    @config_path = Rails.root.join(".rubocop.yml").to_s
    @todo_path = Rails.root.join(".rubocop_todo.yml").to_s
  else
    @config_path = ".rubocop.yml"
    @todo_path = ".rubocop_todo.yml"
  end
end

Instance Method Details

#commandObject



16
17
18
19
20
21
22
# File 'lib/best_practice_project/rubocop_handler.rb', line 16

def command
  command = "bundle exec rubocop --display-cop-names"
  command << " --rails" if rails?
  command << " --auto-correct" if ARGV.include?("auto-correct")

  command
end

#executeObject



40
41
42
# File 'lib/best_practice_project/rubocop_handler.rb', line 40

def execute
  system(command)
end

#generate_configObject



24
25
26
27
28
29
30
31
# File 'lib/best_practice_project/rubocop_handler.rb', line 24

def generate_config
  FileUtils.copy(@actual_config_path, @config_path)

  generate_todo_config

  puts "Generated Rubocop todo config in #{@todo_path}"
  puts "Generated Rubocop config in  #{@config_path}"
end

#generate_todo_configObject



33
34
35
36
37
38
# File 'lib/best_practice_project/rubocop_handler.rb', line 33

def generate_todo_config
  rubocop_command = "rubocop --display-cop-names --auto-gen-config --config=#{@config_path}"
  rubocop_command << " --rails" if rails?

  system(rubocop_command)
end

#installed?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/best_practice_project/rubocop_handler.rb', line 44

def installed?
  require "rubocop"
  true
rescue LoadError
  false
end