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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

initialize the runner.

Parameters:

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

    pass the prepares and reviews.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rails_best_practices/core/runner.rb', line 37

def initialize(options={})
  custom_config = File.join(Runner.base_path, 'config/rails_best_practices.yml')
  @config = File.exists?(custom_config) ? custom_config : RailsBestPractices::Analyzer::DEFAULT_CONFIG

  lexicals = Array(options[:lexicals])
  prepares = Array(options[:prepares])
  reviews = Array(options[:reviews])
  @lexicals = lexicals.empty? ? load_lexicals : lexicals
  @prepares = prepares.empty? ? load_prepares : prepares
  @reviews = reviews.empty? ? load_reviews : reviews

  load_plugin_reviews if reviews.empty?

  @checker ||= CheckingVisitor.new(:prepares => @prepares, :reviews => @reviews, :lexicals => @lexicals)
  @debug = false
  @whiny = false
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



17
18
19
# File 'lib/rails_best_practices/core/runner.rb', line 17

def checks
  @checks
end

#colorObject

Returns the value of attribute color.



18
19
20
# File 'lib/rails_best_practices/core/runner.rb', line 18

def color
  @color
end

#debugObject

Returns the value of attribute debug.



18
19
20
# File 'lib/rails_best_practices/core/runner.rb', line 18

def debug
  @debug
end

#whinyObject

Returns the value of attribute whiny.



18
19
20
# File 'lib/rails_best_practices/core/runner.rb', line 18

def whiny
  @whiny
end

Class Method Details

.base_pathString

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

Returns:

  • (String)

    the base path



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

def self.base_path
  @base_path || "."
end

.base_path=(path) ⇒ Object

set the base path.

Parameters:

  • path (String)

    the base path



23
24
25
# File 'lib/rails_best_practices/core/runner.rb', line 23

def self.base_path=(path)
  @base_path = path
end

Instance Method Details

#after_lexicalObject

After lexical method.



127
# File 'lib/rails_best_practices/core/runner.rb', line 127

def after_lexical; end

#after_prepareObject

provide a handler after all files reviewed.



130
131
132
133
134
135
136
# File 'lib/rails_best_practices/core/runner.rb', line 130

def after_prepare
  filename = "rails_best_practices.after_prepare"
  content = "class RailsBestPractices::AfterPrepare; end"
  node = parse_ruby(filename, content)
  node.file = filename
  node.prepare(@checker)
end

#after_reviewObject

provide a handler after all files reviewed.



139
140
141
142
143
144
145
# File 'lib/rails_best_practices/core/runner.rb', line 139

def after_review
  filename = "rails_best_practices.after_review"
  content = "class RailsBestPractices::AfterReview; end"
  node = parse_ruby(filename, content)
  node.file = filename
  node.review(@checker)
end

#errorsArray

get all errors from lexicals and reviews.

Returns:

  • (Array)

    all errors from lexicals and reviews



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

def errors
  (@reviews + @lexicals).collect {|check| check.errors}.flatten
end

#lexical(filename, content) ⇒ Object

lexical analysis the file.

Parameters:

  • filename (String)

    name of the file

  • content (String)

    content of the file



59
60
61
62
# File 'lib/rails_best_practices/core/runner.rb', line 59

def lexical(filename, content)
  puts filename if @debug
  @checker.lexical(filename, content)
end

#lexical_file(filename) ⇒ Object

lexical analysis the file.

Parameters:

  • filename (String)


67
68
69
# File 'lib/rails_best_practices/core/runner.rb', line 67

def lexical_file(filename)
  lexical(filename, read_file(filename))
end

#prepare(filename, content) ⇒ Object

parepare a file’s content with filename.

Parameters:

  • filename (String)

    name of the file

  • content (String)

    content of the file



75
76
77
78
79
80
81
82
# File 'lib/rails_best_practices/core/runner.rb', line 75

def prepare(filename, content)
  puts filename if @debug
  node = parse_ruby(filename, content)
  if node
    node.file = filename
    node.prepare(@checker)
  end
end

#prepare_file(filename) ⇒ Object

parapare the file.

Parameters:

  • filename (String)


87
88
89
# File 'lib/rails_best_practices/core/runner.rb', line 87

def prepare_file(filename)
  prepare(filename, read_file(filename))
end

#resultsHash

get the results for all active checkers (reviews and lexicals).

Returns:

  • (Hash)

    all checkers details



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

def results
  (@reviews + @lexicals).map &:result
end

#review(filename, content) ⇒ Object

review a file’s content with filename.

Parameters:

  • filename (String)

    name of the file

  • content (String)

    content of the file



95
96
97
98
99
100
101
102
103
# File 'lib/rails_best_practices/core/runner.rb', line 95

def review(filename, content)
  puts filename if @debug
  content = parse_html_template(filename, content)
  node = parse_ruby(filename, content)
  if node
    node.file = filename
    node.review(@checker)
  end
end

#review_file(filename) ⇒ Object

review the file.

Parameters:

  • filename (String)


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

def review_file(filename)
  review(filename, read_file(filename))
end