Class: RailsBestPractices::Core::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_best_practices/core/runner.rb

Overview

Runner is the main class, it can check source code of a filename with all checks (according to the configuration).

the check process is partitioned into two parts,

  1. prepare process, it will do some preparations for further checking, such as remember the model associations.

  2. review process, it does real check, if the source code violates some best practices, the violations will be notified.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

initialize the runner.

Parameters:

  • options (Hash) (defaults to: {})

    pass the prepares and reviews.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails_best_practices/core/runner.rb', line 45

def initialize(options = {})
  @config = self.class.config_path

  lexicals = Array(options[:lexicals])
  prepares = Array(options[:prepares])
  reviews = Array(options[:reviews])

  checks_loader = ChecksLoader.new(@config)
  @lexicals = lexicals.empty? ? checks_loader.load_lexicals : lexicals
  @prepares = prepares.empty? ? load_prepares : prepares
  @reviews = reviews.empty? ? checks_loader.load_reviews : reviews
  load_plugin_reviews if reviews.empty?

  @lexical_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @lexicals)
  @plain_prepare_checker ||=
    CodeAnalyzer::CheckingVisitor::Plain.new(
      checkers: @prepares.select { |checker| checker.is_a? Prepares::GemfilePrepare }
    )
  @default_prepare_checker ||=
    CodeAnalyzer::CheckingVisitor::Default.new(
      checkers: @prepares.reject { |checker| checker.is_a? Prepares::GemfilePrepare }
    )
  @review_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @reviews)

  @inlnie_disable ||= InlineDisables::InlineDisable.new
  @inline_disable_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: [@inlnie_disable])
end

Class Attribute Details

.base_pathString

get the base path, by default, the base path is current path.

Returns:

  • (String)

    the base path



29
30
31
# File 'lib/rails_best_practices/core/runner.rb', line 29

def base_path
  @base_path || '.'
end

.config_pathString

get the configuration path, if will default to config/rails_best_practices.yml

Returns:

  • (String)

    the config path



36
37
38
39
# File 'lib/rails_best_practices/core/runner.rb', line 36

def config_path
  custom_config = @config_path || File.join(Runner.base_path, 'config/rails_best_practices.yml')
  File.exist?(custom_config) ? custom_config : RailsBestPractices::Analyzer::DEFAULT_CONFIG
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



21
22
23
# File 'lib/rails_best_practices/core/runner.rb', line 21

def checks
  @checks
end

Instance Method Details

#after_inline_disableObject



121
122
123
# File 'lib/rails_best_practices/core/runner.rb', line 121

def after_inline_disable
  @inline_disable_checker.after_check
end

#after_lexicalObject



81
82
83
# File 'lib/rails_best_practices/core/runner.rb', line 81

def after_lexical
  @lexical_checker.after_check
end

#after_prepareObject



94
95
96
97
# File 'lib/rails_best_practices/core/runner.rb', line 94

def after_prepare
  @plain_prepare_checker.after_check
  @default_prepare_checker.after_check
end

#after_reviewObject



108
109
110
# File 'lib/rails_best_practices/core/runner.rb', line 108

def after_review
  @review_checker.after_check
end

#errorsArray

get all errors from lexicals and reviews.

Returns:

  • (Array)

    all errors from lexicals and reviews



128
129
130
131
132
133
# File 'lib/rails_best_practices/core/runner.rb', line 128

def errors
  @errors ||= begin
    reported_errors = (@reviews + @lexicals).collect(&:errors).flatten
    reported_errors.reject { |error| @inlnie_disable.disabled?(error) }
  end
end

#inline_disable(filename, content) ⇒ Object

disable check by inline comment the file.

Parameters:

  • filename (String)

    of the file

  • content (String)

    of the file



116
117
118
119
# File 'lib/rails_best_practices/core/runner.rb', line 116

def inline_disable(filename, content)
  content = parse_html_template(filename, content)
  @inline_disable_checker.check(filename, content)
end

#lexical(filename, content) ⇒ Object

lexical analysis the file.

Parameters:

  • filename (String)

    of the file

  • content (String)

    of the file



77
78
79
# File 'lib/rails_best_practices/core/runner.rb', line 77

def lexical(filename, content)
  @lexical_checker.check(filename, content)
end

#prepare(filename, content) ⇒ Object

prepare the file.

Parameters:

  • filename (String)

    of the file

  • content (String)

    of the file



89
90
91
92
# File 'lib/rails_best_practices/core/runner.rb', line 89

def prepare(filename, content)
  @plain_prepare_checker.check(filename, content)
  @default_prepare_checker.check(filename, content)
end

#review(filename, content) ⇒ Object

review the file.

Parameters:

  • filename (String)

    of the file

  • content (String)

    of the file



103
104
105
106
# File 'lib/rails_best_practices/core/runner.rb', line 103

def review(filename, content)
  content = parse_html_template(filename, content)
  @review_checker.check(filename, content)
end