class RailsBestPractices::Core::Check

A Check class that takes charge of checking the sexp.

Constants

ALL_FILES
CAPFILE
CONFIG_FILES
CONTROLLER_FILES
DEPLOY_FILES
GEMFILE_LOCK
HELPER_FILES
INITIALIZER_FILES
MAILER_FILES
MIGRATION_FILES
MODEL_FILES
PARTIAL_VIEW_FILES
ROUTE_FILES
SCHEMA_FILE
SKIP_FILES
VIEW_FILES

Public Class Methods

debug() click to toggle source
# File lib/rails_best_practices/core/check.rb, line 109
def debug
  @debug = true
end
debug?() click to toggle source
# File lib/rails_best_practices/core/check.rb, line 105
def debug?
  @debug == true
end
new(options = {}) click to toggle source
# 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
url(url = nil) click to toggle source
# File lib/rails_best_practices/core/check.rb, line 101
def url(url = nil)
  url ? @url = url : @url
end

Public Instance Methods

add_error(message, filename = @node.file, line_number = @node.line_number) click to toggle source

add error if source code violates rails best practice.

@param [String] message, is the string message for violation of the rails best practice @param [String] filename, is the filename of source code @param [Integer] line_number, is the line number of the source code which is reviewing

# 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
errors() click to toggle source

errors that violate the rails best practices.

# File lib/rails_best_practices/core/check.rb, line 71
def errors
  @errors ||= []
end
is_ignored?(node_file) click to toggle source
# 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) click to toggle source
# 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
method_missing(method_name, *args) click to toggle source

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.

Calls superclass method
# 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
parse_file?(node_file) click to toggle source

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

@param [String] the file name of node. @return [Boolean] true if the check will need to parse the file.

# 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_files() click to toggle source
# 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
url() click to toggle source

default url is empty.

@return [String] the url of rails best practice

# File lib/rails_best_practices/core/check.rb, line 78
def url
  self.class.url
end