Files

RailsBestPractices::Checks::LawOfDemeterCheck

Check to make sure not avoid the law of demeter.

Implementation:

  1. check all models to record belongs_to associations

  2. check if calling belongs_to association’s method or attribute

Public Class Methods

new() click to toggle source
# File lib/rails_best_practices/checks/law_of_demeter_check.rb, line 16
def initialize
  super
  @associations = {}
end

Public Instance Methods

evaluate_start(node) click to toggle source
# File lib/rails_best_practices/checks/law_of_demeter_check.rb, line 21
def evaluate_start(node)
  if node.node_type == :class
    remember_belongs_to(node)
  elsif [:lvar, :ivar].include?(node.subject.node_type) and node.subject != s(:lvar, :_erbout)
    add_error "law of demeter" if need_delegate?(node)
  end
end
interesting_nodes() click to toggle source
# File lib/rails_best_practices/checks/law_of_demeter_check.rb, line 12
def interesting_nodes
  [:call, :class]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.