Class: RailsBestPractices::Core::Check
- Inherits:
-
Object
- Object
- RailsBestPractices::Core::Check
- Defined in:
- lib/rails_best_practices/core/check.rb
Overview
A Check class that takes charge of checking the sexp.
Direct Known Subclasses
Lexicals::RemoveTabCheck, Lexicals::RemoveTrailingWhitespaceCheck, Prepares::ControllerPrepare, Prepares::HelperPrepare, Prepares::MailerPrepare, Prepares::ModelPrepare, Prepares::RoutePrepare, Prepares::SchemaPrepare, Reviews::Review
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
-
.add_callback(name, &block) ⇒ Object
add a callback.
-
.callbacks ⇒ Object
callbacks for start_xxx and end_xxx.
- .interesting_files(*file_patterns) ⇒ Object
- .interesting_nodes(*nodes) ⇒ Object
Instance Method Summary collapse
-
#add_error(message, filename = @node.file, line_number = @node.line) ⇒ Object
add error if source code violates rails best practice.
- #after_prepare ⇒ Object
- #after_review ⇒ Object
-
#errors ⇒ Object
errors that vialote the rails best practices.
-
#increment_total_files_checked!(increment = 1) ⇒ Integer
increments by one the number of files checked.
-
#initialize(options = {}) ⇒ Check
constructor
A new instance of Check.
-
#interesting_files ⇒ Object
interesting files that the check will parse.
-
#interesting_nodes ⇒ Object
interesting nodes that the check will parse.
-
#method_missing(method_name, *args) ⇒ Object
method_missing to catch all start and end process for each node type, like.
-
#node_end(node) ⇒ Object
delegate to end_### according to the sexp_type, like.
-
#node_start(node) ⇒ Object
delegate to start_### according to the sexp_type, like.
-
#parse_file?(node_file) ⇒ Boolean
check if the check will need to parse the node file.
-
#result ⇒ Object
Hash representation of the checker.
-
#total_files_checked ⇒ Integer
default value is 0.
-
#url ⇒ String
default url is empty.
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(={}) .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.
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 |
.callbacks ⇒ Object
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.
90 91 92 93 94 95 96 97 98 |
# File 'lib/rails_best_practices/core/check.rb', line 90 def add_error(, filename = @node.file, line_number = @node.line) errors << RailsBestPractices::Core::Error.new( :filename => filename, :line_number => line_number, :message => , :type => self.class.to_s, :url => url ) end |
#after_prepare ⇒ Object
82 |
# File 'lib/rails_best_practices/core/check.rb', line 82 def after_prepare; end |
#after_review ⇒ Object
83 |
# File 'lib/rails_best_practices/core/check.rb', line 83 def after_review; end |
#errors ⇒ Object
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.
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_files ⇒ Object
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_nodes ⇒ Object
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
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
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.
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 |
#result ⇒ Object
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_checked ⇒ Integer
default value is 0.
115 116 117 |
# File 'lib/rails_best_practices/core/check.rb', line 115 def total_files_checked @total_files_checked || 0 end |
#url ⇒ String
default url is empty.
108 109 110 |
# File 'lib/rails_best_practices/core/check.rb', line 108 def url "" end |