Module: RuboCop::Rails::SchemaLoader

Extended by:
SchemaLoader
Included in:
SchemaLoader
Defined in:
lib/rubocop/rails/schema_loader.rb,
lib/rubocop/rails/schema_loader/schema.rb

Overview

It loads db/schema.rb and return Schema object. Cops refers database schema information with this module.

Defined Under Namespace

Classes: AddIndex, Column, Index, Schema, Table

Instance Method Summary collapse

Instance Method Details

#db_schema_pathObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/rails/schema_loader.rb', line 27

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

#load(target_ruby_version, parser_engine) ⇒ Schema?

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.

Returns:



15
16
17
18
19
# File 'lib/rubocop/rails/schema_loader.rb', line 15

def load(target_ruby_version, parser_engine)
  return @load if defined?(@load)

  @load = load!(target_ruby_version, parser_engine)
end

#reset!Object



21
22
23
24
25
# File 'lib/rubocop/rails/schema_loader.rb', line 21

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

  remove_instance_variable(:@load)
end