Class: RailsBestPractices::Core::Check

Inherits:
CodeAnalyzer::Checker
  • Object
show all
Defined in:
lib/rails_best_practices/core/check.rb

Overview

A Check class that takes charge of checking the sexp.

Defined Under Namespace

Modules: Accessable, Callable, Classable, Exceptable, InheritedResourcesable, Moduleable

Constant Summary collapse

ALL_FILES =
/.*/.freeze
CONTROLLER_FILES =
%r{app/(controllers|cells)/.*\.rb$}.freeze
MIGRATION_FILES =
%r{db/migrate/.*\.rb$}.freeze
MODEL_FILES =
%r{app/models/.*\.rb$}.freeze
MAILER_FILES =
%r{app/models/.*mailer\.rb$|app/mailers/.*\.rb}.freeze
VIEW_FILES =
%r{app/(views|cells)/.*\.(erb|haml|slim|builder|rxml)$}.freeze
PARTIAL_VIEW_FILES =
%r{app/(views|cells)/.*/_.*\.(erb|haml|slim|builder|rxml)$}.freeze
ROUTE_FILES =
%r{config/routes.*\.rb}.freeze
SCHEMA_FILE =
%r{db/schema\.rb}.freeze
HELPER_FILES =
%r{app/helpers/.*\.rb$}.freeze
DEPLOY_FILES =
%r{config/deploy.*\.rb}.freeze
CONFIG_FILES =
%r{config/(application|environment|environments/.*)\.rb}.freeze
INITIALIZER_FILES =
%r{config/initializers/.*\.rb}.freeze
CAPFILE =
/Capfile/.freeze
GEMFILE_LOCK =
/Gemfile\.lock/.freeze
SKIP_FILES =
%r{db/schema.rb}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Check

Returns a new instance of Check.



26
27
28
29
30
# File 'lib/rails_best_practices/core/check.rb', line 26

def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

method_missing to catch all start and end process for each node type, like

start_def
end_def
start_call
end_call

if there is a “debug” method defined in check, each node will be output.



90
91
92
93
94
95
96
97
98
# File 'lib/rails_best_practices/core/check.rb', line 90

def method_missing(method_name, *args)
  if method_name.to_s =~ /^start_/
    p args if respond_to?(:debug)
  elsif method_name.to_s =~ /^end_/
    # nothing to do
  else
    super
  end
end

Class Method Details

.debugObject



109
110
111
# File 'lib/rails_best_practices/core/check.rb', line 109

def debug
  @debug = true
end

.debug?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/rails_best_practices/core/check.rb', line 105

def debug?
  @debug == true
end

.url(url = nil) ⇒ Object



101
102
103
# File 'lib/rails_best_practices/core/check.rb', line 101

def url(url = nil)
  url ? @url = url : @url
end

Instance Method Details

#add_error(message, filename = @node.file, line_number = @node.line_number) ⇒ Object

add error if source code violates rails best practice.

Parameters:

  • message, (String)

    is the string message for violation of the rails best practice

  • filename, (String)

    is the filename of source code

  • line_number, (Integer)

    is the line number of the source code which is reviewing



63
64
65
66
67
68
# File 'lib/rails_best_practices/core/check.rb', line 63

def add_error(message, filename = @node.file, line_number = @node.line_number)
  errors <<
    RailsBestPractices::Core::Error.new(
      filename: filename, line_number: line_number, message: message, type: self.class.to_s, url: url
    )
end

#errorsObject

errors that violate the rails best practices.



71
72
73
# File 'lib/rails_best_practices/core/check.rb', line 71

def errors
  @errors ||= []
end

#is_ignored?(node_file) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rails_best_practices/core/check.rb', line 50

def is_ignored?(node_file)
  regex_ignored_files.map { |r| !!r.match(node_file) }.inject(:|)
end

#is_interesting_file?(node_file) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
# File 'lib/rails_best_practices/core/check.rb', line 40

def is_interesting_file?(node_file)
  interesting_files.any? do |pattern|
    if pattern == ALL_FILES
      node_file =~ pattern && node_file !~ SKIP_FILES
    else
      node_file =~ pattern
    end
  end
end

#parse_file?(node_file) ⇒ Boolean

check if the check will need to parse the node file.

Parameters:

  • the (String)

    file name of node.

Returns:

  • (Boolean)

    true if the check will need to parse the file.



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

def parse_file?(node_file)
  node_file.is_a?(String) && is_interesting_file?(node_file) && !is_ignored?(node_file)
end

#regex_ignored_filesObject



54
55
56
# File 'lib/rails_best_practices/core/check.rb', line 54

def regex_ignored_files
  @regex_ignored_files ||= Array(@ignored_files).map { |pattern| Regexp.new(pattern) }
end

#urlString

default url is empty.

Returns:

  • (String)

    the url of rails best practice



78
79
80
# File 'lib/rails_best_practices/core/check.rb', line 78

def url
  self.class.url
end