Sha256: a5a4528e5062c6ba5deb3446b29c7e28bc496c52e5b123d6f2a1e975aa5780c8

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# encoding: utf-8
require 'rails_best_practices/core/error'

module RailsBestPractices
  module Checks
    class Check
      NODE_TYPES = [:call, :defn, :defs, :if, :unless, :class, :lasgn, :iasgn, :ivar, :lvar, :block, :iter]
      
      CONTROLLER_FILES = /_controller\.rb$/
      MIGRATION_FILES = /db\/migrate\/.*\.rb$/
      MODLE_FILES = /models\/.*\.rb$/
      VIEW_FILES = /views\/.*\.(erb|haml)$/
      PARTIAL_VIEW_FILES = /views\/.*\/_.*\.(erb|haml)$/

      attr_reader :errors

      def initialize
        @errors = []
      end
      
      def interesting_files
        /.*/
      end
  
      NODE_TYPES.each do |node|
        start_node_method = "evaluate_start_#{node}"
        end_node_method = "evaluate_end_#{node}"
        define_method(start_node_method) { |node| } unless self.respond_to?(start_node_method)
        define_method(end_node_method) { |node| } unless self.respond_to?(end_node_method)
      end

      def position(offset = 0)
        "#{@line[2]}:#{@line[1] + offset}"
      end
      
      def evaluate_start(node)
      end

      def evaluate_end(node)
      end

      def evaluate_node(position, node)
        @node = node
        eval_method = "evaluate_#{position}_#{node.node_type}"
        self.send(eval_method, node)
      end

      def evaluate_node_start(node)
        evaluate_node(:start, node)
        evaluate_start(node)
      end
  
      def evaluate_node_end(node)
        evaluate_node(:end, node)
        evaluate_end(node)
      end
      
      def add_error(error, file = nil, line = nil)
        file ||= @node.file
        line ||= @node.line
        @errors << RailsBestPractices::Core::Error.new("#{file}", "#{line}", error)
      end
      
      def equal?(node, expected)
        node.to_s == expected or node.to_s == ':' + expected.to_s
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_best_practices-0.5.3 lib/rails_best_practices/checks/check.rb
rails_best_practices-0.5.2 lib/rails_best_practices/checks/check.rb
rails_best_practices-0.5.1 lib/rails_best_practices/checks/check.rb
rails_best_practices-0.5.0 lib/rails_best_practices/checks/check.rb