Sha256: 53bae455ccb02678cd3a78e443e0e3cc5a3dd9091697b52e943a700aff76f01e

Contents?: true

Size: 1.47 KB

Versions: 48

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Rails
    # It loads db/schema.rb and return Schema object.
    # Cops refers database schema information with this module.
    module SchemaLoader
      extend self

      # It parses `db/schema.rb` and return it.
      # It returns `nil` if it can't find `db/schema.rb`.
      # So a cop that uses the loader should handle `nil` properly.
      #
      # @return [Schema, nil]
      def load(target_ruby_version)
        return @load if defined?(@load)

        @load = load!(target_ruby_version)
      end

      def reset!
        return unless instance_variable_defined?(:@load)

        remove_instance_variable(:@load)
      end

      def db_schema_path
        path = Pathname.pwd
        until path.root?
          schema_path = path.join('db/schema.rb')
          return schema_path if schema_path.exist?

          path = path.join('../').cleanpath
        end

        nil
      end

      private

      def load!(target_ruby_version)
        path = db_schema_path
        return unless path

        ast = parse(path, target_ruby_version)
        Schema.new(ast)
      end

      def parse(path, target_ruby_version)
        klass_name = :"Ruby#{target_ruby_version.to_s.sub('.', '')}"
        klass = ::Parser.const_get(klass_name)
        parser = klass.new(RuboCop::AST::Builder.new)

        buffer = Parser::Source::Buffer.new(path, 1)
        buffer.source = path.read

        parser.parse(buffer)
      end
    end
  end
end

Version data entries

48 entries across 43 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/rails/schema_loader.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/rails/schema_loader.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/rails/schema_loader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/rails/schema_loader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/rails/schema_loader.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/rails/schema_loader.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/rails/schema_loader.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/rails/schema_loader.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/rails/schema_loader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/rails/schema_loader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/rails/schema_loader.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.20.2 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.20.1 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.20.0 lib/rubocop/rails/schema_loader.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.19.1 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.19.0 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.18.0 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.17.4 lib/rubocop/rails/schema_loader.rb