Class: Rubomop::Mop

Inherits:
Object
  • Object
show all
Defined in:
lib/rubomop/mop.rb

Defined Under Namespace

Classes: DeleteOption

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(todo_file, number, autocorrect_only, verbose, run_rubocop, only, except, blocklist) ⇒ Mop

Returns a new instance of Mop.



6
7
8
9
10
11
12
13
14
15
# File 'lib/rubomop/mop.rb', line 6

def initialize(todo_file, number, autocorrect_only, verbose, run_rubocop, only, except, blocklist)
  @todo_file = todo_file
  @number = number
  @autocorrect_only = autocorrect_only
  @verbose = verbose
  @run_rubocop = run_rubocop
  @only = only
  @except = except
  @block = blocklist
end

Instance Attribute Details

#autocorrect_onlyObject

Returns the value of attribute autocorrect_only.



3
4
5
# File 'lib/rubomop/mop.rb', line 3

def autocorrect_only
  @autocorrect_only
end

#blockObject

Returns the value of attribute block.



4
5
6
# File 'lib/rubomop/mop.rb', line 4

def block
  @block
end

#exceptObject

Returns the value of attribute except.



4
5
6
# File 'lib/rubomop/mop.rb', line 4

def except
  @except
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/rubomop/mop.rb', line 3

def number
  @number
end

#onlyObject

Returns the value of attribute only.



4
5
6
# File 'lib/rubomop/mop.rb', line 4

def only
  @only
end

#run_rubocopObject

Returns the value of attribute run_rubocop.



3
4
5
# File 'lib/rubomop/mop.rb', line 3

def run_rubocop
  @run_rubocop
end

#todo_fileObject

Returns the value of attribute todo_file.



3
4
5
# File 'lib/rubomop/mop.rb', line 3

def todo_file
  @todo_file
end

#verboseObject

Returns the value of attribute verbose.



4
5
6
# File 'lib/rubomop/mop.rb', line 4

def verbose
  @verbose
end

Instance Method Details

#accept?(delete_option) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubomop/mop.rb', line 21

def accept?(delete_option)
  return false if autocorrect_only && !delete_option.cop.autocorrect
  unless except.empty?
    return except.none? { delete_option.cop.name.include?(_1) }
  end
  unless block.empty?
    return block.none? { delete_option.file.include?(_1) }
  end
  unless only.empty?
    return only.any? { delete_option.cop.name.include?(_1) }
  end
  true
  # return true unless autocorrect_only
  # cop.autocorrect
end

#copsObject



17
18
19
# File 'lib/rubomop/mop.rb', line 17

def cops
  todo_file.cops
end

#delete_optionsObject



37
38
39
40
# File 'lib/rubomop/mop.rb', line 37

def delete_options
  cops.flat_map { delete_options_for(_1) }
    .select { accept?(_1) }
end

#delete_options_for(cop) ⇒ Object



42
43
44
# File 'lib/rubomop/mop.rb', line 42

def delete_options_for(cop)
  cop.files.map { DeleteOption.new(cop, _1, verbose, run_rubocop) }
end

#log(message) ⇒ Object



46
47
48
49
# File 'lib/rubomop/mop.rb', line 46

def log(message)
  return unless verbose
  print message
end

#mop!Object



51
52
53
54
55
56
57
58
59
# File 'lib/rubomop/mop.rb', line 51

def mop!
  number.times do |i|
    options = delete_options
    next if options.empty?
    log("#{i + 1}:")
    mop_once!(options.sample)
    log("\n")
  end
end

#mop_once!(delete_option) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rubomop/mop.rb', line 61

def mop_once!(delete_option)
  delete_option.print_message if verbose
  delete_option.delete!
  return unless run_rubocop
  todo_file.save!
  delete_option.rubocop_runner
end