Class: RubocopPr::Rubocop

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubocop_pr/rubocop.rb

Overview

small helper for rubocop commands, everything should be stubbed in tests

Constant Summary collapse

TODO_FILENAME =
'.rubocop_todo.yml'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Rubocop

Returns a new instance of Rubocop.



10
11
12
13
# File 'lib/rubocop_pr/rubocop.rb', line 10

def initialize(**options)
  @branch = options.delete(:branch)
  @git = options.delete(:git)
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



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

def branch
  @branch
end

#gitObject (readonly)

Returns the value of attribute git.



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

def git
  @git
end

Instance Method Details

#corrected?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/rubocop_pr/rubocop.rb', line 42

def corrected?
  `bundle exec rubocop -a`
  git.checkout TODO_FILENAME
  !git.status.blank?
end

#eachObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop_pr/rubocop.rb', line 19

def each
  git.checkout(branch)
  todos_backup = todo
  todos_backup.keys.sort.reverse_each do |cop|
    git.checkout(branch)
    todos = todos_backup.dup
    todos.delete cop
    File.open(TODO_FILENAME, 'w') { |f| f.write todos.blank? ? '' : YAML.dump(todos) }
    yield cop
  end
end

#generate_todoObject



38
39
40
# File 'lib/rubocop_pr/rubocop.rb', line 38

def generate_todo
  `bundle exec rubocop --auto-gen-config`
end

#read_or_generate_todoObject



31
32
33
34
35
36
# File 'lib/rubocop_pr/rubocop.rb', line 31

def read_or_generate_todo
  return File.read(TODO_FILENAME) if File.exist?(TODO_FILENAME)
  generate_todo
  git.commit_all('Generate initial Rubocop todo file')
  File.read(TODO_FILENAME)
end

#todoObject



15
16
17
# File 'lib/rubocop_pr/rubocop.rb', line 15

def todo
  YAML.safe_load(read_or_generate_todo)
end