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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Rubocop

Returns a new instance of Rubocop.



20
21
22
23
# File 'lib/rubocop_pr/rubocop.rb', line 20

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

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



18
19
20
# File 'lib/rubocop_pr/rubocop.rb', line 18

def branch
  @branch
end

#gitObject (readonly)

Returns the value of attribute git.



18
19
20
# File 'lib/rubocop_pr/rubocop.rb', line 18

def git
  @git
end

Class Method Details

.correct!(all) ⇒ Object



13
14
15
# File 'lib/rubocop_pr/rubocop.rb', line 13

def correct!(all)
  system "bundle exec rubocop -#{all ? 'A' : 'a'}"
end

.generate_todoObject



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

def generate_todo
  system 'bundle exec rubocop --auto-gen-config'
end

Instance Method Details

#eachObject



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

def each
  todo.keys.sort.reverse_each do |cop|
    git.checkout(branch)
    File.open(TODO_FILENAME, 'w') do |f|
      f.write todo.except(cop.to_s).blank? ? '' : YAML.dump(todo.except(cop.to_s))
    end
    yield Cop.new(name: cop)
  end
end

#read_or_generate_todoObject



39
40
41
42
43
44
45
# File 'lib/rubocop_pr/rubocop.rb', line 39

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

#todoObject



25
26
27
# File 'lib/rubocop_pr/rubocop.rb', line 25

def todo
  @todo ||= YAML.safe_load(read_or_generate_todo)
end