Class: RailsBestPractices::Core::Check

Inherits:
Object
  • 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, Afterable, Callable, Classable, Exceptable, InheritedResourcesable, Moduleable

Constant Summary collapse

ALL_FILES =
/.*/
CONTROLLER_FILES =
/(controllers|cells)\/.*\.rb$/
MIGRATION_FILES =
/db\/migrate\/.*\.rb$/
MODEL_FILES =
/models\/.*\.rb$/
MAILER_FILES =
/models\/.*mailer\.rb$|mailers\/.*mailer\.rb/
VIEW_FILES =
/(views|cells)\/.*\.(erb|haml|slim|builder|rxml)$/
PARTIAL_VIEW_FILES =
/(views|cells)\/.*\/_.*\.(erb|haml|slim|builder|rxml)$/
ROUTE_FILES =
/config\/routes.*\.rb/
SCHEMA_FILE =
/db\/schema\.rb/
HELPER_FILES =
/helpers\/.*\.rb$/
DEPLOY_FILES =
/config\/deploy.*\.rb/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Check

Returns a new instance of Check.



18
19
20
21
22
# File 'lib/rails_best_practices/core/check.rb', line 18

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.



134
135
136
137
138
139
140
141
142
# File 'lib/rails_best_practices/core/check.rb', line 134

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

.add_callback(name, &block) ⇒ Object

add a callback.

Parameters:

  • name, (String)

    callback name, can be start_xxx or end_xxx

  • block, (Proc)

    be executed when callbacks are called



166
167
168
169
# File 'lib/rails_best_practices/core/check.rb', line 166

def add_callback(name, &block)
  callbacks[name] ||= []
  callbacks[name] << block
end

.callbacksObject

callbacks for start_xxx and end_xxx.



158
159
160
# File 'lib/rails_best_practices/core/check.rb', line 158

def callbacks
  @callbacks ||= {}
end

.interesting_files(*file_patterns) ⇒ Object



151
152
153
154
155
# File 'lib/rails_best_practices/core/check.rb', line 151

def interesting_files(*file_patterns)
  @interesting_files ||= []
  @interesting_files += file_patterns
  @interesting_files.uniq
end

.interesting_nodes(*nodes) ⇒ Object



145
146
147
148
149
# File 'lib/rails_best_practices/core/check.rb', line 145

def interesting_nodes(*nodes)
  @interesting_nodes ||= []
  @interesting_nodes += nodes
  @interesting_nodes.uniq
end

Instance Method Details

#add_error(message, filename = @node.file, line_number = @node.line) ⇒ 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



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

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

#after_prepareObject



82
# File 'lib/rails_best_practices/core/check.rb', line 82

def after_prepare; end

#after_reviewObject



83
# File 'lib/rails_best_practices/core/check.rb', line 83

def after_review; end

#errorsObject

errors that vialote the rails best practices.



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

def errors
  @errors ||= []
end

#increment_total_files_checked!(increment = 1) ⇒ Integer

increments by one the number of files checked.

Returns:

  • (Integer)

    the number of files checked by this checker



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

def increment_total_files_checked!(increment=1)
  @total_files_checked = total_files_checked + increment
end

#interesting_filesObject

interesting files that the check will parse.



42
43
44
# File 'lib/rails_best_practices/core/check.rb', line 42

def interesting_files
  self.class.interesting_files
end

#interesting_nodesObject

interesting nodes that the check will parse.



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

def interesting_nodes
  self.class.interesting_nodes
end

#node_end(node) ⇒ Object

delegate to end_### according to the sexp_type, like

end_call
end_def

Parameters:



74
75
76
77
78
79
80
# File 'lib/rails_best_practices/core/check.rb', line 74

def node_end(node)
  @node = node
  self.send("end_#{node.sexp_type}", node)
  Array(self.class.callbacks["end_#{node.sexp_type}"]).each do |callback|
    self.instance_exec node, &callback
  end
end

#node_start(node) ⇒ Object

delegate to start_### according to the sexp_type, like

start_call
start_def

Parameters:



60
61
62
63
64
65
66
# File 'lib/rails_best_practices/core/check.rb', line 60

def node_start(node)
  @node = node
  Array(self.class.callbacks["start_#{node.sexp_type}"]).each do |callback|
    self.instance_exec node, &callback
  end
  self.send("start_#{node.sexp_type}", node)
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.



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

def parse_file?(node_file)
  interesting_files.any? { |pattern| node_file =~ pattern }
end

#resultObject

Hash representation of the checker



25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_best_practices/core/check.rb', line 25

def result
  class_name = self.class.to_s.split("::")
  { :checker_name  => class_name.last,
    :checker_type  => class_name[-2],
    :error_count   => errors.size,
    :checker_url   => url, 
   :errors        => errors,
   :files_checked => total_files_checked
  }
end

#total_files_checkedInteger

default value is 0.

Returns:

  • (Integer)

    the number of files checked by this checker



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

def total_files_checked
  @total_files_checked || 0
end

#urlString

default url is empty.

Returns:

  • (String)

    the url of rails best practice



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

def url
  ""
end