Class: RailsBestPractices::Reviews::LawOfDemeterReview

Inherits:
Review show all
Defined in:
lib/rails_best_practices/reviews/law_of_demeter_review.rb

Overview

Review to make sure not to avoid the law of demeter.

See the best practice details here rails-bestpractices.com/posts/15-the-law-of-demeter.

Implementation:

Review process:

check all method calls to see if there is method call to the association object.
if there is a call node whose subject is an object of model (compare by name),
and whose message is an association of that model (also compare by name),
and outer the call node, it is also a call node,
then it violate the law of demeter.

Constant Summary collapse

ASSOCIATION_METHODS =
%w(belongs_to has_one)

Constants inherited from Core::Check

Core::Check::ALL_FILES, Core::Check::CONTROLLER_FILES, Core::Check::DEPLOY_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILES, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Method Summary collapse

Methods inherited from Review

#model_associations, #model_attributes, #models, #remember_variable_use_count, #reset_variable_use_count, #variable, #variable_use_count

Methods inherited from Core::Check

add_callback, #add_error, #after_prepare, #after_review, callbacks, #errors, #increment_total_files_checked!, #initialize, interesting_files, #interesting_files, #interesting_nodes, interesting_nodes, #method_missing, #node_end, #node_start, #parse_file?, #result, #total_files_checked

Constructor Details

This class inherits a constructor from RailsBestPractices::Core::Check

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check

Instance Method Details

#start_call(node) ⇒ Object

check the call node,

if the subject of the call node is also a call node, and the subject of the subject call node matchs one of the class names, and the message of the subject call node matchs one of the association name with the class name, then it violates the law of demeter.



34
35
36
37
38
# File 'lib/rails_best_practices/reviews/law_of_demeter_review.rb', line 34

def start_call(node)
  if :call == node.subject.sexp_type && need_delegate?(node)
    add_error "law of demeter"
  end
end

#urlObject



24
25
26
# File 'lib/rails_best_practices/reviews/law_of_demeter_review.rb', line 24

def url
  "http://rails-bestpractices.com/posts/15-the-law-of-demeter"
end