Sha256: e3aef34dbf732562d81c0a006d0906877eca89c5629bd5fc54ab61116cbbf2ab

Contents?: true

Size: 1.26 KB

Versions: 9

Compression:

Stored size: 1.26 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, parser_engine)
        return @load if defined?(@load)

        @load = load!(target_ruby_version, parser_engine)
      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, parser_engine)
        path = db_schema_path
        return unless path

        ast = RuboCop::ProcessedSource.new(File.read(path), target_ruby_version, path, parser_engine: parser_engine).ast

        Schema.new(ast) if ast
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
rubocop-rails-2.27.0 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.26.2 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.26.1 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.26.0 lib/rubocop/rails/schema_loader.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.25.1 lib/rubocop/rails/schema_loader.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.24.1 lib/rubocop/rails/schema_loader.rb
rubocop-rails-2.24.0 lib/rubocop/rails/schema_loader.rb