Sha256: 5e92d28a530079c403d10464f2c33177e11de9d2a42aab6c05c6ad1b9643ddf2

Contents?: true

Size: 1.31 KB

Versions: 32

Compression:

Stored size: 1.31 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # 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
    class LawOfDemeterCheck < Check
      
      def interesting_nodes
        [:call, :class]
      end

      def initialize
        super
        @associations = {}
      end

      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

      private

      def remember_belongs_to(node)
        node.body.grep_nodes(:message => :belongs_to).collect do |body_node|
          class_name = node.subject.to_s.underscore
          @associations[class_name] ||= []
          @associations[class_name] << body_node.arguments[1].to_ruby_string
        end
      end

      def need_delegate?(node)
        @associations.each do |class_name, associations|
          return true if node.subject.to_ruby =~ /#{class_name}$/ and associations.include? node.message.to_s
        end
        false
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
rails_best_practices-0.3.23 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.22 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.21 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.20 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.19 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.18 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.17 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.14 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.13 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.12 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.11 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.10 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.9 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.8 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.7 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.6 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.5 lib/rails_best_practices/checks/law_of_demeter_check.rb
rails_best_practices-0.3.4 lib/rails_best_practices/checks/law_of_demeter_check.rb