Sha256: 47d569f3e74090bf5ef5b794f804b82c3f277b67ddf0c7777285646b1c53e491

Contents?: true

Size: 914 Bytes

Versions: 5

Compression:

Stored size: 914 Bytes

Contents

module Pelusa
  module Lint
    class InstanceVariables
      def initialize
        @ivars = Set.new
      end

      def check(klass)
        initialize
        iterate_lines!(klass)

        return SuccessfulAnalysis.new(name) if @ivars.length < limit

        FailedAnalysis.new(name, @ivars) do |ivars|
          "This class uses #{ivars.length} instance variables: #{ivars.to_a.join(', ')}."
        end
      end

      private

      def name
        "Uses less than #{limit} ivars"
      end

      def limit
        Pelusa.configuration['InstanceVariables'].fetch('limit', 3)
      end

      def iterate_lines!(klass)
        iterator = Iterator.new do |node|
          if node.is_a?(Rubinius::AST::InstanceVariableAccess) || node.is_a?(Rubinius::AST::InstanceVariableAssignment)
            @ivars << node.name
          end
        end
        Array(klass).each(&iterator)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pelusa-0.2.2 lib/pelusa/lint/instance_variables.rb
pelusa-0.2.1 lib/pelusa/lint/instance_variables.rb
pelusa-0.2.0 lib/pelusa/lint/instance_variables.rb
pelusa-0.1.1 lib/pelusa/lint/instance_variables.rb
pelusa-0.1.0 lib/pelusa/lint/instance_variables.rb