Sha256: f18f21b6b2f57b264ea4a38bfbc6ad454515f10e7512c829419f28d505ac2dfb

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Pelusa
  module Lint
    class DemeterLaw
      def initialize
        @violations = Set.new
      end

      def check(klass)
        initialize
        iterate_lines!(klass)

        return SuccessfulAnalysis.new(name) if @violations.empty?

        FailedAnalysis.new(name, @violations) do |violations|
          "There are #{violations.length} Demeter law violations in lines #{violations.to_a.join(', ')}."
        end
      end

      private

      def name
        "Respects Demeter law"
      end

      def iterate_lines!(klass)
        iterator = Iterator.new do |node|
          if node.is_a?(Rubinius::AST::Send) && node.receiver.is_a?(Rubinius::AST::Send)
            @violations << node.line unless white_listed?(node.receiver.name)
          end
        end
        Array(klass).each(&iterator)
      end

      def white_listed? method
        [Class, Fixnum, Enumerable].any? do |enclosing_module|
          enclosing_module.instance_methods.any? {|instance_method| instance_method == method }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pelusa-0.2.2 lib/pelusa/lint/demeter_law.rb
pelusa-0.2.1 lib/pelusa/lint/demeter_law.rb
pelusa-0.2.0 lib/pelusa/lint/demeter_law.rb