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
# 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::DEFAULT_CONFIG

  prepares = Array(options[:prepares])
  reviews = Array(options[:reviews])
  @lexicals = load_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

#errorsArray

get all errors from lexicals and reviews.

Returns:

  • (Array)

    all errors from lexicals and reviews



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

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



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

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

#lexical_file(filename) ⇒ Object

lexical analysis the file.

Parameters:

  • filename (String)


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

def lexical_file(filename)
  lexical(filename, File.open(filename, "r:UTF-8") { |f| f.read })
end