Class: Pronto::RailsBestPractices

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/rails_best_practices.rb

Instance Method Summary collapse

Instance Method Details

#messages_for(patches, errors) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pronto/rails_best_practices.rb', line 25

def messages_for(patches, errors)
  errors.map do |error|
    patch = patch_for_error(patches, error)

    if patch
      line = patch.added_lines.find do |added_line|
        added_line.new_lineno == error.line_number.to_i
      end

      new_message(line, error) if line
    end
  end
end

#new_message(line, error) ⇒ Object



39
40
41
42
# File 'lib/pronto/rails_best_practices.rb', line 39

def new_message(line, error)
  Message.new(line.patch.delta.new_file[:path], line, :warning,
              error.message.capitalize, nil, self.class)
end

#patch_for_error(patches, error) ⇒ Object



44
45
46
47
48
# File 'lib/pronto/rails_best_practices.rb', line 44

def patch_for_error(patches, error)
  patches.find do |patch|
    patch.new_file_full_path.to_s == error.filename
  end
end

#runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pronto/rails_best_practices.rb', line 6

def run
  return [] unless @patches

  patches_with_additions = @patches.select { |patch| patch.additions > 0 }

  files = patches_with_additions.map do |patch|
    Regexp.new(patch.new_file_full_path.to_s)
  end

  if files.any?
    analyzer = ::RailsBestPractices::Analyzer.new('.', 'silent' => true,
                                                       'only' => files)
    analyzer.analyze
    messages_for(patches_with_additions, analyzer.errors).compact
  else
    []
  end
end